Skip to content

Commit e6347c6

Browse files
committed
fix(opencode): skip the overflow check after a trim and keep turn-end prune accounting
1 parent eb5c4c1 commit e6347c6

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ const layer = Layer.effect(
283283
const estimate = Token.estimate(part.state.output)
284284
total += estimate
285285
if (!trim && total <= PRUNE_PROTECT) continue
286-
pruned += estimate + attached * MEDIA_TOKEN_ESTIMATE
286+
pruned += estimate + (trim ? attached * MEDIA_TOKEN_ESTIMATE : 0)
287287
toPrune.push(part)
288288
}
289289
}

‎packages/opencode/src/session/prompt.ts‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,7 @@ const layer = Layer.effect(
10841084
const ctx = yield* InstanceState.context
10851085
let structured: unknown
10861086
let step = 0
1087+
let afterTrim = false
10871088
const session = yield* sessions.get(sessionID).pipe(Effect.orDie)
10881089

10891090
while (true) {
@@ -1143,6 +1144,17 @@ const layer = Layer.effect(
11431144
break
11441145
}
11451146

1147+
// A trim frees context the last finished message's usage does not show yet, so the next pass skips the
1148+
// overflow check instead of compacting on stale numbers.
1149+
const trimmed =
1150+
lastFinished && lastFinished.summary !== true
1151+
? yield* compaction.prune({ sessionID, tokens: tokenCount(lastFinished.tokens) })
1152+
: false
1153+
if (trimmed) {
1154+
afterTrim = true
1155+
continue
1156+
}
1157+
11461158
step++
11471159
if (step === 1)
11481160
yield* title({
@@ -1175,18 +1187,13 @@ const layer = Layer.effect(
11751187
if (
11761188
lastFinished &&
11771189
lastFinished.summary !== true &&
1178-
(yield* compaction.prune({ sessionID, tokens: tokenCount(lastFinished.tokens) }))
1179-
)
1180-
continue
1181-
1182-
if (
1183-
lastFinished &&
1184-
lastFinished.summary !== true &&
1190+
!afterTrim &&
11851191
(yield* compaction.isOverflow({ tokens: lastFinished.tokens, model }))
11861192
) {
11871193
yield* compaction.create({ sessionID, agent: lastUser.agent, model: lastUser.model, auto: true })
11881194
continue
11891195
}
1196+
afterTrim = false
11901197

11911198
const agent = yield* agents.get(lastUser.agent)
11921199
if (!agent) {

0 commit comments

Comments
 (0)