Skip to content

Commit c752d15

Browse files
fix(core): stop duplicating the boundary message in compaction head
select() partitions the conversation into head (summarized) and recent (kept) at a token budget. When the boundary lands mid-message, that message is split into splitPrefix (head) and splitSuffix (recent). The split branch set split = index + 1 and reused it for both head's slice end and recent's slice start, so head = slice(0, index + 1) included the full boundary message AND splitPrefix (a truncated copy) — duplicating it. head is the exact text fed to the summarizer, so on every overflow-triggered compaction where the boundary lands mid-message the boundary message was sent twice (full + truncated), wasting tokens and risking pushing the summary prompt past the context limit so compaction silently fails. Track two boundaries: headEnd (end of head's full-message slice) and recentStart (start of recent's slice). They differ only in the split case (headEnd = index, recentStart = index + 1); all other paths keep them equal, preserving existing behavior. Export select and add a regression test for the duplication plus a fully-fitting sanity case. Closes anomalyco#33329
1 parent 90f0576 commit c752d15

2 files changed

Lines changed: 60 additions & 6 deletions

File tree

‎packages/core/src/session/compaction.ts‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ const settings = (documents: readonly Config.Entry[]) => {
130130
)
131131
}
132132

133-
const select = (
133+
export const select = (
134134
entries: readonly Entry[],
135135
tokens: number,
136136
): { readonly head: string; readonly recent: string } | undefined => {
@@ -140,7 +140,12 @@ const select = (
140140
.filter(Boolean)
141141
if (conversation.length === 0) return
142142
let total = 0
143-
let split = conversation.length
143+
// head ends at headEnd (exclusive); recent starts at recentStart. They differ
144+
// only when the budget boundary lands mid-message: that message is split into
145+
// splitPrefix (head) and splitSuffix (recent), so the full message must NOT
146+
// also appear in head's slice — head ends at `index`, recent starts at `index + 1`.
147+
let headEnd = conversation.length
148+
let recentStart = conversation.length
144149
let splitPrefix = ""
145150
let splitSuffix = ""
146151
for (let index = conversation.length - 1; index >= 0; index--) {
@@ -150,16 +155,22 @@ const select = (
150155
if (remaining > 0) {
151156
splitPrefix = conversation[index].slice(0, -remaining)
152157
splitSuffix = conversation[index].slice(-remaining)
153-
split = index + 1
158+
headEnd = index
159+
recentStart = index + 1
160+
} else {
161+
// No room even for a suffix: the boundary message goes entirely to head.
162+
headEnd = index + 1
163+
recentStart = index + 1
154164
}
155165
break
156166
}
157167
total = next
158-
split = index
168+
headEnd = index
169+
recentStart = index
159170
}
160171
return {
161-
head: [...conversation.slice(0, split), splitPrefix].filter(Boolean).join("\n\n"),
162-
recent: [splitSuffix, ...conversation.slice(split)].filter(Boolean).join("\n\n"),
172+
head: [...conversation.slice(0, headEnd), splitPrefix].filter(Boolean).join("\n\n"),
173+
recent: [splitSuffix, ...conversation.slice(recentStart)].filter(Boolean).join("\n\n"),
163174
}
164175
}
165176

‎packages/core/test/session-compaction.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { expect, test } from "bun:test"
2+
import { DateTime } from "effect"
23
import { SessionCompaction } from "@opencode-ai/core/session/compaction"
4+
import { SessionMessage } from "@opencode-ai/core/session/message"
35

46
test("compaction describes tool media without embedding base64", () => {
57
const base64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB"
@@ -16,3 +18,44 @@ test("compaction describes tool media without embedding base64", () => {
1618
expect(serialized).toBe("Image read successfully\n[Attached image/png: pixel.png]")
1719
expect(serialized).not.toContain(base64)
1820
})
21+
22+
const created = DateTime.makeUnsafe(0)
23+
const userEntry = (seq: number, text: string) => ({
24+
seq,
25+
message: new SessionMessage.User({
26+
id: SessionMessage.ID.make(`msg_${seq}`),
27+
type: "user",
28+
text,
29+
time: { created },
30+
}),
31+
})
32+
33+
test("select does not duplicate the boundary message into head", () => {
34+
// serialize() renders a user message as `[User]: <text>`; Token.estimate is
35+
// round(length / 4). With tokens=5: the small message (estimate 3) fits, then
36+
// the big message overflows and is split mid-message. The boundary message
37+
// must appear in head ONLY as the truncated prefix — never in full.
38+
const big = "A".repeat(40)
39+
const entries = [userEntry(0, big), userEntry(1, "BBBB")]
40+
41+
const result = SessionCompaction.select(entries, 5)
42+
43+
expect(result).toBeDefined()
44+
// head is just the prefix slice, not the full boundary message + its prefix.
45+
expect(result!.head).toBe(`[User]: ${"A".repeat(32)}`)
46+
expect(result!.recent).toBe(`${"A".repeat(8)}\n\n[User]: BBBB`)
47+
// Regression guard for the duplication bug (head once contained the full
48+
// 40-char message AND its 32-char prefix copy):
49+
expect(result!.head).not.toContain("A".repeat(40))
50+
expect(result!.head.split("[User]:").length - 1).toBe(1)
51+
})
52+
53+
test("select keeps everything in recent when the whole conversation fits", () => {
54+
const entries = [userEntry(0, "first"), userEntry(1, "second")]
55+
56+
const result = SessionCompaction.select(entries, 1000)
57+
58+
expect(result).toBeDefined()
59+
expect(result!.head).toBe("")
60+
expect(result!.recent).toBe("[User]: first\n\n[User]: second")
61+
})

0 commit comments

Comments
 (0)