Skip to content

Commit 58df74f

Browse files
committed
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
1 parent 9318923 commit 58df74f

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

pkg/compaction/compaction.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func BuildPrompt(messages []chat.Message, additionalPrompt string) []chat.Messag
6565
for i, msg := range messages {
6666
cloned := msg
6767
cloned.Cost = 0
68+
cloned.CacheControl = false
6869
out[i] = cloned
6970
}
7071
out = append(out, chat.Message{

pkg/compaction/compaction_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,26 @@ func TestBuildPrompt(t *testing.T) {
268268
assert.InDelta(t, 0.05, original[0].Cost, 1e-9)
269269
assert.Len(t, original, 1)
270270
})
271+
272+
t.Run("strips CacheControl from cloned messages", func(t *testing.T) {
273+
t.Parallel()
274+
275+
input := []chat.Message{
276+
{Role: chat.MessageRoleSystem, Content: "system", CacheControl: true},
277+
{Role: chat.MessageRoleSystem, Content: "context", CacheControl: true},
278+
{Role: chat.MessageRoleUser, Content: "hello"},
279+
}
280+
281+
out := BuildPrompt(input, "")
282+
283+
// All cloned messages should have CacheControl=false
284+
for i, msg := range out {
285+
assert.False(t, msg.CacheControl, "message %d should have CacheControl stripped", i)
286+
}
287+
// Original should be unchanged
288+
assert.True(t, input[0].CacheControl)
289+
assert.True(t, input[1].CacheControl)
290+
})
271291
}
272292

273293
func TestPromptsAreEmbedded(t *testing.T) {

0 commit comments

Comments
 (0)