fix(cli): avoid repeat compaction from stale totals

This commit is contained in:
Catriel Müller
2026-06-04 10:05:35 -03:00
parent 5637375874
commit bc3af9a145
4 changed files with 33 additions and 2 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@kilocode/cli": patch
---
Prevent unnecessary repeat auto-compactions when providers report inconsistent token totals.
@@ -1,5 +1,6 @@
import type { Config } from "@/config/config"
import type { Provider } from "@/provider/provider"
import type { MessageV2 } from "@/session/message-v2"
import { Token } from "@/util/token"
import type { ModelMessage } from "ai"
@@ -26,6 +27,11 @@ export namespace KiloSessionOverflow {
}
}
export function count(tokens: MessageV2.Assistant["tokens"]) {
const total = tokens.input + tokens.output + tokens.reasoning + tokens.cache.read + tokens.cache.write
return total || tokens.total || 0
}
export function limit(input: { cfg: Config.Info; model: Provider.Model; usable: number }) {
const percent = input.cfg.compaction?.threshold_percent
if (typeof percent !== "number") return input.usable
+1 -2
View File
@@ -21,8 +21,7 @@ export function isOverflow(input: { cfg: Config.Info; tokens: MessageV2.Assistan
if (input.cfg.compaction?.auto === false) return false
if (input.model.limit.context === 0) return false
const count =
input.tokens.total || input.tokens.input + input.tokens.output + input.tokens.cache.read + input.tokens.cache.write
const count = KiloSessionOverflow.count(input.tokens) // kilocode_change
// kilocode_change start
const cap = KiloSessionOverflow.limit({ cfg: input.cfg, model: input.model, usable: usable(input) })
return count >= cap
@@ -86,6 +86,27 @@ describe("Kilo auto-compaction threshold", () => {
expect(isOverflow({ cfg: conf, model: mdl, tokens: tokens(150_000) })).toBe(false)
})
test("uses normalized fields when the provider total disagrees", () => {
const conf = cfg({ threshold_percent: 75 })
const mdl = model({ context: 200_000, output: 32_000 })
expect(isOverflow({ cfg: conf, model: mdl, tokens: { ...tokens(80_000), total: 250_000 } })).toBe(false)
})
test("counts reasoning tokens", () => {
const conf = cfg({ threshold_percent: 75 })
const mdl = model({ context: 200_000, output: 32_000 })
expect(isOverflow({ cfg: conf, model: mdl, tokens: { ...tokens(149_999), reasoning: 1 } })).toBe(true)
})
test("falls back to provider total when normalized usage is unavailable", () => {
const conf = cfg({ threshold_percent: 75 })
const mdl = model({ context: 200_000, output: 32_000 })
expect(isOverflow({ cfg: conf, model: mdl, tokens: { ...tokens(0), total: 150_000 } })).toBe(true)
})
})
describe("Kilo request estimation", () => {