59f0f06e71
- New context-limit-resolver.ts with resolveActualContextLimit() shared helper
- Anthropic provider detection now uses .includes('anthropic') instead of hard-coded IDs
- Both context-window-monitor and dynamic-truncator use the shared resolver
- Added missing test cases: Anthropic+1M disabled+cached limit, non-Anthropic without cache
185 lines
5.8 KiB
TypeScript
185 lines
5.8 KiB
TypeScript
/// <reference types="bun-types" />
|
|
|
|
import { describe, expect, it } from "bun:test"
|
|
import { createContextWindowMonitorHook } from "./context-window-monitor"
|
|
|
|
function createOutput() {
|
|
return { title: "", output: "original", metadata: null }
|
|
}
|
|
|
|
describe("context-window-monitor modelContextLimitsCache", () => {
|
|
it("does not append reminder below cached non-anthropic threshold", async () => {
|
|
// given
|
|
const modelContextLimitsCache = new Map<string, number>()
|
|
modelContextLimitsCache.set("opencode/kimi-k2.5-free", 262144)
|
|
|
|
const hook = createContextWindowMonitorHook({} as never, {
|
|
anthropicContext1MEnabled: false,
|
|
modelContextLimitsCache,
|
|
})
|
|
const sessionID = "ses_non_anthropic_below_threshold"
|
|
|
|
await hook.event({
|
|
event: {
|
|
type: "message.updated",
|
|
properties: {
|
|
info: {
|
|
role: "assistant",
|
|
sessionID,
|
|
providerID: "opencode",
|
|
modelID: "kimi-k2.5-free",
|
|
finish: true,
|
|
tokens: {
|
|
input: 150000,
|
|
output: 0,
|
|
reasoning: 0,
|
|
cache: { read: 10000, write: 0 },
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
// when
|
|
const output = createOutput()
|
|
await hook["tool.execute.after"]({ tool: "bash", sessionID, callID: "call_1" }, output)
|
|
|
|
// then
|
|
expect(output.output).toBe("original")
|
|
})
|
|
|
|
it("appends reminder above cached non-anthropic threshold", async () => {
|
|
// given
|
|
const modelContextLimitsCache = new Map<string, number>()
|
|
modelContextLimitsCache.set("opencode/kimi-k2.5-free", 262144)
|
|
|
|
const hook = createContextWindowMonitorHook({} as never, {
|
|
anthropicContext1MEnabled: false,
|
|
modelContextLimitsCache,
|
|
})
|
|
const sessionID = "ses_non_anthropic_above_threshold"
|
|
|
|
await hook.event({
|
|
event: {
|
|
type: "message.updated",
|
|
properties: {
|
|
info: {
|
|
role: "assistant",
|
|
sessionID,
|
|
providerID: "opencode",
|
|
modelID: "kimi-k2.5-free",
|
|
finish: true,
|
|
tokens: {
|
|
input: 180000,
|
|
output: 0,
|
|
reasoning: 0,
|
|
cache: { read: 10000, write: 0 },
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
// when
|
|
const output = createOutput()
|
|
await hook["tool.execute.after"]({ tool: "bash", sessionID, callID: "call_1" }, output)
|
|
|
|
// then
|
|
expect(output.output).toContain("context remaining")
|
|
expect(output.output).toContain("262,144-token context window")
|
|
expect(output.output).toContain("[Context Status: 72.5% used (190,000/262,144 tokens), 27.5% remaining]")
|
|
expect(output.output).not.toContain("1,000,000")
|
|
})
|
|
|
|
describe("#given Anthropic provider with cached context limit and 1M mode enabled", () => {
|
|
describe("#when cached usage would exceed 200K but stay below 1M", () => {
|
|
it("#then should ignore the cached limit and skip the reminder", async () => {
|
|
// given
|
|
const modelContextLimitsCache = new Map<string, number>()
|
|
modelContextLimitsCache.set("anthropic/claude-sonnet-4-5", 200000)
|
|
|
|
const hook = createContextWindowMonitorHook({} as never, {
|
|
anthropicContext1MEnabled: true,
|
|
modelContextLimitsCache,
|
|
})
|
|
const sessionID = "ses_anthropic_1m_overrides_cached_limit"
|
|
|
|
await hook.event({
|
|
event: {
|
|
type: "message.updated",
|
|
properties: {
|
|
info: {
|
|
role: "assistant",
|
|
sessionID,
|
|
providerID: "anthropic",
|
|
modelID: "claude-sonnet-4-5",
|
|
finish: true,
|
|
tokens: {
|
|
input: 300000,
|
|
output: 0,
|
|
reasoning: 0,
|
|
cache: { read: 0, write: 0 },
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
// when
|
|
const output = createOutput()
|
|
await hook["tool.execute.after"]({ tool: "bash", sessionID, callID: "call_1" }, output)
|
|
|
|
// then
|
|
expect(output.output).toBe("original")
|
|
})
|
|
})
|
|
})
|
|
|
|
describe("#given Anthropic provider with cached context limit and 1M mode disabled", () => {
|
|
describe("#when cached usage exceeds the Anthropic default limit", () => {
|
|
it("#then should ignore the cached limit and append the reminder from the default Anthropic limit", async () => {
|
|
// given
|
|
const modelContextLimitsCache = new Map<string, number>()
|
|
modelContextLimitsCache.set("anthropic/claude-sonnet-4-5", 500000)
|
|
|
|
const hook = createContextWindowMonitorHook({} as never, {
|
|
anthropicContext1MEnabled: false,
|
|
modelContextLimitsCache,
|
|
})
|
|
const sessionID = "ses_anthropic_default_overrides_cached_limit"
|
|
|
|
await hook.event({
|
|
event: {
|
|
type: "message.updated",
|
|
properties: {
|
|
info: {
|
|
role: "assistant",
|
|
sessionID,
|
|
providerID: "anthropic",
|
|
modelID: "claude-sonnet-4-5",
|
|
finish: true,
|
|
tokens: {
|
|
input: 150000,
|
|
output: 0,
|
|
reasoning: 0,
|
|
cache: { read: 10000, write: 0 },
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
// when
|
|
const output = createOutput()
|
|
await hook["tool.execute.after"]({ tool: "bash", sessionID, callID: "call_1" }, output)
|
|
|
|
// then
|
|
expect(output.output).toContain("context remaining")
|
|
expect(output.output).toContain("200,000-token context window")
|
|
expect(output.output).not.toContain("500,000-token context window")
|
|
expect(output.output).not.toContain("1,000,000-token context window")
|
|
})
|
|
})
|
|
})
|
|
})
|