Skip to content

Experience-Driven Black-Box Cost Control for Coding Agents

fix: reject fractional loop-guard count thresholds

Sheikh Muhammad Talha Bin Khalidd352dcc28393
diff --git a/tests/test_config_integer_thresholds.py b/tests/test_config_integer_thresholds.py
new file mode 100644
index 0000000..64682f0
--- /dev/null
+++ b/tests/test_config_integer_thresholds.py
@@ -0,0 +1,56 @@
+"""Count thresholds must not silently truncate fractions from JSON numbers."""
+import json
+
+import pytest
+
+from tokeneconomizer.plugin.config import (LOOPGUARD_KEYS, LiveConfig, load_config,
+ write_config)
+
+
+INTEGER_THRESHOLDS = [name for name, spec in LOOPGUARD_KEYS.items() if spec[0] is int]
+
+
[email protected]("name", INTEGER_THRESHOLDS)
[email protected]("prefix", ["loopguard", "spiral"])
+def test_fractional_numeric_count_is_rejected_without_changing_the_previous_value(name, prefix):
+ cfg = LiveConfig()
+ assert cfg.set(f"{prefix}.{name}", 8)
+ assert not cfg.set(f"{prefix}.{name}", 12.5)
+ assert cfg.loopguard_thresholds[name] == 8
+ assert cfg.warnings
+
+
[email protected]("value", [12, 12.0, "12", "12.0"])
+def test_whole_number_representations_remain_supported(value):
+ cfg = LiveConfig()
+ assert cfg.set("loopguard.min_turns", value)
+ assert cfg.loopguard_thresholds["min_turns"] == 12
+ assert type(cfg.loopguard_thresholds["min_turns"]) is int
+
+
[email protected]("source", ["file", "environment"])
+def test_fractional_count_from_json_does_not_become_a_pinned_threshold(tmp_path, source):
+ thresholds = {"min_turns": 12.5, "stagnation": 0.5}
+ env = {}
+ if source == "file":
+ (tmp_path / "config.json").write_text(
+ json.dumps({"loopguard_thresholds": thresholds}), encoding="utf-8")
+ else:
+ env["TE_LOOPGUARD_THRESHOLDS"] = json.dumps(thresholds)
+ cfg = load_config(str(tmp_path), env=env)
+ assert "min_turns" not in cfg.loopguard_thresholds
+ assert cfg.loopguard_thresholds["stagnation"] == 0.5
+ assert cfg.warnings
+
+
[email protected]("updates", [
+ {"loopguard.min_turns": 12.5},
+ {"loopguard_thresholds": {"min_turns": 12.5}},
+])
+def test_fractional_update_leaves_the_saved_config_unchanged(tmp_path, updates):
+ path = str(tmp_path / "config.json")
+ write_config(path, {"loopguard.min_turns": 8})
+ before = (tmp_path / "config.json").read_bytes()
+ with pytest.raises(ValueError):
+ write_config(path, updates)
+ assert (tmp_path / "config.json").read_bytes() == before
diff --git a/tokeneconomizer/plugin/config.py b/tokeneconomizer/plugin/config.py
index 058a0c9..c31b3cc 100644
--- a/tokeneconomizer/plugin/config.py
+++ b/tokeneconomizer/plugin/config.py
@@ -241,9 +241,12 @@ class LiveConfig:
try:
if isinstance(value, bool):
raise ValueError("not a number")
- if not math.isfinite(float(value)):
+ number = float(value)
+ if not math.isfinite(number):
raise ValueError("must be finite")
- v = kind(float(value)) if kind is int and float(value).is_integer() else kind(value)
+ if kind is int and not number.is_integer():
+ raise ValueError("must be a whole number")
+ v = kind(number) if kind is int else kind(value)
except (TypeError, ValueError, OverflowError) as e:
self.warnings.append(f"{source}: loopguard.{name}={value!r} rejected ({e})")
return False