Skip to content

The agent that grows with you

refactor(compaction): share the budgeted head bisect across string and typed boundaries

kshitijk4poor40eebc785a42
diff --git a/agent/native_compaction.py b/agent/native_compaction.py
index 919931d09f3..1e5636e2146 100644
--- a/agent/native_compaction.py
+++ b/agent/native_compaction.py
@@ -207,18 +207,9 @@ def _truncate_input_text_parts(content: List[Dict[str, Any]], budget: int) -> Li
text = part["text"]
cost = _approx_tokens(text)
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.
- # 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:
- lo = mid
- else:
- hi = mid - 1
- if lo:
- head.append({**part, "text": text[:lo]})
+ kept = _head_within_budget(text, budget)
+ if kept:
+ head.append({**part, "text": kept})
break
head.append(part)
budget -= cost