Skip to content

Commit ed06de5

Browse files
committed
core: add configurable compaction settings to allow users to disable auto-compaction and pruning via config instead of flags
1 parent 52b9962 commit ed06de5

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

packages/opencode/src/config/config.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ export namespace Config {
141141

142142
if (!result.keybinds) result.keybinds = Info.shape.keybinds.parse({})
143143

144+
// Apply flag overrides for compaction settings
145+
if (Flag.OPENCODE_DISABLE_AUTOCOMPACT) {
146+
result.compaction = { ...result.compaction, auto: false }
147+
}
148+
if (Flag.OPENCODE_DISABLE_PRUNE) {
149+
result.compaction = { ...result.compaction, prune: false }
150+
}
151+
144152
return {
145153
config: result,
146154
directories,
@@ -791,6 +799,12 @@ export namespace Config {
791799
url: z.string().optional().describe("Enterprise URL"),
792800
})
793801
.optional(),
802+
compaction: z
803+
.object({
804+
auto: z.boolean().optional().describe("Enable automatic compaction when context is full (default: true)"),
805+
prune: z.boolean().optional().describe("Enable pruning of old tool outputs (default: true)"),
806+
})
807+
.optional(),
794808
experimental: z
795809
.object({
796810
hook: z

packages/opencode/src/session/compaction.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import { Provider } from "../provider/provider"
77
import { MessageV2 } from "./message-v2"
88
import z from "zod"
99
import { SessionPrompt } from "./prompt"
10-
import { Flag } from "../flag/flag"
1110
import { Token } from "../util/token"
1211
import { Log } from "../util/log"
1312
import { SessionProcessor } from "./processor"
1413
import { fn } from "@/util/fn"
1514
import { Agent } from "@/agent/agent"
1615
import { Plugin } from "@/plugin"
16+
import { Config } from "@/config/config"
1717

1818
export namespace SessionCompaction {
1919
const log = Log.create({ service: "session.compaction" })
@@ -27,8 +27,9 @@ export namespace SessionCompaction {
2727
),
2828
}
2929

30-
export function isOverflow(input: { tokens: MessageV2.Assistant["tokens"]; model: Provider.Model }) {
31-
if (Flag.OPENCODE_DISABLE_AUTOCOMPACT) return false
30+
export async function isOverflow(input: { tokens: MessageV2.Assistant["tokens"]; model: Provider.Model }) {
31+
const config = await Config.get()
32+
if ((config.compaction?.auto ?? true) === false) return false
3233
const context = input.model.limit.context
3334
if (context === 0) return false
3435
const count = input.tokens.input + input.tokens.cache.read + input.tokens.output
@@ -46,7 +47,8 @@ export namespace SessionCompaction {
4647
// calls. then erases output of previous tool calls. idea is to throw away old
4748
// tool calls that are no longer relevant.
4849
export async function prune(input: { sessionID: string }) {
49-
if (Flag.OPENCODE_DISABLE_PRUNE) return
50+
const config = await Config.get()
51+
if ((config.compaction?.prune ?? true) === false) return
5052
log.info("pruning")
5153
const msgs = await Session.messages({ sessionID: input.sessionID })
5254
let total = 0

packages/opencode/src/session/prompt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ export namespace SessionPrompt {
459459
if (
460460
lastFinished &&
461461
lastFinished.summary !== true &&
462-
SessionCompaction.isOverflow({ tokens: lastFinished.tokens, model })
462+
(await SessionCompaction.isOverflow({ tokens: lastFinished.tokens, model }))
463463
) {
464464
await SessionCompaction.create({
465465
sessionID,

0 commit comments

Comments
 (0)