From 58df74fdb2bb296fa5ce5e859ee5a7e22d120827 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Wed, 25 Mar 2026 08:00:54 +0100 Subject: [PATCH] fix: strip CacheControl from messages during compaction When compaction runs, the original session's system messages carry CacheControl=true markers. These were preserved through BuildPrompt into the summarization session, which then added its own markers plus applyMessageCacheControl's 2 markers, exceeding Anthropic's limit of 4 cache control breakpoints. Assisted-By: docker-agent --- pkg/compaction/compaction.go | 1 + pkg/compaction/compaction_test.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/pkg/compaction/compaction.go b/pkg/compaction/compaction.go index d2af35d99..23367ac9b 100644 --- a/pkg/compaction/compaction.go +++ b/pkg/compaction/compaction.go @@ -65,6 +65,7 @@ func BuildPrompt(messages []chat.Message, additionalPrompt string) []chat.Messag for i, msg := range messages { cloned := msg cloned.Cost = 0 + cloned.CacheControl = false out[i] = cloned } out = append(out, chat.Message{ diff --git a/pkg/compaction/compaction_test.go b/pkg/compaction/compaction_test.go index 37103aa39..7dde3b155 100644 --- a/pkg/compaction/compaction_test.go +++ b/pkg/compaction/compaction_test.go @@ -268,6 +268,26 @@ func TestBuildPrompt(t *testing.T) { assert.InDelta(t, 0.05, original[0].Cost, 1e-9) assert.Len(t, original, 1) }) + + t.Run("strips CacheControl from cloned messages", func(t *testing.T) { + t.Parallel() + + input := []chat.Message{ + {Role: chat.MessageRoleSystem, Content: "system", CacheControl: true}, + {Role: chat.MessageRoleSystem, Content: "context", CacheControl: true}, + {Role: chat.MessageRoleUser, Content: "hello"}, + } + + out := BuildPrompt(input, "") + + // All cloned messages should have CacheControl=false + for i, msg := range out { + assert.False(t, msg.CacheControl, "message %d should have CacheControl stripped", i) + } + // Original should be unchanged + assert.True(t, input[0].CacheControl) + assert.True(t, input[1].CacheControl) + }) } func TestPromptsAreEmbedded(t *testing.T) {