fix(learn): keep the whole digest inside max_tokens * 4

The event stream counted each line without the newline that joins it and
checked the budget before formatting the next line, so it let in the line
that crossed it, then the truncation markers on top. A full digest on real
Codex sessions came to 322,604 chars against a 320,000-char budget.

Each line now costs its length plus one, and a session header, event or
tool-call line goes in only if it leaves 100 chars for the truncation
markers that can follow it. Once a session is cut, no later session is
started. The loop section's cap counts the same newlines and the same
reserve. Only a prior-patterns section that leaves no room on its own can
still push the digest over, since it is never cut.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Garm
2026-10-19 23:34:45 +02:00
co-authored by Claude Opus 5.5
parent d9391dab3a
commit df75e3faf4
2 changed files with 68 additions and 20 deletions
+30 -20
View File
@@ -397,6 +397,12 @@ def _build_digest(
# Budget tracking — stop adding events when we approach the limit
# Rough estimate: 4 chars per token
char_budget = max_tokens * 4
# Every line costs its length plus the newline joining it, and goes in only
# if it leaves this room for the truncation markers that can follow it (an
# events line, a blank, a sessions line), so the digest stays inside
# char_budget. Only a prior-patterns section that leaves no such room on
# its own (never cut, see below) can push it past.
room = char_budget - 100
# Prior learned patterns (if any) — gives the LLM the current baseline so
# it can produce complete updated sections instead of condensed deltas.
@@ -411,24 +417,19 @@ def _build_digest(
# patterns leave short. Rendered whole, every loop with every index, the
# section alone outgrew the digest on a machine with years of Codex
# sessions, which learn scans as one project.
pre_event_chars = sum(len(ln) for ln in lines) + len(prior_section)
pre_event_chars = sum(len(ln) + 1 for ln in lines) + len(prior_section) + 2
loop_section = format_loops_for_digest(
loops, max_chars=min(char_budget // 4, char_budget - pre_event_chars)
loops, max_chars=min(char_budget // 4, room - pre_event_chars)
)
if loop_section:
lines.append(loop_section)
if prior_section:
lines.append(prior_section)
chars_used = sum(len(ln) for ln in lines)
chars_used = sum(len(ln) + 1 for ln in lines)
full = False
for session in sessions:
if chars_used > char_budget:
lines.append(
f"... (remaining {len(sessions) - sessions.index(session)} sessions truncated)"
)
break
session_header = (
f"=== Session {session.session_id[:12]} "
f"({len(session.tool_calls)} calls, {session.failure_count} failures"
@@ -436,29 +437,38 @@ def _build_digest(
if session.total_input_tokens:
session_header += f", {session.total_input_tokens:,} input tokens"
session_header += ") ==="
if full or chars_used + len(session_header) + 1 > room:
lines.append(
f"... (remaining {len(sessions) - sessions.index(session)} sessions truncated)"
)
break
lines.append(session_header)
chars_used += len(session_header)
chars_used += len(session_header) + 1
# Use events if available (richer context), fall back to tool_calls
if session.events:
for event in session.events:
if chars_used > char_budget:
lines.append(" ... (remaining events truncated)")
break
event_line = _format_event(event)
if event_line:
lines.append(event_line)
chars_used += len(event_line)
if not event_line:
continue
if chars_used + len(event_line) + 1 > room:
lines.append(" ... (remaining events truncated)")
full = True
break
lines.append(event_line)
chars_used += len(event_line) + 1
else:
for tc in session.tool_calls:
if chars_used > char_budget:
lines.append(" ... (remaining calls truncated)")
break
tc_line = _format_tool_call(tc)
if chars_used + len(tc_line) + 1 > room:
lines.append(" ... (remaining calls truncated)")
full = True
break
lines.append(tc_line)
chars_used += len(tc_line)
chars_used += len(tc_line) + 1
lines.append("")
chars_used += 1
return "\n".join(lines)
+38
View File
@@ -38,6 +38,7 @@ from headroom.learn.models import (
Recommendation,
RecommendationTarget,
SessionData,
SessionEvent,
ToolCall,
)
@@ -201,6 +202,43 @@ class TestDigestSurfacesLoops:
assert "~1,000,000 tokens wasted" in digest
assert len(digest) <= max_tokens * 4
@pytest.mark.parametrize("with_loops", [False, True], ids=["no-loops", "loops"])
@pytest.mark.parametrize("with_events", [False, True], ids=["tool-calls", "events"])
@pytest.mark.parametrize("max_tokens", [80_000, 10_000])
def test_whole_digest_stays_inside_the_budget(self, max_tokens, with_events, with_loops):
# Far more session text than the budget: every line and its newline
# counts, and none goes in that would leave no room for the markers.
sessions = []
for s in range(5):
calls = [
ToolCall(
name="Bash",
tool_call_id=f"{s}-{i}",
input_data={"command": f"run {i}"},
output="e" * 300,
is_error=True,
msg_index=i,
)
for i in range(3000)
]
events = (
[SessionEvent(type="tool_call", msg_index=c.msg_index, tool_call=c) for c in calls]
if with_events
else []
)
sessions.append(SessionData(session_id=f"s{s}", tool_calls=calls, events=events))
loops = (
[
LoopPattern("Grep", "s", "x" * 2000, 300, False, 1_000_000 - i, list(range(300)))
for i in range(2000)
]
if with_loops
else []
)
digest = _build_digest(_project(), sessions, loops=loops, max_tokens=max_tokens)
assert len(digest) <= max_tokens * 4
assert "remaining" in digest and "sessions truncated" in digest
# =============================================================================
# apply_loop_weighting