diff --git a/README.md b/README.md index e4f4f3462aac57fcdd4247e12f1a940f1bbf1a9d..ecdf0da666d14adfbdd174a1ca238a89119a7121 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ not fit in its context window. Each task streams a sequence of operations as ord user turns, more than the context budget holds, and the reward is read from the agent's own messages: what survived in its context, not what it wrote to disk. -Paper: [https://huggingface.co/papers/2609.37725](https://huggingface.co/papers/2609.37725) +Paper: [Context Language Models](https://arxiv.org/abs/2609.37725) ([paper page](https://huggingface.co/papers/2609.37725)) ## Families @@ -73,6 +73,9 @@ and a reserve of 2048. | `gold` | string (JSON) | the answers the graders score against | | `meta` | string (JSON) | generator settings and instance statistics | +The budget is counted in `o200k_base` tokens for every agent. An agent may enforce a +stricter budget of its own; that is part of the agent, not of the benchmark. + Each operation is shown to the agent as `=== {header} ({remaining} op(s) remaining after this) ===` followed by its body. @@ -112,8 +115,15 @@ result = run(my_agent, inst, budget=row["budget"], reserve=row["reserve"], The `harbor/` directory holds one Harbor task per instance, and `registry.json` lists the datasets `contextbench` (every task) and `contextbench-` -(for example `contextbench-kv-store`). Download the repository, then run Harbor -from inside it: +(for example `contextbench-kv-store`). The same tasks are on GitHub, so Harbor runs +them in one line: + +```bash +harbor jobs start --repo RulinShao/ContextBench -d contextbench \ + -a contextbench.harbor.agents:Terminus2Stream -m +``` + +Or download this repository and run Harbor from inside it: ```bash hf download context-language-model/ContextBench --repo-type dataset --local-dir contextbench-hf @@ -123,10 +133,25 @@ harbor jobs start --registry-path registry.json -d contextbench \ ``` Use `-d contextbench-sudoku-sketchpad` for one family, `-i ` for one task, -`-p harbor/` to run the directory without the registry, or `-a oracle` to check a -task with its reference solution. Agents that deliver operations into a running +`-p harbor/` to run the downloaded directory without the registry, or `-a oracle` to +check a task with its reference solution. Agents that deliver operations into a running conversation ship with the `contextbench` package (`contextbench.harbor.agents`). +## Citation + +ContextBench was introduced in the Context Language Models paper. If you use it, please cite: + +```bibtex +@article{shao2026context, + title = {Context Language Models}, + author = {Shao, Rulin and Shen, Shannon Zejiang and Yin, Junjie Oscar and Li, Yuetai and + Wang, Minheng and Ivison, Hamish and Poovendran, Radha and Lambert, Nathan and + Xiao, Teng and Lewis, Mike and Yih, Wen-tau and Zettlemoyer, Luke and Koh, Pang Wei}, + journal = {arXiv preprint arXiv:2609.37725}, + year = {2026} +} +``` + ## License Apache-2.0. diff --git a/harbor/kv-store-R1000-seed42/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R1000-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R1000-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R1000-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R1000-seed42/tests/_cb/contextbench/runner.py b/harbor/kv-store-R1000-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R1000-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R1000-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R1000-seed42/tests/_cb/contextbench/stream.py b/harbor/kv-store-R1000-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R1000-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R1000-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R1000-seed43/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R1000-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R1000-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R1000-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R1000-seed43/tests/_cb/contextbench/runner.py b/harbor/kv-store-R1000-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R1000-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R1000-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R1000-seed43/tests/_cb/contextbench/stream.py b/harbor/kv-store-R1000-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R1000-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R1000-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R1000-seed44/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R1000-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R1000-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R1000-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R1000-seed44/tests/_cb/contextbench/runner.py b/harbor/kv-store-R1000-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R1000-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R1000-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R1000-seed44/tests/_cb/contextbench/stream.py b/harbor/kv-store-R1000-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R1000-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R1000-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R1000-seed45/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R1000-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R1000-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R1000-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R1000-seed45/tests/_cb/contextbench/runner.py b/harbor/kv-store-R1000-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R1000-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R1000-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R1000-seed45/tests/_cb/contextbench/stream.py b/harbor/kv-store-R1000-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R1000-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R1000-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R1000-seed46/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R1000-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R1000-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R1000-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R1000-seed46/tests/_cb/contextbench/runner.py b/harbor/kv-store-R1000-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R1000-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R1000-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R1000-seed46/tests/_cb/contextbench/stream.py b/harbor/kv-store-R1000-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R1000-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R1000-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R1000-seed47/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R1000-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R1000-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R1000-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R1000-seed47/tests/_cb/contextbench/runner.py b/harbor/kv-store-R1000-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R1000-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R1000-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R1000-seed47/tests/_cb/contextbench/stream.py b/harbor/kv-store-R1000-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R1000-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R1000-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R1000-seed48/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R1000-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R1000-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R1000-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R1000-seed48/tests/_cb/contextbench/runner.py b/harbor/kv-store-R1000-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R1000-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R1000-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R1000-seed48/tests/_cb/contextbench/stream.py b/harbor/kv-store-R1000-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R1000-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R1000-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R1000-seed49/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R1000-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R1000-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R1000-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R1000-seed49/tests/_cb/contextbench/runner.py b/harbor/kv-store-R1000-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R1000-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R1000-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R1000-seed49/tests/_cb/contextbench/stream.py b/harbor/kv-store-R1000-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R1000-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R1000-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R130-seed42/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R130-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R130-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R130-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R130-seed42/tests/_cb/contextbench/runner.py b/harbor/kv-store-R130-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R130-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R130-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R130-seed42/tests/_cb/contextbench/stream.py b/harbor/kv-store-R130-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R130-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R130-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R130-seed43/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R130-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R130-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R130-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R130-seed43/tests/_cb/contextbench/runner.py b/harbor/kv-store-R130-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R130-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R130-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R130-seed43/tests/_cb/contextbench/stream.py b/harbor/kv-store-R130-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R130-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R130-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R130-seed44/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R130-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R130-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R130-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R130-seed44/tests/_cb/contextbench/runner.py b/harbor/kv-store-R130-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R130-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R130-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R130-seed44/tests/_cb/contextbench/stream.py b/harbor/kv-store-R130-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R130-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R130-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R130-seed45/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R130-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R130-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R130-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R130-seed45/tests/_cb/contextbench/runner.py b/harbor/kv-store-R130-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R130-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R130-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R130-seed45/tests/_cb/contextbench/stream.py b/harbor/kv-store-R130-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R130-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R130-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R130-seed46/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R130-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R130-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R130-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R130-seed46/tests/_cb/contextbench/runner.py b/harbor/kv-store-R130-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R130-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R130-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R130-seed46/tests/_cb/contextbench/stream.py b/harbor/kv-store-R130-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R130-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R130-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R130-seed47/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R130-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R130-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R130-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R130-seed47/tests/_cb/contextbench/runner.py b/harbor/kv-store-R130-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R130-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R130-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R130-seed47/tests/_cb/contextbench/stream.py b/harbor/kv-store-R130-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R130-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R130-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R130-seed48/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R130-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R130-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R130-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R130-seed48/tests/_cb/contextbench/runner.py b/harbor/kv-store-R130-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R130-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R130-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R130-seed48/tests/_cb/contextbench/stream.py b/harbor/kv-store-R130-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R130-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R130-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R130-seed49/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R130-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R130-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R130-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R130-seed49/tests/_cb/contextbench/runner.py b/harbor/kv-store-R130-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R130-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R130-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R130-seed49/tests/_cb/contextbench/stream.py b/harbor/kv-store-R130-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R130-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R130-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R16000-seed42/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R16000-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R16000-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R16000-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R16000-seed42/tests/_cb/contextbench/runner.py b/harbor/kv-store-R16000-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R16000-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R16000-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R16000-seed42/tests/_cb/contextbench/stream.py b/harbor/kv-store-R16000-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R16000-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R16000-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R16000-seed43/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R16000-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R16000-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R16000-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R16000-seed43/tests/_cb/contextbench/runner.py b/harbor/kv-store-R16000-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R16000-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R16000-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R16000-seed43/tests/_cb/contextbench/stream.py b/harbor/kv-store-R16000-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R16000-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R16000-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R16000-seed44/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R16000-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R16000-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R16000-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R16000-seed44/tests/_cb/contextbench/runner.py b/harbor/kv-store-R16000-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R16000-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R16000-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R16000-seed44/tests/_cb/contextbench/stream.py b/harbor/kv-store-R16000-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R16000-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R16000-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R16000-seed45/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R16000-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R16000-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R16000-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R16000-seed45/tests/_cb/contextbench/runner.py b/harbor/kv-store-R16000-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R16000-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R16000-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R16000-seed45/tests/_cb/contextbench/stream.py b/harbor/kv-store-R16000-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R16000-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R16000-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R16000-seed46/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R16000-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R16000-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R16000-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R16000-seed46/tests/_cb/contextbench/runner.py b/harbor/kv-store-R16000-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R16000-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R16000-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R16000-seed46/tests/_cb/contextbench/stream.py b/harbor/kv-store-R16000-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R16000-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R16000-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R16000-seed47/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R16000-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R16000-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R16000-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R16000-seed47/tests/_cb/contextbench/runner.py b/harbor/kv-store-R16000-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R16000-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R16000-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R16000-seed47/tests/_cb/contextbench/stream.py b/harbor/kv-store-R16000-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R16000-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R16000-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R16000-seed48/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R16000-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R16000-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R16000-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R16000-seed48/tests/_cb/contextbench/runner.py b/harbor/kv-store-R16000-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R16000-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R16000-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R16000-seed48/tests/_cb/contextbench/stream.py b/harbor/kv-store-R16000-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R16000-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R16000-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R16000-seed49/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R16000-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R16000-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R16000-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R16000-seed49/tests/_cb/contextbench/runner.py b/harbor/kv-store-R16000-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R16000-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R16000-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R16000-seed49/tests/_cb/contextbench/stream.py b/harbor/kv-store-R16000-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R16000-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R16000-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R2000-seed42/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R2000-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R2000-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R2000-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R2000-seed42/tests/_cb/contextbench/runner.py b/harbor/kv-store-R2000-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R2000-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R2000-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R2000-seed42/tests/_cb/contextbench/stream.py b/harbor/kv-store-R2000-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R2000-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R2000-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R2000-seed43/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R2000-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R2000-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R2000-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R2000-seed43/tests/_cb/contextbench/runner.py b/harbor/kv-store-R2000-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R2000-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R2000-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R2000-seed43/tests/_cb/contextbench/stream.py b/harbor/kv-store-R2000-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R2000-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R2000-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R2000-seed44/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R2000-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R2000-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R2000-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R2000-seed44/tests/_cb/contextbench/runner.py b/harbor/kv-store-R2000-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R2000-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R2000-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R2000-seed44/tests/_cb/contextbench/stream.py b/harbor/kv-store-R2000-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R2000-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R2000-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R2000-seed45/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R2000-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R2000-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R2000-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R2000-seed45/tests/_cb/contextbench/runner.py b/harbor/kv-store-R2000-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R2000-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R2000-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R2000-seed45/tests/_cb/contextbench/stream.py b/harbor/kv-store-R2000-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R2000-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R2000-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R2000-seed46/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R2000-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R2000-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R2000-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R2000-seed46/tests/_cb/contextbench/runner.py b/harbor/kv-store-R2000-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R2000-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R2000-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R2000-seed46/tests/_cb/contextbench/stream.py b/harbor/kv-store-R2000-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R2000-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R2000-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R2000-seed47/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R2000-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R2000-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R2000-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R2000-seed47/tests/_cb/contextbench/runner.py b/harbor/kv-store-R2000-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R2000-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R2000-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R2000-seed47/tests/_cb/contextbench/stream.py b/harbor/kv-store-R2000-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R2000-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R2000-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R2000-seed48/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R2000-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R2000-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R2000-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R2000-seed48/tests/_cb/contextbench/runner.py b/harbor/kv-store-R2000-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R2000-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R2000-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R2000-seed48/tests/_cb/contextbench/stream.py b/harbor/kv-store-R2000-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R2000-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R2000-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R2000-seed49/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R2000-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R2000-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R2000-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R2000-seed49/tests/_cb/contextbench/runner.py b/harbor/kv-store-R2000-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R2000-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R2000-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R2000-seed49/tests/_cb/contextbench/stream.py b/harbor/kv-store-R2000-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R2000-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R2000-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R260-seed42/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R260-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R260-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R260-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R260-seed42/tests/_cb/contextbench/runner.py b/harbor/kv-store-R260-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R260-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R260-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R260-seed42/tests/_cb/contextbench/stream.py b/harbor/kv-store-R260-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R260-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R260-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R260-seed43/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R260-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R260-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R260-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R260-seed43/tests/_cb/contextbench/runner.py b/harbor/kv-store-R260-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R260-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R260-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R260-seed43/tests/_cb/contextbench/stream.py b/harbor/kv-store-R260-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R260-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R260-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R260-seed44/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R260-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R260-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R260-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R260-seed44/tests/_cb/contextbench/runner.py b/harbor/kv-store-R260-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R260-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R260-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R260-seed44/tests/_cb/contextbench/stream.py b/harbor/kv-store-R260-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R260-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R260-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R260-seed45/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R260-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R260-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R260-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R260-seed45/tests/_cb/contextbench/runner.py b/harbor/kv-store-R260-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R260-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R260-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R260-seed45/tests/_cb/contextbench/stream.py b/harbor/kv-store-R260-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R260-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R260-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R260-seed46/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R260-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R260-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R260-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R260-seed46/tests/_cb/contextbench/runner.py b/harbor/kv-store-R260-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R260-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R260-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R260-seed46/tests/_cb/contextbench/stream.py b/harbor/kv-store-R260-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R260-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R260-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R260-seed47/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R260-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R260-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R260-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R260-seed47/tests/_cb/contextbench/runner.py b/harbor/kv-store-R260-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R260-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R260-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R260-seed47/tests/_cb/contextbench/stream.py b/harbor/kv-store-R260-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R260-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R260-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R260-seed48/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R260-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R260-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R260-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R260-seed48/tests/_cb/contextbench/runner.py b/harbor/kv-store-R260-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R260-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R260-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R260-seed48/tests/_cb/contextbench/stream.py b/harbor/kv-store-R260-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R260-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R260-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R260-seed49/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R260-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R260-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R260-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R260-seed49/tests/_cb/contextbench/runner.py b/harbor/kv-store-R260-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R260-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R260-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R260-seed49/tests/_cb/contextbench/stream.py b/harbor/kv-store-R260-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R260-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R260-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R4000-seed42/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R4000-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R4000-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R4000-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R4000-seed42/tests/_cb/contextbench/runner.py b/harbor/kv-store-R4000-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R4000-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R4000-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R4000-seed42/tests/_cb/contextbench/stream.py b/harbor/kv-store-R4000-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R4000-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R4000-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R4000-seed43/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R4000-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R4000-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R4000-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R4000-seed43/tests/_cb/contextbench/runner.py b/harbor/kv-store-R4000-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R4000-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R4000-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R4000-seed43/tests/_cb/contextbench/stream.py b/harbor/kv-store-R4000-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R4000-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R4000-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R4000-seed44/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R4000-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R4000-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R4000-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R4000-seed44/tests/_cb/contextbench/runner.py b/harbor/kv-store-R4000-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R4000-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R4000-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R4000-seed44/tests/_cb/contextbench/stream.py b/harbor/kv-store-R4000-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R4000-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R4000-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R4000-seed45/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R4000-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R4000-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R4000-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R4000-seed45/tests/_cb/contextbench/runner.py b/harbor/kv-store-R4000-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R4000-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R4000-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R4000-seed45/tests/_cb/contextbench/stream.py b/harbor/kv-store-R4000-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R4000-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R4000-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R4000-seed46/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R4000-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R4000-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R4000-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R4000-seed46/tests/_cb/contextbench/runner.py b/harbor/kv-store-R4000-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R4000-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R4000-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R4000-seed46/tests/_cb/contextbench/stream.py b/harbor/kv-store-R4000-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R4000-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R4000-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R4000-seed47/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R4000-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R4000-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R4000-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R4000-seed47/tests/_cb/contextbench/runner.py b/harbor/kv-store-R4000-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R4000-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R4000-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R4000-seed47/tests/_cb/contextbench/stream.py b/harbor/kv-store-R4000-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R4000-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R4000-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R4000-seed48/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R4000-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R4000-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R4000-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R4000-seed48/tests/_cb/contextbench/runner.py b/harbor/kv-store-R4000-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R4000-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R4000-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R4000-seed48/tests/_cb/contextbench/stream.py b/harbor/kv-store-R4000-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R4000-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R4000-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R4000-seed49/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R4000-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R4000-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R4000-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R4000-seed49/tests/_cb/contextbench/runner.py b/harbor/kv-store-R4000-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R4000-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R4000-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R4000-seed49/tests/_cb/contextbench/stream.py b/harbor/kv-store-R4000-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R4000-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R4000-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R520-seed42/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R520-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R520-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R520-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R520-seed42/tests/_cb/contextbench/runner.py b/harbor/kv-store-R520-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R520-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R520-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R520-seed42/tests/_cb/contextbench/stream.py b/harbor/kv-store-R520-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R520-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R520-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R520-seed43/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R520-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R520-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R520-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R520-seed43/tests/_cb/contextbench/runner.py b/harbor/kv-store-R520-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R520-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R520-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R520-seed43/tests/_cb/contextbench/stream.py b/harbor/kv-store-R520-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R520-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R520-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R520-seed44/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R520-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R520-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R520-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R520-seed44/tests/_cb/contextbench/runner.py b/harbor/kv-store-R520-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R520-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R520-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R520-seed44/tests/_cb/contextbench/stream.py b/harbor/kv-store-R520-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R520-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R520-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R520-seed45/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R520-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R520-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R520-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R520-seed45/tests/_cb/contextbench/runner.py b/harbor/kv-store-R520-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R520-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R520-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R520-seed45/tests/_cb/contextbench/stream.py b/harbor/kv-store-R520-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R520-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R520-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R520-seed46/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R520-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R520-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R520-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R520-seed46/tests/_cb/contextbench/runner.py b/harbor/kv-store-R520-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R520-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R520-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R520-seed46/tests/_cb/contextbench/stream.py b/harbor/kv-store-R520-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R520-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R520-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R520-seed47/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R520-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R520-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R520-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R520-seed47/tests/_cb/contextbench/runner.py b/harbor/kv-store-R520-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R520-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R520-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R520-seed47/tests/_cb/contextbench/stream.py b/harbor/kv-store-R520-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R520-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R520-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R520-seed48/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R520-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R520-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R520-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R520-seed48/tests/_cb/contextbench/runner.py b/harbor/kv-store-R520-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R520-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R520-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R520-seed48/tests/_cb/contextbench/stream.py b/harbor/kv-store-R520-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R520-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R520-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R520-seed49/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R520-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R520-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R520-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R520-seed49/tests/_cb/contextbench/runner.py b/harbor/kv-store-R520-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R520-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R520-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R520-seed49/tests/_cb/contextbench/stream.py b/harbor/kv-store-R520-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R520-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R520-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R8000-seed42/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R8000-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R8000-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R8000-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R8000-seed42/tests/_cb/contextbench/runner.py b/harbor/kv-store-R8000-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R8000-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R8000-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R8000-seed42/tests/_cb/contextbench/stream.py b/harbor/kv-store-R8000-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R8000-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R8000-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R8000-seed43/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R8000-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R8000-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R8000-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R8000-seed43/tests/_cb/contextbench/runner.py b/harbor/kv-store-R8000-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R8000-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R8000-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R8000-seed43/tests/_cb/contextbench/stream.py b/harbor/kv-store-R8000-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R8000-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R8000-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R8000-seed44/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R8000-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R8000-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R8000-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R8000-seed44/tests/_cb/contextbench/runner.py b/harbor/kv-store-R8000-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R8000-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R8000-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R8000-seed44/tests/_cb/contextbench/stream.py b/harbor/kv-store-R8000-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R8000-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R8000-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R8000-seed45/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R8000-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R8000-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R8000-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R8000-seed45/tests/_cb/contextbench/runner.py b/harbor/kv-store-R8000-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R8000-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R8000-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R8000-seed45/tests/_cb/contextbench/stream.py b/harbor/kv-store-R8000-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R8000-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R8000-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R8000-seed46/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R8000-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R8000-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R8000-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R8000-seed46/tests/_cb/contextbench/runner.py b/harbor/kv-store-R8000-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R8000-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R8000-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R8000-seed46/tests/_cb/contextbench/stream.py b/harbor/kv-store-R8000-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R8000-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R8000-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R8000-seed47/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R8000-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R8000-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R8000-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R8000-seed47/tests/_cb/contextbench/runner.py b/harbor/kv-store-R8000-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R8000-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R8000-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R8000-seed47/tests/_cb/contextbench/stream.py b/harbor/kv-store-R8000-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R8000-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R8000-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R8000-seed48/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R8000-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R8000-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R8000-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R8000-seed48/tests/_cb/contextbench/runner.py b/harbor/kv-store-R8000-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R8000-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R8000-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R8000-seed48/tests/_cb/contextbench/stream.py b/harbor/kv-store-R8000-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R8000-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R8000-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/kv-store-R8000-seed49/tests/_cb/contextbench/graders/log.py b/harbor/kv-store-R8000-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/kv-store-R8000-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/kv-store-R8000-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/kv-store-R8000-seed49/tests/_cb/contextbench/runner.py b/harbor/kv-store-R8000-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/kv-store-R8000-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/kv-store-R8000-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/kv-store-R8000-seed49/tests/_cb/contextbench/stream.py b/harbor/kv-store-R8000-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/kv-store-R8000-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/kv-store-R8000-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L12800-seed42/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L12800-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L12800-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L12800-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L12800-seed42/tests/_cb/contextbench/runner.py b/harbor/log-triage-L12800-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L12800-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L12800-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L12800-seed42/tests/_cb/contextbench/stream.py b/harbor/log-triage-L12800-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L12800-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L12800-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L12800-seed43/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L12800-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L12800-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L12800-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L12800-seed43/tests/_cb/contextbench/runner.py b/harbor/log-triage-L12800-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L12800-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L12800-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L12800-seed43/tests/_cb/contextbench/stream.py b/harbor/log-triage-L12800-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L12800-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L12800-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L12800-seed44/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L12800-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L12800-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L12800-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L12800-seed44/tests/_cb/contextbench/runner.py b/harbor/log-triage-L12800-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L12800-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L12800-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L12800-seed44/tests/_cb/contextbench/stream.py b/harbor/log-triage-L12800-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L12800-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L12800-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L12800-seed45/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L12800-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L12800-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L12800-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L12800-seed45/tests/_cb/contextbench/runner.py b/harbor/log-triage-L12800-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L12800-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L12800-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L12800-seed45/tests/_cb/contextbench/stream.py b/harbor/log-triage-L12800-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L12800-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L12800-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L12800-seed46/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L12800-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L12800-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L12800-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L12800-seed46/tests/_cb/contextbench/runner.py b/harbor/log-triage-L12800-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L12800-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L12800-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L12800-seed46/tests/_cb/contextbench/stream.py b/harbor/log-triage-L12800-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L12800-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L12800-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L12800-seed47/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L12800-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L12800-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L12800-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L12800-seed47/tests/_cb/contextbench/runner.py b/harbor/log-triage-L12800-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L12800-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L12800-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L12800-seed47/tests/_cb/contextbench/stream.py b/harbor/log-triage-L12800-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L12800-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L12800-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L12800-seed48/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L12800-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L12800-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L12800-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L12800-seed48/tests/_cb/contextbench/runner.py b/harbor/log-triage-L12800-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L12800-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L12800-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L12800-seed48/tests/_cb/contextbench/stream.py b/harbor/log-triage-L12800-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L12800-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L12800-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L12800-seed49/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L12800-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L12800-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L12800-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L12800-seed49/tests/_cb/contextbench/runner.py b/harbor/log-triage-L12800-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L12800-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L12800-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L12800-seed49/tests/_cb/contextbench/stream.py b/harbor/log-triage-L12800-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L12800-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L12800-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L160-seed42/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L160-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L160-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L160-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L160-seed42/tests/_cb/contextbench/runner.py b/harbor/log-triage-L160-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L160-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L160-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L160-seed42/tests/_cb/contextbench/stream.py b/harbor/log-triage-L160-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L160-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L160-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L160-seed43/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L160-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L160-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L160-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L160-seed43/tests/_cb/contextbench/runner.py b/harbor/log-triage-L160-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L160-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L160-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L160-seed43/tests/_cb/contextbench/stream.py b/harbor/log-triage-L160-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L160-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L160-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L160-seed44/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L160-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L160-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L160-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L160-seed44/tests/_cb/contextbench/runner.py b/harbor/log-triage-L160-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L160-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L160-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L160-seed44/tests/_cb/contextbench/stream.py b/harbor/log-triage-L160-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L160-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L160-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L160-seed45/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L160-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L160-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L160-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L160-seed45/tests/_cb/contextbench/runner.py b/harbor/log-triage-L160-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L160-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L160-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L160-seed45/tests/_cb/contextbench/stream.py b/harbor/log-triage-L160-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L160-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L160-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L160-seed46/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L160-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L160-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L160-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L160-seed46/tests/_cb/contextbench/runner.py b/harbor/log-triage-L160-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L160-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L160-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L160-seed46/tests/_cb/contextbench/stream.py b/harbor/log-triage-L160-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L160-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L160-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L160-seed47/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L160-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L160-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L160-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L160-seed47/tests/_cb/contextbench/runner.py b/harbor/log-triage-L160-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L160-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L160-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L160-seed47/tests/_cb/contextbench/stream.py b/harbor/log-triage-L160-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L160-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L160-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L160-seed48/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L160-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L160-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L160-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L160-seed48/tests/_cb/contextbench/runner.py b/harbor/log-triage-L160-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L160-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L160-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L160-seed48/tests/_cb/contextbench/stream.py b/harbor/log-triage-L160-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L160-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L160-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L160-seed49/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L160-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L160-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L160-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L160-seed49/tests/_cb/contextbench/runner.py b/harbor/log-triage-L160-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L160-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L160-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L160-seed49/tests/_cb/contextbench/stream.py b/harbor/log-triage-L160-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L160-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L160-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L1600-seed42/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L1600-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L1600-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L1600-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L1600-seed42/tests/_cb/contextbench/runner.py b/harbor/log-triage-L1600-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L1600-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L1600-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L1600-seed42/tests/_cb/contextbench/stream.py b/harbor/log-triage-L1600-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L1600-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L1600-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L1600-seed43/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L1600-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L1600-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L1600-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L1600-seed43/tests/_cb/contextbench/runner.py b/harbor/log-triage-L1600-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L1600-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L1600-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L1600-seed43/tests/_cb/contextbench/stream.py b/harbor/log-triage-L1600-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L1600-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L1600-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L1600-seed44/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L1600-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L1600-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L1600-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L1600-seed44/tests/_cb/contextbench/runner.py b/harbor/log-triage-L1600-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L1600-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L1600-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L1600-seed44/tests/_cb/contextbench/stream.py b/harbor/log-triage-L1600-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L1600-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L1600-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L1600-seed45/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L1600-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L1600-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L1600-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L1600-seed45/tests/_cb/contextbench/runner.py b/harbor/log-triage-L1600-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L1600-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L1600-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L1600-seed45/tests/_cb/contextbench/stream.py b/harbor/log-triage-L1600-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L1600-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L1600-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L1600-seed46/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L1600-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L1600-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L1600-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L1600-seed46/tests/_cb/contextbench/runner.py b/harbor/log-triage-L1600-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L1600-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L1600-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L1600-seed46/tests/_cb/contextbench/stream.py b/harbor/log-triage-L1600-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L1600-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L1600-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L1600-seed47/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L1600-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L1600-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L1600-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L1600-seed47/tests/_cb/contextbench/runner.py b/harbor/log-triage-L1600-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L1600-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L1600-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L1600-seed47/tests/_cb/contextbench/stream.py b/harbor/log-triage-L1600-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L1600-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L1600-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L1600-seed48/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L1600-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L1600-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L1600-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L1600-seed48/tests/_cb/contextbench/runner.py b/harbor/log-triage-L1600-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L1600-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L1600-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L1600-seed48/tests/_cb/contextbench/stream.py b/harbor/log-triage-L1600-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L1600-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L1600-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L1600-seed49/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L1600-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L1600-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L1600-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L1600-seed49/tests/_cb/contextbench/runner.py b/harbor/log-triage-L1600-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L1600-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L1600-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L1600-seed49/tests/_cb/contextbench/stream.py b/harbor/log-triage-L1600-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L1600-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L1600-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L320-seed42/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L320-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L320-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L320-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L320-seed42/tests/_cb/contextbench/runner.py b/harbor/log-triage-L320-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L320-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L320-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L320-seed42/tests/_cb/contextbench/stream.py b/harbor/log-triage-L320-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L320-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L320-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L320-seed43/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L320-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L320-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L320-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L320-seed43/tests/_cb/contextbench/runner.py b/harbor/log-triage-L320-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L320-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L320-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L320-seed43/tests/_cb/contextbench/stream.py b/harbor/log-triage-L320-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L320-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L320-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L320-seed44/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L320-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L320-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L320-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L320-seed44/tests/_cb/contextbench/runner.py b/harbor/log-triage-L320-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L320-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L320-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L320-seed44/tests/_cb/contextbench/stream.py b/harbor/log-triage-L320-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L320-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L320-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L320-seed45/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L320-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L320-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L320-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L320-seed45/tests/_cb/contextbench/runner.py b/harbor/log-triage-L320-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L320-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L320-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L320-seed45/tests/_cb/contextbench/stream.py b/harbor/log-triage-L320-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L320-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L320-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L320-seed46/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L320-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L320-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L320-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L320-seed46/tests/_cb/contextbench/runner.py b/harbor/log-triage-L320-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L320-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L320-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L320-seed46/tests/_cb/contextbench/stream.py b/harbor/log-triage-L320-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L320-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L320-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L320-seed47/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L320-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L320-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L320-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L320-seed47/tests/_cb/contextbench/runner.py b/harbor/log-triage-L320-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L320-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L320-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L320-seed47/tests/_cb/contextbench/stream.py b/harbor/log-triage-L320-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L320-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L320-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L320-seed48/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L320-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L320-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L320-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L320-seed48/tests/_cb/contextbench/runner.py b/harbor/log-triage-L320-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L320-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L320-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L320-seed48/tests/_cb/contextbench/stream.py b/harbor/log-triage-L320-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L320-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L320-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L320-seed49/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L320-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L320-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L320-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L320-seed49/tests/_cb/contextbench/runner.py b/harbor/log-triage-L320-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L320-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L320-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L320-seed49/tests/_cb/contextbench/stream.py b/harbor/log-triage-L320-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L320-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L320-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L3200-seed42/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L3200-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L3200-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L3200-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L3200-seed42/tests/_cb/contextbench/runner.py b/harbor/log-triage-L3200-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L3200-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L3200-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L3200-seed42/tests/_cb/contextbench/stream.py b/harbor/log-triage-L3200-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L3200-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L3200-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L3200-seed43/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L3200-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L3200-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L3200-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L3200-seed43/tests/_cb/contextbench/runner.py b/harbor/log-triage-L3200-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L3200-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L3200-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L3200-seed43/tests/_cb/contextbench/stream.py b/harbor/log-triage-L3200-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L3200-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L3200-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L3200-seed44/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L3200-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L3200-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L3200-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L3200-seed44/tests/_cb/contextbench/runner.py b/harbor/log-triage-L3200-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L3200-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L3200-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L3200-seed44/tests/_cb/contextbench/stream.py b/harbor/log-triage-L3200-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L3200-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L3200-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L3200-seed45/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L3200-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L3200-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L3200-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L3200-seed45/tests/_cb/contextbench/runner.py b/harbor/log-triage-L3200-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L3200-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L3200-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L3200-seed45/tests/_cb/contextbench/stream.py b/harbor/log-triage-L3200-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L3200-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L3200-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L3200-seed46/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L3200-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L3200-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L3200-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L3200-seed46/tests/_cb/contextbench/runner.py b/harbor/log-triage-L3200-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L3200-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L3200-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L3200-seed46/tests/_cb/contextbench/stream.py b/harbor/log-triage-L3200-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L3200-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L3200-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L3200-seed47/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L3200-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L3200-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L3200-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L3200-seed47/tests/_cb/contextbench/runner.py b/harbor/log-triage-L3200-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L3200-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L3200-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L3200-seed47/tests/_cb/contextbench/stream.py b/harbor/log-triage-L3200-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L3200-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L3200-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L3200-seed48/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L3200-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L3200-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L3200-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L3200-seed48/tests/_cb/contextbench/runner.py b/harbor/log-triage-L3200-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L3200-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L3200-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L3200-seed48/tests/_cb/contextbench/stream.py b/harbor/log-triage-L3200-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L3200-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L3200-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L3200-seed49/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L3200-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L3200-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L3200-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L3200-seed49/tests/_cb/contextbench/runner.py b/harbor/log-triage-L3200-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L3200-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L3200-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L3200-seed49/tests/_cb/contextbench/stream.py b/harbor/log-triage-L3200-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L3200-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L3200-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L640-seed42/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L640-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L640-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L640-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L640-seed42/tests/_cb/contextbench/runner.py b/harbor/log-triage-L640-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L640-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L640-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L640-seed42/tests/_cb/contextbench/stream.py b/harbor/log-triage-L640-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L640-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L640-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L640-seed43/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L640-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L640-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L640-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L640-seed43/tests/_cb/contextbench/runner.py b/harbor/log-triage-L640-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L640-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L640-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L640-seed43/tests/_cb/contextbench/stream.py b/harbor/log-triage-L640-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L640-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L640-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L640-seed44/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L640-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L640-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L640-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L640-seed44/tests/_cb/contextbench/runner.py b/harbor/log-triage-L640-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L640-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L640-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L640-seed44/tests/_cb/contextbench/stream.py b/harbor/log-triage-L640-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L640-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L640-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L640-seed45/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L640-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L640-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L640-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L640-seed45/tests/_cb/contextbench/runner.py b/harbor/log-triage-L640-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L640-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L640-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L640-seed45/tests/_cb/contextbench/stream.py b/harbor/log-triage-L640-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L640-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L640-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L640-seed46/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L640-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L640-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L640-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L640-seed46/tests/_cb/contextbench/runner.py b/harbor/log-triage-L640-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L640-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L640-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L640-seed46/tests/_cb/contextbench/stream.py b/harbor/log-triage-L640-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L640-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L640-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L640-seed47/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L640-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L640-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L640-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L640-seed47/tests/_cb/contextbench/runner.py b/harbor/log-triage-L640-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L640-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L640-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L640-seed47/tests/_cb/contextbench/stream.py b/harbor/log-triage-L640-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L640-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L640-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L640-seed48/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L640-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L640-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L640-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L640-seed48/tests/_cb/contextbench/runner.py b/harbor/log-triage-L640-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L640-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L640-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L640-seed48/tests/_cb/contextbench/stream.py b/harbor/log-triage-L640-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L640-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L640-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L640-seed49/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L640-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L640-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L640-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L640-seed49/tests/_cb/contextbench/runner.py b/harbor/log-triage-L640-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L640-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L640-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L640-seed49/tests/_cb/contextbench/stream.py b/harbor/log-triage-L640-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L640-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L640-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L6400-seed42/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L6400-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L6400-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L6400-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L6400-seed42/tests/_cb/contextbench/runner.py b/harbor/log-triage-L6400-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L6400-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L6400-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L6400-seed42/tests/_cb/contextbench/stream.py b/harbor/log-triage-L6400-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L6400-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L6400-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L6400-seed43/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L6400-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L6400-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L6400-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L6400-seed43/tests/_cb/contextbench/runner.py b/harbor/log-triage-L6400-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L6400-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L6400-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L6400-seed43/tests/_cb/contextbench/stream.py b/harbor/log-triage-L6400-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L6400-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L6400-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L6400-seed44/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L6400-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L6400-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L6400-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L6400-seed44/tests/_cb/contextbench/runner.py b/harbor/log-triage-L6400-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L6400-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L6400-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L6400-seed44/tests/_cb/contextbench/stream.py b/harbor/log-triage-L6400-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L6400-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L6400-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L6400-seed45/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L6400-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L6400-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L6400-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L6400-seed45/tests/_cb/contextbench/runner.py b/harbor/log-triage-L6400-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L6400-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L6400-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L6400-seed45/tests/_cb/contextbench/stream.py b/harbor/log-triage-L6400-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L6400-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L6400-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L6400-seed46/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L6400-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L6400-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L6400-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L6400-seed46/tests/_cb/contextbench/runner.py b/harbor/log-triage-L6400-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L6400-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L6400-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L6400-seed46/tests/_cb/contextbench/stream.py b/harbor/log-triage-L6400-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L6400-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L6400-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L6400-seed47/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L6400-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L6400-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L6400-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L6400-seed47/tests/_cb/contextbench/runner.py b/harbor/log-triage-L6400-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L6400-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L6400-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L6400-seed47/tests/_cb/contextbench/stream.py b/harbor/log-triage-L6400-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L6400-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L6400-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L6400-seed48/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L6400-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L6400-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L6400-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L6400-seed48/tests/_cb/contextbench/runner.py b/harbor/log-triage-L6400-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L6400-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L6400-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L6400-seed48/tests/_cb/contextbench/stream.py b/harbor/log-triage-L6400-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L6400-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L6400-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/log-triage-L6400-seed49/tests/_cb/contextbench/graders/log.py b/harbor/log-triage-L6400-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/log-triage-L6400-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/log-triage-L6400-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/log-triage-L6400-seed49/tests/_cb/contextbench/runner.py b/harbor/log-triage-L6400-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/log-triage-L6400-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/log-triage-L6400-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/log-triage-L6400-seed49/tests/_cb/contextbench/stream.py b/harbor/log-triage-L6400-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/log-triage-L6400-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/log-triage-L6400-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K2-C192-seed42/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K2-C192-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K2-C192-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K2-C192-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K2-C192-seed42/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K2-C192-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K2-C192-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K2-C192-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K2-C192-seed42/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K2-C192-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K2-C192-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K2-C192-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K2-C192-seed43/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K2-C192-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K2-C192-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K2-C192-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K2-C192-seed43/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K2-C192-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K2-C192-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K2-C192-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K2-C192-seed43/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K2-C192-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K2-C192-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K2-C192-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K2-C192-seed44/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K2-C192-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K2-C192-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K2-C192-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K2-C192-seed44/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K2-C192-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K2-C192-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K2-C192-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K2-C192-seed44/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K2-C192-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K2-C192-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K2-C192-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K2-C192-seed45/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K2-C192-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K2-C192-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K2-C192-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K2-C192-seed45/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K2-C192-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K2-C192-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K2-C192-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K2-C192-seed45/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K2-C192-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K2-C192-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K2-C192-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K2-C192-seed46/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K2-C192-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K2-C192-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K2-C192-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K2-C192-seed46/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K2-C192-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K2-C192-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K2-C192-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K2-C192-seed46/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K2-C192-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K2-C192-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K2-C192-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K2-C192-seed47/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K2-C192-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K2-C192-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K2-C192-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K2-C192-seed47/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K2-C192-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K2-C192-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K2-C192-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K2-C192-seed47/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K2-C192-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K2-C192-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K2-C192-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K2-C192-seed48/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K2-C192-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K2-C192-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K2-C192-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K2-C192-seed48/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K2-C192-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K2-C192-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K2-C192-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K2-C192-seed48/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K2-C192-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K2-C192-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K2-C192-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K2-C192-seed49/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K2-C192-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K2-C192-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K2-C192-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K2-C192-seed49/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K2-C192-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K2-C192-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K2-C192-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K2-C192-seed49/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K2-C192-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K2-C192-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K2-C192-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K2-C224-seed42/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K2-C224-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K2-C224-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K2-C224-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K2-C224-seed42/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K2-C224-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K2-C224-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K2-C224-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K2-C224-seed42/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K2-C224-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K2-C224-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K2-C224-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K2-C224-seed43/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K2-C224-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K2-C224-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K2-C224-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K2-C224-seed43/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K2-C224-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K2-C224-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K2-C224-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K2-C224-seed43/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K2-C224-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K2-C224-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K2-C224-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K2-C224-seed44/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K2-C224-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K2-C224-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K2-C224-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K2-C224-seed44/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K2-C224-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K2-C224-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K2-C224-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K2-C224-seed44/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K2-C224-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K2-C224-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K2-C224-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K2-C224-seed45/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K2-C224-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K2-C224-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K2-C224-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K2-C224-seed45/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K2-C224-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K2-C224-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K2-C224-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K2-C224-seed45/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K2-C224-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K2-C224-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K2-C224-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K2-C224-seed46/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K2-C224-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K2-C224-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K2-C224-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K2-C224-seed46/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K2-C224-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K2-C224-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K2-C224-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K2-C224-seed46/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K2-C224-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K2-C224-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K2-C224-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K2-C224-seed47/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K2-C224-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K2-C224-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K2-C224-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K2-C224-seed47/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K2-C224-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K2-C224-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K2-C224-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K2-C224-seed47/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K2-C224-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K2-C224-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K2-C224-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K2-C224-seed48/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K2-C224-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K2-C224-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K2-C224-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K2-C224-seed48/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K2-C224-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K2-C224-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K2-C224-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K2-C224-seed48/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K2-C224-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K2-C224-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K2-C224-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K2-C224-seed49/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K2-C224-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K2-C224-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K2-C224-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K2-C224-seed49/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K2-C224-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K2-C224-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K2-C224-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K2-C224-seed49/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K2-C224-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K2-C224-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K2-C224-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C24-seed42/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C24-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C24-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C24-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C24-seed42/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C24-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C24-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C24-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C24-seed42/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C24-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C24-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C24-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C24-seed43/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C24-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C24-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C24-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C24-seed43/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C24-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C24-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C24-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C24-seed43/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C24-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C24-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C24-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C24-seed44/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C24-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C24-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C24-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C24-seed44/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C24-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C24-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C24-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C24-seed44/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C24-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C24-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C24-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C24-seed45/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C24-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C24-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C24-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C24-seed45/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C24-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C24-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C24-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C24-seed45/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C24-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C24-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C24-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C24-seed46/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C24-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C24-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C24-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C24-seed46/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C24-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C24-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C24-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C24-seed46/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C24-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C24-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C24-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C24-seed47/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C24-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C24-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C24-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C24-seed47/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C24-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C24-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C24-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C24-seed47/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C24-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C24-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C24-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C24-seed48/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C24-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C24-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C24-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C24-seed48/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C24-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C24-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C24-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C24-seed48/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C24-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C24-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C24-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C24-seed49/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C24-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C24-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C24-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C24-seed49/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C24-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C24-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C24-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C24-seed49/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C24-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C24-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C24-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C4-seed42/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C4-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C4-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C4-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C4-seed42/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C4-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C4-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C4-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C4-seed42/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C4-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C4-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C4-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C4-seed43/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C4-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C4-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C4-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C4-seed43/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C4-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C4-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C4-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C4-seed43/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C4-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C4-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C4-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C4-seed44/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C4-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C4-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C4-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C4-seed44/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C4-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C4-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C4-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C4-seed44/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C4-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C4-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C4-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C4-seed45/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C4-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C4-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C4-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C4-seed45/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C4-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C4-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C4-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C4-seed45/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C4-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C4-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C4-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C4-seed46/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C4-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C4-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C4-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C4-seed46/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C4-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C4-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C4-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C4-seed46/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C4-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C4-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C4-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C4-seed47/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C4-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C4-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C4-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C4-seed47/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C4-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C4-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C4-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C4-seed47/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C4-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C4-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C4-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C4-seed48/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C4-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C4-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C4-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C4-seed48/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C4-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C4-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C4-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C4-seed48/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C4-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C4-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C4-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C4-seed49/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C4-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C4-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C4-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C4-seed49/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C4-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C4-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C4-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C4-seed49/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C4-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C4-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C4-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C6-seed42/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C6-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C6-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C6-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C6-seed42/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C6-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C6-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C6-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C6-seed42/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C6-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C6-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C6-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C6-seed43/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C6-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C6-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C6-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C6-seed43/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C6-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C6-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C6-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C6-seed43/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C6-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C6-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C6-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C6-seed44/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C6-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C6-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C6-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C6-seed44/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C6-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C6-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C6-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C6-seed44/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C6-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C6-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C6-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C6-seed45/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C6-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C6-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C6-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C6-seed45/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C6-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C6-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C6-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C6-seed45/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C6-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C6-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C6-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C6-seed46/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C6-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C6-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C6-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C6-seed46/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C6-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C6-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C6-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C6-seed46/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C6-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C6-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C6-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C6-seed47/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C6-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C6-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C6-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C6-seed47/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C6-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C6-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C6-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C6-seed47/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C6-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C6-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C6-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C6-seed48/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C6-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C6-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C6-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C6-seed48/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C6-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C6-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C6-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C6-seed48/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C6-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C6-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C6-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C6-seed49/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C6-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C6-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C6-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C6-seed49/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C6-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C6-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C6-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C6-seed49/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C6-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C6-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C6-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C8-seed42/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C8-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C8-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C8-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C8-seed42/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C8-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C8-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C8-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C8-seed42/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C8-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C8-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C8-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C8-seed43/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C8-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C8-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C8-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C8-seed43/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C8-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C8-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C8-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C8-seed43/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C8-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C8-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C8-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C8-seed44/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C8-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C8-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C8-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C8-seed44/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C8-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C8-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C8-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C8-seed44/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C8-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C8-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C8-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C8-seed45/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C8-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C8-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C8-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C8-seed45/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C8-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C8-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C8-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C8-seed45/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C8-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C8-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C8-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C8-seed46/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C8-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C8-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C8-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C8-seed46/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C8-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C8-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C8-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C8-seed46/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C8-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C8-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C8-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C8-seed47/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C8-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C8-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C8-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C8-seed47/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C8-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C8-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C8-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C8-seed47/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C8-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C8-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C8-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C8-seed48/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C8-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C8-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C8-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C8-seed48/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C8-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C8-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C8-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C8-seed48/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C8-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C8-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C8-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C8-seed49/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C8-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C8-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C8-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C8-seed49/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C8-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C8-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C8-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C8-seed49/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C8-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C8-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C8-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C96-seed42/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C96-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C96-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C96-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C96-seed42/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C96-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C96-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C96-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C96-seed42/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C96-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C96-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C96-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C96-seed43/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C96-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C96-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C96-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C96-seed43/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C96-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C96-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C96-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C96-seed43/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C96-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C96-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C96-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C96-seed44/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C96-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C96-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C96-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C96-seed44/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C96-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C96-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C96-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C96-seed44/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C96-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C96-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C96-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C96-seed45/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C96-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C96-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C96-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C96-seed45/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C96-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C96-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C96-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C96-seed45/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C96-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C96-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C96-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C96-seed46/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C96-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C96-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C96-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C96-seed46/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C96-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C96-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C96-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C96-seed46/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C96-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C96-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C96-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C96-seed47/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C96-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C96-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C96-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C96-seed47/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C96-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C96-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C96-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C96-seed47/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C96-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C96-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C96-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C96-seed48/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C96-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C96-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C96-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C96-seed48/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C96-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C96-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C96-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C96-seed48/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C96-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C96-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C96-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K4-C96-seed49/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K4-C96-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K4-C96-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K4-C96-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K4-C96-seed49/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K4-C96-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K4-C96-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K4-C96-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K4-C96-seed49/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K4-C96-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K4-C96-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K4-C96-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K8-C48-seed42/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K8-C48-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K8-C48-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K8-C48-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K8-C48-seed42/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K8-C48-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K8-C48-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K8-C48-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K8-C48-seed42/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K8-C48-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K8-C48-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K8-C48-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K8-C48-seed43/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K8-C48-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K8-C48-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K8-C48-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K8-C48-seed43/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K8-C48-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K8-C48-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K8-C48-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K8-C48-seed43/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K8-C48-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K8-C48-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K8-C48-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K8-C48-seed44/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K8-C48-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K8-C48-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K8-C48-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K8-C48-seed44/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K8-C48-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K8-C48-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K8-C48-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K8-C48-seed44/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K8-C48-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K8-C48-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K8-C48-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K8-C48-seed45/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K8-C48-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K8-C48-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K8-C48-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K8-C48-seed45/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K8-C48-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K8-C48-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K8-C48-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K8-C48-seed45/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K8-C48-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K8-C48-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K8-C48-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K8-C48-seed46/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K8-C48-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K8-C48-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K8-C48-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K8-C48-seed46/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K8-C48-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K8-C48-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K8-C48-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K8-C48-seed46/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K8-C48-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K8-C48-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K8-C48-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K8-C48-seed47/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K8-C48-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K8-C48-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K8-C48-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K8-C48-seed47/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K8-C48-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K8-C48-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K8-C48-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K8-C48-seed47/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K8-C48-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K8-C48-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K8-C48-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K8-C48-seed48/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K8-C48-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K8-C48-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K8-C48-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K8-C48-seed48/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K8-C48-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K8-C48-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K8-C48-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K8-C48-seed48/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K8-C48-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K8-C48-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K8-C48-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K8-C48-seed49/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K8-C48-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K8-C48-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K8-C48-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K8-C48-seed49/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K8-C48-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K8-C48-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K8-C48-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K8-C48-seed49/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K8-C48-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K8-C48-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K8-C48-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K8-C60-seed42/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K8-C60-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K8-C60-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K8-C60-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K8-C60-seed42/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K8-C60-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K8-C60-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K8-C60-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K8-C60-seed42/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K8-C60-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K8-C60-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K8-C60-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K8-C60-seed43/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K8-C60-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K8-C60-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K8-C60-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K8-C60-seed43/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K8-C60-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K8-C60-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K8-C60-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K8-C60-seed43/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K8-C60-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K8-C60-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K8-C60-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K8-C60-seed44/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K8-C60-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K8-C60-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K8-C60-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K8-C60-seed44/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K8-C60-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K8-C60-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K8-C60-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K8-C60-seed44/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K8-C60-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K8-C60-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K8-C60-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K8-C60-seed45/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K8-C60-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K8-C60-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K8-C60-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K8-C60-seed45/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K8-C60-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K8-C60-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K8-C60-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K8-C60-seed45/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K8-C60-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K8-C60-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K8-C60-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K8-C60-seed46/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K8-C60-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K8-C60-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K8-C60-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K8-C60-seed46/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K8-C60-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K8-C60-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K8-C60-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K8-C60-seed46/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K8-C60-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K8-C60-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K8-C60-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K8-C60-seed47/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K8-C60-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K8-C60-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K8-C60-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K8-C60-seed47/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K8-C60-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K8-C60-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K8-C60-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K8-C60-seed47/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K8-C60-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K8-C60-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K8-C60-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K8-C60-seed48/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K8-C60-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K8-C60-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K8-C60-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K8-C60-seed48/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K8-C60-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K8-C60-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K8-C60-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K8-C60-seed48/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K8-C60-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K8-C60-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K8-C60-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K8-C60-seed49/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K8-C60-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K8-C60-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K8-C60-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K8-C60-seed49/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K8-C60-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K8-C60-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K8-C60-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K8-C60-seed49/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K8-C60-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K8-C60-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K8-C60-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K8-C62-seed42/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K8-C62-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K8-C62-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K8-C62-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K8-C62-seed42/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K8-C62-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K8-C62-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K8-C62-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K8-C62-seed42/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K8-C62-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K8-C62-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K8-C62-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K8-C62-seed43/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K8-C62-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K8-C62-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K8-C62-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K8-C62-seed43/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K8-C62-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K8-C62-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K8-C62-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K8-C62-seed43/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K8-C62-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K8-C62-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K8-C62-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K8-C62-seed44/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K8-C62-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K8-C62-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K8-C62-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K8-C62-seed44/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K8-C62-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K8-C62-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K8-C62-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K8-C62-seed44/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K8-C62-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K8-C62-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K8-C62-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K8-C62-seed45/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K8-C62-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K8-C62-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K8-C62-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K8-C62-seed45/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K8-C62-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K8-C62-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K8-C62-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K8-C62-seed45/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K8-C62-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K8-C62-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K8-C62-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K8-C62-seed46/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K8-C62-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K8-C62-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K8-C62-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K8-C62-seed46/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K8-C62-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K8-C62-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K8-C62-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K8-C62-seed46/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K8-C62-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K8-C62-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K8-C62-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K8-C62-seed47/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K8-C62-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K8-C62-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K8-C62-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K8-C62-seed47/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K8-C62-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K8-C62-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K8-C62-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K8-C62-seed47/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K8-C62-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K8-C62-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K8-C62-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K8-C62-seed48/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K8-C62-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K8-C62-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K8-C62-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K8-C62-seed48/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K8-C62-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K8-C62-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K8-C62-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K8-C62-seed48/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K8-C62-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K8-C62-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K8-C62-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/needle-retention-K8-C62-seed49/tests/_cb/contextbench/graders/log.py b/harbor/needle-retention-K8-C62-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/needle-retention-K8-C62-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/needle-retention-K8-C62-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/needle-retention-K8-C62-seed49/tests/_cb/contextbench/runner.py b/harbor/needle-retention-K8-C62-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/needle-retention-K8-C62-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/needle-retention-K8-C62-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/needle-retention-K8-C62-seed49/tests/_cb/contextbench/stream.py b/harbor/needle-retention-K8-C62-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/needle-retention-K8-C62-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/needle-retention-K8-C62-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t100-seed42/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t100-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t100-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t100-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t100-seed42/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t100-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t100-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t100-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t100-seed42/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t100-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t100-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t100-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t100-seed43/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t100-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t100-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t100-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t100-seed43/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t100-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t100-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t100-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t100-seed43/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t100-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t100-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t100-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t100-seed44/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t100-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t100-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t100-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t100-seed44/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t100-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t100-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t100-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t100-seed44/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t100-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t100-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t100-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t100-seed45/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t100-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t100-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t100-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t100-seed45/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t100-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t100-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t100-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t100-seed45/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t100-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t100-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t100-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t100-seed46/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t100-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t100-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t100-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t100-seed46/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t100-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t100-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t100-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t100-seed46/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t100-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t100-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t100-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t100-seed47/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t100-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t100-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t100-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t100-seed47/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t100-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t100-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t100-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t100-seed47/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t100-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t100-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t100-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t100-seed48/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t100-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t100-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t100-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t100-seed48/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t100-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t100-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t100-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t100-seed48/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t100-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t100-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t100-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t100-seed49/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t100-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t100-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t100-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t100-seed49/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t100-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t100-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t100-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t100-seed49/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t100-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t100-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t100-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t15-seed42/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t15-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t15-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t15-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t15-seed42/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t15-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t15-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t15-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t15-seed42/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t15-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t15-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t15-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t15-seed43/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t15-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t15-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t15-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t15-seed43/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t15-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t15-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t15-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t15-seed43/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t15-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t15-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t15-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t15-seed44/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t15-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t15-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t15-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t15-seed44/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t15-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t15-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t15-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t15-seed44/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t15-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t15-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t15-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t15-seed45/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t15-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t15-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t15-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t15-seed45/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t15-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t15-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t15-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t15-seed45/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t15-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t15-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t15-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t15-seed46/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t15-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t15-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t15-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t15-seed46/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t15-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t15-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t15-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t15-seed46/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t15-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t15-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t15-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t15-seed47/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t15-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t15-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t15-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t15-seed47/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t15-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t15-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t15-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t15-seed47/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t15-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t15-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t15-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t15-seed48/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t15-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t15-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t15-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t15-seed48/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t15-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t15-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t15-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t15-seed48/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t15-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t15-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t15-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t15-seed49/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t15-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t15-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t15-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t15-seed49/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t15-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t15-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t15-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t15-seed49/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t15-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t15-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t15-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t150-seed42/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t150-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t150-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t150-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t150-seed42/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t150-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t150-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t150-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t150-seed42/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t150-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t150-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t150-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t150-seed43/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t150-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t150-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t150-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t150-seed43/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t150-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t150-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t150-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t150-seed43/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t150-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t150-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t150-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t150-seed44/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t150-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t150-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t150-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t150-seed44/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t150-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t150-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t150-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t150-seed44/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t150-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t150-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t150-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t150-seed45/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t150-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t150-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t150-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t150-seed45/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t150-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t150-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t150-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t150-seed45/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t150-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t150-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t150-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t150-seed46/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t150-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t150-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t150-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t150-seed46/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t150-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t150-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t150-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t150-seed46/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t150-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t150-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t150-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t150-seed47/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t150-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t150-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t150-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t150-seed47/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t150-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t150-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t150-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t150-seed47/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t150-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t150-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t150-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t150-seed48/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t150-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t150-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t150-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t150-seed48/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t150-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t150-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t150-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t150-seed48/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t150-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t150-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t150-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t150-seed49/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t150-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t150-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t150-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t150-seed49/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t150-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t150-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t150-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t150-seed49/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t150-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t150-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t150-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t30-seed42/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t30-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t30-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t30-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t30-seed42/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t30-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t30-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t30-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t30-seed42/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t30-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t30-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t30-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t30-seed43/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t30-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t30-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t30-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t30-seed43/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t30-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t30-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t30-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t30-seed43/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t30-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t30-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t30-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t30-seed44/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t30-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t30-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t30-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t30-seed44/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t30-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t30-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t30-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t30-seed44/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t30-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t30-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t30-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t30-seed45/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t30-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t30-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t30-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t30-seed45/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t30-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t30-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t30-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t30-seed45/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t30-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t30-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t30-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t30-seed46/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t30-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t30-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t30-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t30-seed46/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t30-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t30-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t30-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t30-seed46/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t30-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t30-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t30-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t30-seed47/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t30-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t30-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t30-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t30-seed47/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t30-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t30-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t30-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t30-seed47/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t30-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t30-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t30-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t30-seed48/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t30-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t30-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t30-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t30-seed48/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t30-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t30-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t30-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t30-seed48/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t30-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t30-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t30-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t30-seed49/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t30-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t30-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t30-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t30-seed49/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t30-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t30-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t30-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t30-seed49/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t30-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t30-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t30-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t4-seed42/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t4-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t4-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t4-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t4-seed42/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t4-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t4-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t4-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t4-seed42/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t4-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t4-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t4-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t4-seed43/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t4-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t4-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t4-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t4-seed43/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t4-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t4-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t4-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t4-seed43/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t4-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t4-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t4-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t4-seed44/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t4-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t4-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t4-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t4-seed44/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t4-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t4-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t4-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t4-seed44/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t4-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t4-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t4-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t4-seed45/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t4-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t4-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t4-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t4-seed45/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t4-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t4-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t4-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t4-seed45/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t4-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t4-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t4-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t4-seed46/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t4-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t4-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t4-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t4-seed46/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t4-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t4-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t4-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t4-seed46/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t4-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t4-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t4-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t4-seed47/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t4-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t4-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t4-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t4-seed47/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t4-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t4-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t4-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t4-seed47/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t4-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t4-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t4-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t4-seed48/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t4-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t4-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t4-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t4-seed48/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t4-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t4-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t4-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t4-seed48/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t4-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t4-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t4-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t4-seed49/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t4-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t4-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t4-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t4-seed49/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t4-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t4-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t4-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t4-seed49/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t4-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t4-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t4-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t60-seed42/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t60-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t60-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t60-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t60-seed42/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t60-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t60-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t60-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t60-seed42/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t60-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t60-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t60-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t60-seed43/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t60-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t60-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t60-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t60-seed43/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t60-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t60-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t60-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t60-seed43/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t60-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t60-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t60-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t60-seed44/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t60-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t60-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t60-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t60-seed44/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t60-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t60-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t60-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t60-seed44/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t60-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t60-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t60-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t60-seed45/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t60-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t60-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t60-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t60-seed45/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t60-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t60-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t60-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t60-seed45/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t60-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t60-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t60-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t60-seed46/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t60-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t60-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t60-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t60-seed46/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t60-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t60-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t60-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t60-seed46/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t60-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t60-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t60-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t60-seed47/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t60-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t60-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t60-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t60-seed47/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t60-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t60-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t60-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t60-seed47/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t60-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t60-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t60-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t60-seed48/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t60-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t60-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t60-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t60-seed48/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t60-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t60-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t60-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t60-seed48/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t60-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t60-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t60-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t60-seed49/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t60-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t60-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t60-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t60-seed49/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t60-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t60-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t60-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t60-seed49/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t60-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t60-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t60-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t8-seed42/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t8-seed42/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t8-seed42/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t8-seed42/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t8-seed42/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t8-seed42/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t8-seed42/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t8-seed42/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t8-seed42/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t8-seed42/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t8-seed42/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t8-seed42/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t8-seed43/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t8-seed43/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t8-seed43/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t8-seed43/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t8-seed43/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t8-seed43/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t8-seed43/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t8-seed43/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t8-seed43/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t8-seed43/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t8-seed43/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t8-seed43/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t8-seed44/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t8-seed44/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t8-seed44/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t8-seed44/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t8-seed44/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t8-seed44/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t8-seed44/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t8-seed44/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t8-seed44/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t8-seed44/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t8-seed44/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t8-seed44/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t8-seed45/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t8-seed45/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t8-seed45/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t8-seed45/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t8-seed45/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t8-seed45/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t8-seed45/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t8-seed45/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t8-seed45/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t8-seed45/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t8-seed45/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t8-seed45/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t8-seed46/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t8-seed46/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t8-seed46/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t8-seed46/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t8-seed46/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t8-seed46/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t8-seed46/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t8-seed46/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t8-seed46/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t8-seed46/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t8-seed46/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t8-seed46/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t8-seed47/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t8-seed47/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t8-seed47/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t8-seed47/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t8-seed47/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t8-seed47/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t8-seed47/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t8-seed47/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t8-seed47/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t8-seed47/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t8-seed47/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t8-seed47/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t8-seed48/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t8-seed48/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t8-seed48/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t8-seed48/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t8-seed48/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t8-seed48/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t8-seed48/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t8-seed48/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t8-seed48/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t8-seed48/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t8-seed48/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t8-seed48/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True) diff --git a/harbor/sudoku-sketchpad-t8-seed49/tests/_cb/contextbench/graders/log.py b/harbor/sudoku-sketchpad-t8-seed49/tests/_cb/contextbench/graders/log.py index f324ac940fdbcd5265f614c9170ad315fad438b6..f44362eea4e1dddd0b925ed9f4119f0a6c03cc3d 100644 --- a/harbor/sudoku-sketchpad-t8-seed49/tests/_cb/contextbench/graders/log.py +++ b/harbor/sudoku-sketchpad-t8-seed49/tests/_cb/contextbench/graders/log.py @@ -41,10 +41,14 @@ ANSWER_RE = re.compile( _TAG_RE = re.compile(r"\s*#[0-9a-f]+\s*$") +#: A complete number: optional sign, digits, optional decimal part. +_NUMBER_RE = re.compile(r"[-+]?\d+(?:\.\d+)?") + + def _count_correct(want: str, answers: set[str]) -> bool: - """A count is correct when the integer appears as a standalone token.""" - pat = re.compile(rf"(? bool: diff --git a/harbor/sudoku-sketchpad-t8-seed49/tests/_cb/contextbench/runner.py b/harbor/sudoku-sketchpad-t8-seed49/tests/_cb/contextbench/runner.py index c8e7fad637d31f39ff126eba6ea64f1540cb285c..cdd6f6bf1379a87dce91f092e109b2d5fe59acc3 100644 --- a/harbor/sudoku-sketchpad-t8-seed49/tests/_cb/contextbench/runner.py +++ b/harbor/sudoku-sketchpad-t8-seed49/tests/_cb/contextbench/runner.py @@ -105,6 +105,7 @@ def run( max_turns: int | None = None, system_prompt: str | None = None, delivery: str = "reply", + overwrite: bool = False, ) -> dict: """Run one instance end to end and return the result dict. @@ -115,9 +116,22 @@ def run( Writes ``context_snapshots/turn-NNNN.json`` per turn, ``interactive_stream.json`` and ``result.json`` into ``out_dir``, and returns the same result dict. + + ``out_dir`` must be empty or absent: the graders read every snapshot in it, so + files from an earlier run would be credited to this one. ``overwrite=True`` + deletes an existing ``out_dir`` first. """ instance = load_instance(instance_dir) out = Path(out_dir) + if out.exists() and any(out.iterdir()): + if not overwrite: + raise FileExistsError( + f"{out} is not empty; the graders would read its old snapshots as part " + "of this run. Use a fresh directory or pass overwrite=True." + ) + import shutil + + shutil.rmtree(out) out.mkdir(parents=True, exist_ok=True) usable = budget - reserve diff --git a/harbor/sudoku-sketchpad-t8-seed49/tests/_cb/contextbench/stream.py b/harbor/sudoku-sketchpad-t8-seed49/tests/_cb/contextbench/stream.py index 0991206f571efcb5cc6303753cca7fa24d90531e..3a34529c44f25a505a0fff4a143f1436a77f7d3a 100644 --- a/harbor/sudoku-sketchpad-t8-seed49/tests/_cb/contextbench/stream.py +++ b/harbor/sudoku-sketchpad-t8-seed49/tests/_cb/contextbench/stream.py @@ -79,14 +79,11 @@ _READY_SEGMENT_RE = _standalone_command_re(ADVANCE_MARKER) _SUBMIT_LINE_RE = _standalone_line_re(SUBMIT_MARKER) _SUBMIT_SEGMENT_RE = _standalone_command_re(SUBMIT_MARKER) -#: Shell statement separators. A bundled command such as +#: Statements are split on ``;``, newline, ``&&`` and ``||`` outside quotes (see +#: :func:`_split_unquoted`). A bundled command such as #: ``printf '<<>>'; echo READY_FOR_NEXT_OP`` advances, because its -#: last segment is a clean echo. -_SEPARATORS = re.compile(r";|\n|&&|\|\|") - - +#: last statement is a clean echo. _HEREDOC_OPEN = re.compile(r"<<(-?)\s*(['\"]?)([A-Za-z_][A-Za-z0-9_]*)\2") -_MULTILINE_QUOTED = re.compile(r"'[^']*\n[^']*'|\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\"") def _strip_heredoc_bodies(command: str) -> str: @@ -112,12 +109,65 @@ def _strip_heredoc_bodies(command: str) -> str: return "\n".join(out) +def _split_unquoted(text: str) -> list[str]: + """Split on ``;``, newline, ``&&`` and ``||`` outside quotes. + + Single quotes are literal to the closing quote; double quotes honour backslash + escapes; a backslash outside quotes escapes the next character. A separator inside + quotes is data (``printf '%s' 'a;echo READY_FOR_NEXT_OP;b'`` is one statement). + An unterminated quote keeps the rest of the text in the current statement. + """ + segments: list[str] = [] + buf: list[str] = [] + quote: str | None = None + i, n = 0, len(text) + while i < n: + c = text[i] + if quote == "'": + buf.append(c) + if c == "'": + quote = None + i += 1 + continue + if quote == '"': + buf.append(c) + if c == "\\" and i + 1 < n: + buf.append(text[i + 1]) + i += 2 + continue + if c == '"': + quote = None + i += 1 + continue + if c == "\\" and i + 1 < n: + buf.append(text[i:i + 2]) + i += 2 + continue + if c in "'\"": + quote = c + buf.append(c) + i += 1 + continue + if c in ";\n": + segments.append("".join(buf)) + buf = [] + i += 1 + continue + if text.startswith("&&", i) or text.startswith("||", i): + segments.append("".join(buf)) + buf = [] + i += 2 + continue + buf.append(c) + i += 1 + segments.append("".join(buf)) + return segments + + def _statements(command: str) -> list[str]: """Split a shell command into statements, ignoring here-document bodies and - quoted strings that span lines (both are data, not statements).""" - text = _strip_heredoc_bodies(command) - text = _MULTILINE_QUOTED.sub("''", text) - return _SEPARATORS.split(text) + separators inside quoted strings (both are data, not statements).""" + return _split_unquoted(_strip_heredoc_bodies(command)) @dataclass(frozen=True)