summarylogtreecommitdiffstats
path: root/0002-fix-compaction-buffer-overflow-for-small-context-win.patch
blob: d5788d70e48ef02e1823cf74e0573792029f535b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
From 530eb8da89418b5c216e69c68eaf7c1fca771902 Mon Sep 17 00:00:00 2001
From: Ewout van Mansom <ewout@vanmansom.name>
Date: Sat, 11 Apr 2026 22:07:54 +0200
Subject: [PATCH 2/4] fix: compaction buffer overflow for small context windows

Prevent compaction from failing when the model's context window is too
small to fit the buffer margin, causing infinite compaction loops.
---
 packages/opencode/src/session/overflow.ts | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/packages/opencode/src/session/overflow.ts b/packages/opencode/src/session/overflow.ts
index f0e52565d..a4a090b3b 100644
--- a/packages/opencode/src/session/overflow.ts
+++ b/packages/opencode/src/session/overflow.ts
@@ -13,8 +13,9 @@ export function isOverflow(input: { cfg: Config.Info; tokens: MessageV2.Assistan
   const count =
     input.tokens.total || input.tokens.input + input.tokens.output + input.tokens.cache.read + input.tokens.cache.write
 
+  const scaledBuffer = Math.min(COMPACTION_BUFFER, Math.floor(context * 0.3))
   const reserved =
-    input.cfg.compaction?.reserved ?? Math.min(COMPACTION_BUFFER, ProviderTransform.maxOutputTokens(input.model))
+    input.cfg.compaction?.reserved ?? Math.min(scaledBuffer, ProviderTransform.maxOutputTokens(input.model))
   const usable = input.model.limit.input
     ? input.model.limit.input - reserved
     : context - ProviderTransform.maxOutputTokens(input.model)
-- 
2.53.0