Skip to content

armgit / Soup

Public Imported from GitHub
0

Fine-tune LLMs from one YAML. Layer streaming trains an 8B model on a 4 GB laptop GPU.

fix(expect): refuse top-level suite keys other than expectations (#1483) (#1499)

Srinivasan R6879f323f7c8
diff --git a/changelog.d/0.75.1/1499.fixed.md b/changelog.d/0.75.1/1499.fixed.md
new file mode 100644
index 00000000..5e9080e0
--- /dev/null
+++ b/changelog.d/0.75.1/1499.fixed.md
@@ -0,0 +1 @@
+- **`soup expect` refuses a top-level suite key other than `expectations` (#1483 by @Srinivasan8888 in #1499).** `parse_suite_spec` read only `expectations` and dropped every other top-level key, so a suite with `fail_fast: true` (or any typo) beside `expectations:` loaded, ran and exited 0 with no message about the key. It is now refused when the suite loads, before the data file is opened (exit 3), with every unknown key named; the key text is echoed as a Python literal so a hostile key cannot reach the terminal as a control sequence. This is the same check #1461 added one level down, inside an expectation entry.
diff --git a/docs/compliance.md b/docs/compliance.md
index bbfe9f2a..1b36430c 100644
--- a/docs/compliance.md
+++ b/docs/compliance.md
@@ -137,7 +137,9 @@ Supported names: `expect_no_pii`, `expect_token_length_between`,
`expect_no_refusal_pattern`, `expect_chosen_preferred_over_rejected_by_judge`.
Arguments go under `args:`. An entry key other than `name` / `args`, or an
argument the expectation does not take (`max_token`), is refused when the suite
-loads (exit 3), so a mistyped bound cannot silently run on the defaults.
+loads (exit 3), so a mistyped bound cannot silently run on the defaults. A
+top-level key other than `expectations` (a `fail_fast:` or a `name:` label
+beside it) is refused the same way, naming the key.
Scanned fields per format:
- ChatML / tool-calling / audio: every `messages[].content` (string or text parts), tool-call arguments (per message and top-level `tool_calls`), and `tools[].function.description`.
diff --git a/src/soup_cli/utils/expectations.py b/src/soup_cli/utils/expectations.py
index fb210407..312be9e3 100644
--- a/src/soup_cli/utils/expectations.py
+++ b/src/soup_cli/utils/expectations.py
@@ -782,6 +782,12 @@ def parse_suite_spec(raw: Any) -> SuiteSpec:
"""Validate a suite dict and return a ``SuiteSpec``."""
if not isinstance(raw, dict):
raise TypeError("suite spec must be a dict")
+ extra = [key for key in raw if key != "expectations"]
+ if extra: # #1483: a top-level key is a typo or an option that does not exist
+ raise ValueError(
+ f"unknown top-level key(s) {', '.join(map(_key_repr, extra))}; "
+ "a suite takes only 'expectations'"
+ )
raw_expectations = raw.get("expectations")
if raw_expectations is None:
raise ValueError("suite must define 'expectations' key")
diff --git a/tests/test_issue1231_expect_all_formats.py b/tests/test_issue1231_expect_all_formats.py
index cf00719b..6199895b 100644
--- a/tests/test_issue1231_expect_all_formats.py
+++ b/tests/test_issue1231_expect_all_formats.py
@@ -340,7 +340,6 @@ def test_cli_expect_flags_pii_and_refusal_and_exits_2(
suite_file = tmp_path / "suite.yaml"
suite_yaml = """
-name: test_all_formats_suite
expectations:
- name: expect_no_pii
- name: expect_no_refusal_pattern
diff --git a/tests/test_issue1483_suite_top_level_keys.py b/tests/test_issue1483_suite_top_level_keys.py
new file mode 100644
index 00000000..a019dadb
--- /dev/null
+++ b/tests/test_issue1483_suite_top_level_keys.py
@@ -0,0 +1,87 @@
+"""#1483: ``soup expect`` refuses every top-level suite key other than ``expectations``.
+
+#1428 / #1461 made ``parse_suite_spec`` refuse unknown keys inside an expectation
+entry. One level up it still read only ``raw.get("expectations")``, so a suite
+with ``fail_fast: true`` beside ``expectations:`` loaded, ran and exited 0 with no
+message about the key.
+"""
+
+from __future__ import annotations
+
+import re
+from pathlib import Path
+
+import pytest
+from typer.testing import CliRunner
+
+from soup_cli.cli import app
+from soup_cli.utils.expectations import parse_suite_yaml
+
+_TOP_LEVEL = "fail_fast: true\nexpectations:\n - name: expect_no_pii\n"
+_CONTROL = "expectations:\n - name: expect_no_pii\n"
+
+
+def _plain(text: str) -> str:
+ return re.sub(r"\x1b\[[0-9;?]*[A-Za-z]", "", text)
+
+
+def _run(tmp_path: Path, suite_text: str) -> tuple[int, str, str]:
+ (tmp_path / "data.jsonl").write_text('{"text": "a short clean row"}\n', encoding="utf-8")
+ (tmp_path / "suite.yaml").write_text(suite_text, encoding="utf-8")
+ result = CliRunner().invoke(app, ["expect", "data.jsonl", "suite.yaml"])
+ return result.exit_code, _plain(result.output), result.output
+
+
+class TestTopLevelKeys:
+ def test_a_key_beside_expectations_is_refused_by_name(self) -> None:
+ with pytest.raises(ValueError, match="fail_fast") as info:
+ parse_suite_yaml(_TOP_LEVEL)
+ assert "expectations" in str(info.value)
+
+ def test_every_extra_key_is_named(self) -> None:
+ text = "fail_fast: true\nseverity: high\n" + _CONTROL
+ with pytest.raises(ValueError) as info:
+ parse_suite_yaml(text)
+ assert "fail_fast" in str(info.value) and "severity" in str(info.value)
+
+ @pytest.mark.parametrize("key", ["name", "description", "version", "expectation"])
+ def test_a_label_or_typo_beside_expectations_is_refused(self, key: str) -> None:
+ # `name:` is the label the #1231 fixture carried; the rest are the next guesses.
+ with pytest.raises(ValueError, match=key):
+ parse_suite_yaml(f"{key}: x\nexpectations:\n - name: expect_no_pii\n")
+
+ def test_control_the_plain_form_still_loads(self) -> None:
+ assert [e.name for e in parse_suite_yaml(_CONTROL).expectations] == ["expect_no_pii"]
+
+
+class TestSoupExpectCli:
+ def test_exits_3_before_the_data_is_opened(
+ self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ import soup_cli.commands.expect as expect_mod
+
+ def _never(_path: str) -> list:
+ raise AssertionError("the data file was opened before the suite was refused")
+
+ monkeypatch.setattr(expect_mod, "_load_jsonl_rows", _never)
+ monkeypatch.chdir(tmp_path)
+ code, out, _ = _run(tmp_path, _TOP_LEVEL)
+ assert code == 3, out
+ assert "fail_fast" in out
+
+ def test_control_the_plain_form_exits_0(
+ self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ monkeypatch.chdir(tmp_path)
+ code, out, _ = _run(tmp_path, _CONTROL)
+ assert code == 0, out
+
+ def test_a_terminal_escape_in_a_top_level_key_does_not_reach_the_terminal(
+ self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ monkeypatch.chdir(tmp_path)
+ # YAML's ``\e`` escape yields a real ESC byte in the key once parsed.
+ code, out, raw = _run(tmp_path, '"\\e[2Jfast": true\n' + _CONTROL)
+ assert code == 3, out
+ assert "fast" in out # the key is echoed ...
+ assert "\x1b[2J" not in raw # ... but never as a raw control sequence