Skip to content

The agent that grows with you

fix(config): reader gate covers every PyYAML parse entry point and requires the escape's reason; unshadow config_exists

Ben Barclay801847e95479
diff --git a/scripts/check_config_yaml_readers.py b/scripts/check_config_yaml_readers.py
index 3f9e36eb6bb..87f51289ba9 100644
--- a/scripts/check_config_yaml_readers.py
+++ b/scripts/check_config_yaml_readers.py
@@ -12,8 +12,9 @@ Flags, in the scanned trees:
* ``open(p)``, ``p.open()``, ``p.read_text()``, ``p.read_bytes()``, ``p.stat()``,
``p.exists()``, ``p.is_file()``, ``os.stat(p)``, ``os.path.exists(p)``, ``os.path.isfile(p)``,
- ``os.path.getmtime(p)``, ``load_yaml_file_readonly(p)``, ``safe_load(p)``,
- ``fast_safe_load(p)``, ``yaml.load(p)`` and any ``*_load_yaml*`` / ``*_read_yaml*`` helper
+ ``os.path.getmtime(p)``, ``os.path.getsize(p)``, ``load_yaml_file_readonly(p)``,
+ ``fast_safe_load(p)``, ``yaml.load(p)`` / ``safe_load`` / ``full_load`` / ``unsafe_load`` /
+ ``compose`` (and their ``*_all`` forms) and any ``*_load_yaml*`` / ``*_read_yaml*`` helper
call, where ``p`` is a config path: an expression naming ``config.yaml``,
any ``*config_path()`` call (``get_config_path()``, ``_active_config_path()``), or a local name / parameter bound to one
(``cfg_path = home / "config.yaml"`` then ``open(cfg_path)``);
@@ -24,7 +25,8 @@ Flags, in the scanned trees:
Existence checks count: for a backend whose user layer is not a local file, "no local
config.yaml" must never mean "use defaults" (design §4.2) — ``config_exists()`` asks the backend.
-Suppress a true false positive with ``# config-reader: ok — <why>`` on the call's line.
+Suppress a true false positive with ``# config-reader: ok — <why>`` on the call's line; a
+marker without a reason after the dash does not suppress.
Usage: python3 scripts/check_config_yaml_readers.py [paths...]
"""
@@ -50,13 +52,19 @@ FOREIGN_CONFIG_FILES = {
"agent/proxy_sources/iron_proxy.py": "iron-proxy's proxy.yaml",
}
SUPPRESS = "# config-reader: ok"
+# The escape must carry its reason: ``# config-reader: ok — <why>`` (``-`` / ``--`` accepted).
+SUPPRESS_RE = re.compile(r"# config-reader: ok\s*(?:—|--?)\s*\S")
# An expression that evaluates to a user config.yaml path.
CONFIG_PATH_EXPR_RE = re.compile(r"""["']config\.yaml["']|\w*config_path\(\)""")
# Parameters that conventionally carry the user config path.
CONFIG_PARAM_NAMES = {"config_path", "cfg_path", "config_yaml_path"}
PATH_METHOD_READS = {"open", "read_text", "read_bytes", "stat", "exists", "is_file"}
-FUNC_READS = {"exists", "isfile", "open", "load_yaml_file_readonly", "safe_load", "fast_safe_load", "getmtime", "stat"}
+FUNC_READS = {
+ "exists", "isfile", "open", "getmtime", "getsize", "stat", "load_yaml_file_readonly", "fast_safe_load",
+ # Every PyYAML entry point that parses a stream, not just safe_load.
+ "safe_load", "safe_load_all", "full_load", "full_load_all", "unsafe_load", "unsafe_load_all",
+ "load_all", "compose", "compose_all"}
BYPASS_WRITERS = {"atomic_roundtrip_yaml_update", "atomic_roundtrip_yaml_save", "atomic_write_text"}
YAML_HELPER_RE = re.compile(r"(load|read)_yaml|yaml_(load|read)")
@@ -132,8 +140,11 @@ class _Scanner:
return names
def flag(self, node: ast.Call, why: str) -> None:
- if SUPPRESS in self.lines[node.lineno - 1]:
+ line = self.lines[node.lineno - 1]
+ if SUPPRESS_RE.search(line):
return
+ if SUPPRESS in line:
+ why = f"{why}; the escape needs a reason (`{SUPPRESS} — <why>`)"
self.problems.append(f"{self.rel}:{node.lineno}: {why} — go through hermes_cli.config_backend")
def check_call(self, node: ast.Call, names: set[str]) -> None:
diff --git a/tests/hermes_cli/test_config_backend.py b/tests/hermes_cli/test_config_backend.py
index 1692d1de0ce..ca077314a5d 100644
--- a/tests/hermes_cli/test_config_backend.py
+++ b/tests/hermes_cli/test_config_backend.py
@@ -227,6 +227,19 @@ class TestReaderGate:
problems = guard.scan_file(bad)
assert sorted(int(p.split(":")[1]) for p in problems) == [4, 6, 13], problems
+ @pytest.mark.parametrize("call", [
+ "yaml.full_load(config_path)", "yaml.unsafe_load(config_path)", "yaml.compose(config_path)",
+ "yaml.load_all(config_path)", "os.path.getsize(config_path)",
+ "config_path.stat() # config-reader: ok", # an escape without a reason does not suppress
+ ])
+ def test_every_read_form_is_flagged(self, tmp_path, call):
+ guard = self._guard()
+ bad = tmp_path / "hermes_cli" / "bad_reader.py"
+ bad.parent.mkdir()
+ bad.write_text(f"import os, yaml\ndef a(config_path):\n return {call}\n", encoding="utf-8")
+ with patch.object(guard, "ROOT", tmp_path):
+ assert len(guard.scan_file(bad)) == 1
+
def test_backend_calls_are_clean(self, tmp_path):
guard = self._guard()
good = tmp_path / "hermes_cli" / "good_reader.py"
diff --git a/tools/terminal_scope.py b/tools/terminal_scope.py
index a9110eb6d29..240c4f6c8e5 100644
--- a/tools/terminal_scope.py
+++ b/tools/terminal_scope.py
@@ -161,10 +161,10 @@ def build_profile_terminal_scope(
# "unparseable" into {}): present-but-unparseable must fail closed.
config_path = home / "config.yaml"
try:
- config_exists = config_exists(config_path)
+ has_config = config_exists(config_path)
except Exception as exc:
raise TerminalPolicyUnavailable(f"cannot resolve terminal config in {home}: {exc}") from exc
- if config_exists:
+ if has_config:
from hermes_cli.config_backend import read_config_doc_readonly
try: