Skip to content

The agent that grows with you

perf(compaction): cap the typed-boundary bisect at budget*4 chars

kshitijk4poor98c77d065776
diff --git a/agent/native_compaction.py b/agent/native_compaction.py
index 09dd1cc30f0..227ac0e3406 100644
--- a/agent/native_compaction.py
+++ b/agent/native_compaction.py
@@ -196,7 +196,8 @@ def _truncate_input_text_parts(content: List[Dict[str, Any]], budget: int) -> Li
if cost > budget:
# Use the same estimator as retention: four characters per token is
# not a safe head bound for CJK or multibyte non-ASCII text.
- lo, hi = 0, len(text)
+ # Every char costs >= 1/4 token, so no prefix past budget*4 fits.
+ lo, hi = 0, min(len(text), budget * 4)
while lo < hi:
mid = (lo + hi + 1) // 2
if _approx_tokens(text[:mid]) <= budget: