fix(learn): keep the digest's loop section inside its budget

_build_digest budgets the per-session event stream but rendered every
detected loop ahead of it, each with every message index it repeated at.
Codex learn scans all of a machine's Codex sessions as one project, so on
a heavy user the loop section alone outgrew the digest: a 1,786,526-char
prompt that Codex refuses over its 1,048,576-char input cap, on every
scan. The prompt-too-long retry could not help either, since halving the
budget never touched the loops.

Render only the costliest max_tokens // 400 loops (detect_loops sorts by
waste), each with its first 20 indices beside its repeat count. The
section stays under a quarter of the digest budget and halves with it on
a retry. apply_loop_weighting still sees every loop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Garm
2026-10-18 16:05:05 +02:00
co-authored by Claude Opus 5.5
parent fbf7bc3278
commit f52a43566a
2 changed files with 35 additions and 2 deletions
+18 -2
View File
@@ -23,6 +23,7 @@ import subprocess
import threading
import time
import typing
from dataclasses import replace
from headroom._subprocess import Popen, run
@@ -52,6 +53,13 @@ _MAX_DIGEST_TOKENS = 80_000 # Budget for the digest (leave room for prompt + ou
# budget halves on each "prompt is too long" failure; below this the digest
# carries too little signal to produce useful recommendations.
_MIN_DIGEST_TOKENS = 10_000
# The loop section precedes the budgeted event stream, so it is bounded on its
# own: one loop per this many digest tokens (a rendered loop line is ~100
# tokens, so the section stays under a quarter of the budget and halves with it
# on a retry), each listing its first _MAX_LOOP_INDICES message indices beside
# its repeat count.
_DIGEST_TOKENS_PER_LOOP = 400
_MAX_LOOP_INDICES = 20
# CLI tools to try when no API key is set (checked in order).
# Each entry: (binary_name, model_identifier, command_prefix). The claude-cli
@@ -393,8 +401,16 @@ def _build_digest(
lines.append("")
# Detected loops first — the most expensive waste pattern, so the LLM sees
# it before the (budget-truncatable) per-session event stream.
loop_section = format_loops_for_digest(loops)
# it before the (budget-truncatable) per-session event stream. Only the
# costliest fit (detect_loops sorts by waste): 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.
loop_section = format_loops_for_digest(
[
replace(lp, msg_indices=lp.msg_indices[:_MAX_LOOP_INDICES])
for lp in loops[: max_tokens // _DIGEST_TOKENS_PER_LOOP]
]
)
if loop_section:
lines.append(loop_section)
+17
View File
@@ -26,6 +26,7 @@ from headroom.learn.fixtures import (
)
from headroom.learn.loops import (
_IDENTITY_FIELDS,
LoopPattern,
_canonical_signature,
_identity_input,
_signature_tokens,
@@ -150,6 +151,22 @@ class TestDigestSurfacesLoops:
digest = _build_digest(_project(), [one_off_error_session()])
assert "Detected Loops" not in digest
def test_loop_section_stays_inside_the_digest_budget(self):
# Rendered whole, thousands of loops with hundreds of indices each made
# a 1.79M-char prompt, over Codex's 1,048,576-char input cap.
loops = [
LoopPattern("Bash", "s", "x" * 120, 300, False, 1_000_000 - i, list(range(300)))
for i in range(2000)
]
full = _build_digest(_project(), [], loops=loops)
small = _build_digest(_project(), [], loops=loops, max_tokens=10_000)
assert len(full) < 80_000 # a quarter of the 80k-token budget's chars
assert len(small) < 10_000
# The costliest loops are the ones kept, and the caller's list is intact.
assert "~1,000,000 tokens wasted" in small
assert "~999,000 tokens wasted" not in full
assert len(loops[0].msg_indices) == 300
# =============================================================================
# apply_loop_weighting