Skip to content

Experience-Driven Black-Box Cost Control for Coding Agents

Reject non-finite numeric configuration values

aifanatic03e35c533945
diff --git a/tests/test_contribution_finite_config.py b/tests/test_contribution_finite_config.py
new file mode 100644
index 0000000..1910b89
--- /dev/null
+++ b/tests/test_contribution_finite_config.py
@@ -0,0 +1,10 @@
+import pytest
+from tokeneconomizer.plugin.config import LiveConfig
+
[email protected]("key,value", [("loopguard.judge_confidence", "nan"), ("loopguard.envelope_margin", "inf"), ("loopguard.min_turns", float("inf")), ("repomap_tokens", float("inf"))])
+def test_nonfinite_config_is_rejected(key, value):
+ cfg = LiveConfig()
+ before = cfg.as_dict()
+ assert cfg.set(key, value) is False
+ assert cfg.as_dict() == before
+ assert cfg.warnings
diff --git a/tokeneconomizer/plugin/config.py b/tokeneconomizer/plugin/config.py
index 877a1e8..b3dc71c 100644
--- a/tokeneconomizer/plugin/config.py
+++ b/tokeneconomizer/plugin/config.py
@@ -60,6 +60,7 @@ a warning recorded in the result rather than raised: a typo in a config file mus
agent's hooks.
"""
import json
+import math
import os
from dataclasses import asdict, dataclass, field, fields
@@ -215,7 +216,7 @@ class LiveConfig:
raise ValueError(f"must be >= {lo}")
else:
v = str(value).strip()
- except (TypeError, ValueError) as e:
+ except (TypeError, ValueError, OverflowError) as e:
self.warnings.append(f"{source}: {key}={value!r} rejected ({e})")
return False
choices = self._CHOICES.get(key)
@@ -235,8 +236,10 @@ class LiveConfig:
try:
if isinstance(value, bool):
raise ValueError("not a number")
+ if not math.isfinite(float(value)):
+ raise ValueError("must be finite")
v = kind(float(value)) if kind is int and float(value).is_integer() else kind(value)
- except (TypeError, ValueError) as e:
+ except (TypeError, ValueError, OverflowError) as e:
self.warnings.append(f"{source}: loopguard.{name}={value!r} rejected ({e})")
return False
if (lo is not None and v < lo) or (hi is not None and v > hi):