Compaction: the model's notes replace the middle of a long run (#158)
CI / checks (push) Skipped
CI / checks (pull_request) Successful in 1m57s
CI / e2e (pull_request) Successful in 4m57s

Before each driver call, over compact_at (0.6) of the prompt budget, the
same model is asked for notes over the very request it was about to get,
plus one message, so the server's cache covers all but the notes. The notes,
with code's list of the calls they replace, become a compaction event that
renders in place of its range, keeping word for word what started the work
and the last few turns. Rare, large steps keep the prompt's start unchanged
between them. A later compaction folds in the earlier; one from an earlier
run's view is hidden.

Failed notes are logged and fitting takes over. Fitting now applies
compactions before it measures, records what it cuts (Trim) and logs each
cut as context.trimmed. Wired into both agents' drivers and the benchmark;
nisse-dev's helper loop is a follow-up.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J5Utyfzw7gF7UhjRDEtjTk
This commit is contained in:
dongarandClaude Opus 5.5 committed 2026-10-17 22:51:36 +00:00
1 parent fa8d93cf41
commit cb638fe687
20 files changed
+952 -44

No files matched your search

+16 -2
View File
@@ -66,11 +66,25 @@ def test_compaction_replaces_its_range_only() -> None:
)
)
remaining = effective_events(events)
assert [e.id for e in remaining] == [3, 4]
# The summary takes the place of what it replaces.
assert [e.id for e in remaining] == [4, 3]
assert effective_events(remaining) == remaining # applying again: no change
body = OpenAIChatRenderer().render(events, tools=None, model="q")
contents = [m["content"] for m in body["messages"]]
# Both render as user messages, so they share one: roles must alternate.
assert contents == ["kept\n\n[summary of earlier conversation]\nthe old stuff"]
assert contents == ["[summary of earlier conversation]\nthe old stuff\n\nkept"]
def test_a_later_compaction_folds_in_an_earlier_one() -> None:
events = stored([UserMessage(text=f"e{i}", source="m") for i in range(1, 7)])
events.append(
StoredEvent(id=7, ts="t", event=Compaction("first", "q", replaces=[2, 3]))
)
events.append(
StoredEvent(id=8, ts="t", event=Compaction("second", "q", replaces=[2, 5]))
)
remaining = effective_events(events)
assert [e.id for e in remaining] == [1, 8, 6]
def test_tool_call_without_a_result_gets_a_placeholder() -> None: