From df9c20e6c215fc833e1d35e62a21a20721d82ed0 Mon Sep 17 00:00:00 2001 From: red person Date: Mon, 29 Jun 2026 11:56:17 -0700 Subject: [PATCH] Ignore invalid context budget numbers (#1831) --- src/context_budget.py | 11 +++++++++-- tests/test_context_budget.py | 5 +++++ 2 files changed, 14 insertions(+), 2 deletions(-) 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