fix: read anthropic 1m flag from live model cache state

This commit is contained in:
YeonGyu-Kim
2026-02-17 10:45:48 +09:00
parent d979bd945a
commit 21144ed1e3
12 changed files with 64 additions and 38 deletions
+6 -2
View File
@@ -249,7 +249,9 @@ describe("context-window-monitor", () => {
it("should use 1M limit when model cache flag is enabled", async () => {
//#given
const hook = createContextWindowMonitorHook(ctx as never, true)
const hook = createContextWindowMonitorHook(ctx as never, {
anthropicContext1MEnabled: true,
})
const sessionID = "ses_1m_flag"
await hook.event({
@@ -286,7 +288,9 @@ describe("context-window-monitor", () => {
it("should keep env var fallback when model cache flag is disabled", async () => {
//#given
process.env[ANTHROPIC_CONTEXT_ENV_KEY] = "true"
const hook = createContextWindowMonitorHook(ctx as never, false)
const hook = createContextWindowMonitorHook(ctx as never, {
anthropicContext1MEnabled: false,
})
const sessionID = "ses_env_fallback"
await hook.event({
+8 -4
View File
@@ -5,8 +5,12 @@ const ANTHROPIC_DISPLAY_LIMIT = 1_000_000
const DEFAULT_ANTHROPIC_ACTUAL_LIMIT = 200_000
const CONTEXT_WARNING_THRESHOLD = 0.70
function getAnthropicActualLimit(anthropicContext1MEnabled: boolean): number {
return anthropicContext1MEnabled ||
type ModelCacheStateLike = {
anthropicContext1MEnabled: boolean
}
function getAnthropicActualLimit(modelCacheState?: ModelCacheStateLike): number {
return (modelCacheState?.anthropicContext1MEnabled ?? false) ||
process.env.ANTHROPIC_1M_CONTEXT === "true" ||
process.env.VERTEX_ANTHROPIC_1M_CONTEXT === "true"
? 1_000_000
@@ -37,7 +41,7 @@ function isAnthropicProvider(providerID: string): boolean {
export function createContextWindowMonitorHook(
_ctx: PluginInput,
anthropicContext1MEnabled = false,
modelCacheState?: ModelCacheStateLike,
) {
const remindedSessions = new Set<string>()
const tokenCache = new Map<string, CachedTokenState>()
@@ -59,7 +63,7 @@ export function createContextWindowMonitorHook(
const totalInputTokens = (lastTokens?.input ?? 0) + (lastTokens?.cache?.read ?? 0)
const actualUsagePercentage =
totalInputTokens / getAnthropicActualLimit(anthropicContext1MEnabled)
totalInputTokens / getAnthropicActualLimit(modelCacheState)
if (actualUsagePercentage < CONTEXT_WARNING_THRESHOLD) return
+2 -2
View File
@@ -29,10 +29,10 @@ interface EventInput {
export function createDirectoryAgentsInjectorHook(
ctx: PluginInput,
anthropicContext1MEnabled?: boolean,
modelCacheState?: { anthropicContext1MEnabled: boolean },
) {
const sessionCaches = new Map<string, Set<string>>();
const truncator = createDynamicTruncator(ctx, anthropicContext1MEnabled);
const truncator = createDynamicTruncator(ctx, modelCacheState);
const toolExecuteAfter = async (input: ToolExecuteInput, output: ToolExecuteOutput) => {
const toolName = input.tool.toLowerCase();
+2 -2
View File
@@ -29,10 +29,10 @@ interface EventInput {
export function createDirectoryReadmeInjectorHook(
ctx: PluginInput,
anthropicContext1MEnabled?: boolean,
modelCacheState?: { anthropicContext1MEnabled: boolean },
) {
const sessionCaches = new Map<string, Set<string>>();
const truncator = createDynamicTruncator(ctx, anthropicContext1MEnabled);
const truncator = createDynamicTruncator(ctx, modelCacheState);
const toolExecuteAfter = async (input: ToolExecuteInput, output: ToolExecuteOutput) => {
const toolName = input.tool.toLowerCase();
+6 -2
View File
@@ -269,7 +269,9 @@ describe("preemptive-compaction", () => {
it("should use 1M limit when model cache flag is enabled", async () => {
//#given
const hook = createPreemptiveCompactionHook(ctx as never, true)
const hook = createPreemptiveCompactionHook(ctx as never, {
anthropicContext1MEnabled: true,
})
const sessionID = "ses_1m_flag"
await hook.event({
@@ -306,7 +308,9 @@ describe("preemptive-compaction", () => {
it("should keep env var fallback when model cache flag is disabled", async () => {
//#given
process.env[ANTHROPIC_CONTEXT_ENV_KEY] = "true"
const hook = createPreemptiveCompactionHook(ctx as never, false)
const hook = createPreemptiveCompactionHook(ctx as never, {
anthropicContext1MEnabled: false,
})
const sessionID = "ses_env_fallback"
await hook.event({
+8 -4
View File
@@ -2,8 +2,12 @@ import { log } from "../shared/logger"
const DEFAULT_ACTUAL_LIMIT = 200_000
function getAnthropicActualLimit(anthropicContext1MEnabled: boolean): number {
return anthropicContext1MEnabled ||
type ModelCacheStateLike = {
anthropicContext1MEnabled: boolean
}
function getAnthropicActualLimit(modelCacheState?: ModelCacheStateLike): number {
return (modelCacheState?.anthropicContext1MEnabled ?? false) ||
process.env.ANTHROPIC_1M_CONTEXT === "true" ||
process.env.VERTEX_ANTHROPIC_1M_CONTEXT === "true"
? 1_000_000
@@ -47,7 +51,7 @@ type PluginInput = {
export function createPreemptiveCompactionHook(
ctx: PluginInput,
anthropicContext1MEnabled = false,
modelCacheState?: ModelCacheStateLike,
) {
const compactionInProgress = new Set<string>()
const compactedSessions = new Set<string>()
@@ -65,7 +69,7 @@ export function createPreemptiveCompactionHook(
const actualLimit =
isAnthropicProvider(cached.providerID)
? getAnthropicActualLimit(anthropicContext1MEnabled)
? getAnthropicActualLimit(modelCacheState)
: DEFAULT_ACTUAL_LIMIT
const lastTokens = cached.tokens
+2 -2
View File
@@ -31,9 +31,9 @@ const TRACKED_TOOLS = ["read", "write", "edit", "multiedit"];
export function createRulesInjectorHook(
ctx: PluginInput,
anthropicContext1MEnabled?: boolean,
modelCacheState?: { anthropicContext1MEnabled: boolean },
) {
const truncator = createDynamicTruncator(ctx, anthropicContext1MEnabled);
const truncator = createDynamicTruncator(ctx, modelCacheState);
const { getSessionCache, clearSessionCache } = createSessionCacheStore();
const { processFilePathForInjection } = createRuleInjectionProcessor({
workspaceDirectory: ctx.directory,
+2 -2
View File
@@ -27,12 +27,12 @@ const TOOL_SPECIFIC_MAX_TOKENS: Record<string, number> = {
}
interface ToolOutputTruncatorOptions {
anthropicContext1MEnabled?: boolean
modelCacheState?: { anthropicContext1MEnabled: boolean }
experimental?: ExperimentalConfig
}
export function createToolOutputTruncatorHook(ctx: PluginInput, options?: ToolOutputTruncatorOptions) {
const truncator = createDynamicTruncator(ctx, options?.anthropicContext1MEnabled)
const truncator = createDynamicTruncator(ctx, options?.modelCacheState)
const truncateAll = options?.experimental?.truncate_all_tool_outputs ?? false
const toolExecuteAfter = async (