permanence-training / tests /test_config_yaml.py
chane35's picture
PERMANENCE: reversibility-aware RL environment for training LLM agents
796da7c verified
Raw
History Blame
2.42 kB
"""Regression tests for training/config.py's tiny YAML parser.
Early pipeline startup crashed because the parser did not strip inline ``# comment``
suffixes from values — a two-line comment on the same line as ``group_size: 4``
was read verbatim as the value and int() threw. These tests make sure that
class of bug can't regress.
"""
from __future__ import annotations
import tempfile
from pathlib import Path
from training.config import TrainingConfig, load_simple_yaml
def test_strips_inline_comment_before_parsing():
"""The parser must strip `` # …`` suffixes from values so int/float
conversions don't see comment text."""
with tempfile.NamedTemporaryFile("w", suffix=".yaml", delete=False) as f:
f.write("group_size: 4 # A comment explaining this\n")
f.write("learning_rate: 4.0e-5 # trailing comment\n")
path = Path(f.name)
cfg_map = load_simple_yaml(path)
assert cfg_map["group_size"] == "4"
assert cfg_map["learning_rate"] == "4.0e-5"
def test_preserves_hash_when_no_space_before():
"""If a value has a ``#`` with no preceding space (e.g. URL fragment),
it must be preserved. Our rule is: only strip when the ``#`` is
whitespace-separated from the value."""
with tempfile.NamedTemporaryFile("w", suffix=".yaml", delete=False) as f:
f.write('url: https://example.com#anchor\n')
path = Path(f.name)
cfg_map = load_simple_yaml(path)
assert cfg_map["url"] == "https://example.com#anchor"
def test_full_config_load_with_inline_comments():
"""End-to-end: the shipped config must parse cleanly into a
TrainingConfig."""
root = Path(__file__).resolve().parent.parent
cfg_map = load_simple_yaml(root / "training" / "config.yaml")
cfg = TrainingConfig.from_mapping(cfg_map)
assert cfg.group_size >= 2
assert cfg.learning_rate > 0
assert cfg.total_episodes > 0
assert cfg.domain in ("devtools", "meridian", "") or cfg.domain is None
def test_section_dict_entries_also_strip_comments():
"""Indented section values must also have their comments stripped."""
with tempfile.NamedTemporaryFile("w", suffix=".yaml", delete=False) as f:
f.write("section:\n")
f.write(" key: value # inline comment in section\n")
path = Path(f.name)
cfg_map = load_simple_yaml(path)
assert cfg_map.get("section", {}).get("key") == "value"