Skip to content

Self-hosted AI workspace.

Python

Ignore invalid context budget numbers (#1831)

red personc8fc4d003c38
diff --git a/src/context_budget.py b/src/context_budget.py
index de4789e28..a97fbce88 100644
--- a/src/context_budget.py
+++ b/src/context_budget.py
@@ -18,6 +18,13 @@ DEFAULT_BUDGET = 6000
DEFAULT_HEADROOM = 0.85
+def _int_or_zero(value) -> int:
+ try:
+ return int(value or 0)
+ except (TypeError, ValueError):
+ return 0
+
+
def compute_input_token_budget(
configured: int,
context_length: int,
@@ -48,8 +55,8 @@ def compute_input_token_budget(
- When the window is unknown (context_length <= 0), use the conservative
``default`` budget and do NOT scale off the fallback.
"""
- configured = int(configured or 0)
- context_length = int(context_length or 0)
+ configured = _int_or_zero(configured)
+ context_length = _int_or_zero(context_length)
if explicit and configured > 0:
return min(configured, context_length) if context_length > 0 else configured
diff --git a/tests/test_context_budget.py b/tests/test_context_budget.py
index 05db1d811..8480eae08 100644
--- a/tests/test_context_budget.py
+++ b/tests/test_context_budget.py
@@ -33,6 +33,11 @@ def test_unknown_window_falls_back_to_configured():
assert compute_input_token_budget(0, 0, explicit=False) == 6000 # default
+def test_invalid_numeric_inputs_fall_back_cleanly():
+ assert compute_input_token_budget("bad", "also bad", explicit=False) == 6000
+ assert compute_input_token_budget("bad", 128000, explicit=True) == int(128000 * 0.85)
+
+
def test_is_setting_overridden_reads_raw_saved_file(tmp_path, monkeypatch):
import src.settings as settings