Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/avoid-repeat-compactions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kilocode/cli": patch
---

Prevent unnecessary repeat auto-compactions when providers report inconsistent token totals.
6 changes: 6 additions & 0 deletions packages/opencode/src/kilocode/session/overflow.ts
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions packages/opencode/src/session/overflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions packages/opencode/test/kilocode/session-overflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
Loading