enable session pruning and allow disabling with OPENCODE_DISABLE_PRUNE

This commit is contained in:
Dax Raad
2025-09-16 04:52:34 -04:00
parent 52fcdcc37b
commit 3aeac02bf1
4 changed files with 46 additions and 30 deletions

View File

@@ -16,7 +16,6 @@ import { Log } from "../util/log"
import { MessageV2 } from "./message-v2"
import { Project } from "../project/project"
import { Instance } from "../project/instance"
import { Token } from "../util/token"
import { SessionPrompt } from "./prompt"
export namespace Session {
@@ -293,34 +292,6 @@ export namespace Session {
return part
}
// goes backwards through parts until there are 40_000 tokens worth of tool
// calls. then erases output of previous tool calls. idea is to throw away old
// tool calls that are no longer relevant.
export async function prune(input: { sessionID: string }) {
const msgs = await messages(input.sessionID)
let sum = 0
for (let msgIndex = msgs.length - 2; msgIndex >= 0; msgIndex--) {
const msg = msgs[msgIndex]
if (msg.info.role === "assistant" && msg.info.summary) return
for (let partIndex = msg.parts.length - 1; partIndex >= 0; partIndex--) {
const part = msg.parts[partIndex]
if (part.type === "tool")
if (part.state.status === "completed") {
if (part.state.time.compacted) return
sum += Token.estimate(part.state.output)
if (sum > 40_000) {
log.info("pruning", {
sum,
id: part.id,
})
part.state.time.compacted = Date.now()
await updatePart(part)
}
}
}
}
}
export function getUsage(model: ModelsDev.Model, usage: LanguageModelUsage, metadata?: ProviderMetadata) {
const tokens = {
input: usage.inputTokens ?? 0,