Skip to content

CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.

fix: correct thinking budget guard and partial JSON stack pop

xf gong2be76f7a57df
diff --git a/cubepi/providers/base.py b/cubepi/providers/base.py
index 1226b3d..dfc8495 100644
--- a/cubepi/providers/base.py
+++ b/cubepi/providers/base.py
@@ -55,7 +55,7 @@ def adjust_max_tokens_for_thinking(
max_tokens = min(base_max_tokens + thinking_budget, model_max_tokens)
- if max_tokens <= thinking_budget:
+ if max_tokens - thinking_budget < min_output_tokens:
thinking_budget = max(0, max_tokens - min_output_tokens)
return max_tokens, thinking_budget
diff --git a/cubepi/utils/json_parse.py b/cubepi/utils/json_parse.py
index 4005c4f..bb1e672 100644
--- a/cubepi/utils/json_parse.py
+++ b/cubepi/utils/json_parse.py
@@ -139,7 +139,7 @@ def _close_partial_json(text: str) -> str:
continue
if ch == '"':
in_string = False
- if stack:
+ if stack and stack[-1] == '"':
stack.pop()
i += 1
continue
diff --git a/tests/providers/test_thinking_budgets.py b/tests/providers/test_thinking_budgets.py
index 37be369..1fa32b1 100644
--- a/tests/providers/test_thinking_budgets.py
+++ b/tests/providers/test_thinking_budgets.py
@@ -140,11 +140,25 @@ class TestAdjustMaxTokensForThinking:
def test_model_cap_just_above_budget(self):
# max_tokens = min(8192 + 1024, 1025) = 1025
- # 1025 > 1024 so budget stays at 1024
+ # 1025 - 1024 = 1 < min_output_tokens(1024), so budget reduced
max_tokens, budget = adjust_max_tokens_for_thinking(
base_max_tokens=8192,
model_max_tokens=1025,
reasoning_level="minimal",
)
assert max_tokens == 1025
- assert budget == 1024
+ assert budget == 1 # max(0, 1025 - 1024)
+
+ def test_output_tokens_minimum_guaranteed(self):
+ # Bug case: base=8192, model=9000, medium budget=8192
+ # max_tokens = min(16384, 9000) = 9000
+ # Without fix: 9000 > 8192 so budget stays 8192, only 808 for output
+ # With fix: 9000 - 8192 = 808 < 1024, so budget = 9000 - 1024 = 7976
+ max_tokens, budget = adjust_max_tokens_for_thinking(
+ base_max_tokens=8192,
+ model_max_tokens=9000,
+ reasoning_level="medium",
+ )
+ assert max_tokens == 9000
+ assert budget == 7976
+ assert max_tokens - budget >= 1024