fix(session): check for context overflow mid-turn in finish-step (#6480)

This commit is contained in:
Saatvik Arya 2026-01-01 23:33:18 +05:30 committed by GitHub
parent 3b03324578
commit 7a3ff5b98f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 267 additions and 0 deletions

View file

@ -13,6 +13,7 @@ import { Plugin } from "@/plugin"
import type { Provider } from "@/provider/provider"
import { LLM } from "./llm"
import { Config } from "@/config/config"
import { SessionCompaction } from "./compaction"
export namespace SessionProcessor {
const DOOM_LOOP_THRESHOLD = 3
@ -31,6 +32,7 @@ export namespace SessionProcessor {
let snapshot: string | undefined
let blocked = false
let attempt = 0
let needsCompaction = false
const result = {
get message() {
@ -41,6 +43,7 @@ export namespace SessionProcessor {
},
async process(streamInput: LLM.StreamInput) {
log.info("process")
needsCompaction = false
const shouldBreak = (await Config.get()).experimental?.continue_loop_on_deny !== true
while (true) {
try {
@ -279,6 +282,9 @@ export namespace SessionProcessor {
sessionID: input.sessionID,
messageID: input.assistantMessage.parentID,
})
if (await SessionCompaction.isOverflow({ tokens: usage.tokens, model: input.model })) {
needsCompaction = true
}
break
case "text-start":
@ -339,6 +345,7 @@ export namespace SessionProcessor {
})
continue
}
if (needsCompaction) break
}
} catch (e: any) {
log.error("process", {
@ -398,6 +405,7 @@ export namespace SessionProcessor {
}
input.assistantMessage.time.completed = Date.now()
await Session.updateMessage(input.assistantMessage)
if (needsCompaction) return "compact"
if (blocked) return "stop"
if (input.assistantMessage.error) return "stop"
return "continue"

View file

@ -549,6 +549,14 @@ export namespace SessionPrompt {
model,
})
if (result === "stop") break
if (result === "compact") {
await SessionCompaction.create({
sessionID,
agent: lastUser.agent,
model: lastUser.model,
auto: true,
})
}
continue
}
SessionCompaction.prune({ sessionID })