fix: use model cache context flag for runtime context limits

This commit is contained in:
YeonGyu-Kim
2026-02-17 10:33:17 +09:00
parent b1e7bb4c59
commit b444899153
12 changed files with 94 additions and 39 deletions
+15 -7
View File
@@ -2,13 +2,17 @@ import type { PluginInput } from "@opencode-ai/plugin"
import { createSystemDirective, SystemDirectiveTypes } from "../shared/system-directive"
const ANTHROPIC_DISPLAY_LIMIT = 1_000_000
const ANTHROPIC_ACTUAL_LIMIT =
process.env.ANTHROPIC_1M_CONTEXT === "true" ||
process.env.VERTEX_ANTHROPIC_1M_CONTEXT === "true"
? 1_000_000
: 200_000
const DEFAULT_ANTHROPIC_ACTUAL_LIMIT = 200_000
const CONTEXT_WARNING_THRESHOLD = 0.70
function getAnthropicActualLimit(anthropicContext1MEnabled: boolean): number {
return anthropicContext1MEnabled ||
process.env.ANTHROPIC_1M_CONTEXT === "true" ||
process.env.VERTEX_ANTHROPIC_1M_CONTEXT === "true"
? 1_000_000
: DEFAULT_ANTHROPIC_ACTUAL_LIMIT
}
const CONTEXT_REMINDER = `${createSystemDirective(SystemDirectiveTypes.CONTEXT_WINDOW_MONITOR)}
You are using Anthropic Claude with 1M context window.
@@ -31,7 +35,10 @@ function isAnthropicProvider(providerID: string): boolean {
return providerID === "anthropic" || providerID === "google-vertex-anthropic"
}
export function createContextWindowMonitorHook(_ctx: PluginInput) {
export function createContextWindowMonitorHook(
_ctx: PluginInput,
anthropicContext1MEnabled = false,
) {
const remindedSessions = new Set<string>()
const tokenCache = new Map<string, CachedTokenState>()
@@ -51,7 +58,8 @@ export function createContextWindowMonitorHook(_ctx: PluginInput) {
const lastTokens = cached.tokens
const totalInputTokens = (lastTokens?.input ?? 0) + (lastTokens?.cache?.read ?? 0)
const actualUsagePercentage = totalInputTokens / ANTHROPIC_ACTUAL_LIMIT
const actualUsagePercentage =
totalInputTokens / getAnthropicActualLimit(anthropicContext1MEnabled)
if (actualUsagePercentage < CONTEXT_WARNING_THRESHOLD) return
+5 -2
View File
@@ -27,9 +27,12 @@ interface EventInput {
};
}
export function createDirectoryAgentsInjectorHook(ctx: PluginInput) {
export function createDirectoryAgentsInjectorHook(
ctx: PluginInput,
anthropicContext1MEnabled?: boolean,
) {
const sessionCaches = new Map<string, Set<string>>();
const truncator = createDynamicTruncator(ctx);
const truncator = createDynamicTruncator(ctx, anthropicContext1MEnabled);
const toolExecuteAfter = async (input: ToolExecuteInput, output: ToolExecuteOutput) => {
const toolName = input.tool.toLowerCase();
+5 -2
View File
@@ -27,9 +27,12 @@ interface EventInput {
};
}
export function createDirectoryReadmeInjectorHook(ctx: PluginInput) {
export function createDirectoryReadmeInjectorHook(
ctx: PluginInput,
anthropicContext1MEnabled?: boolean,
) {
const sessionCaches = new Map<string, Set<string>>();
const truncator = createDynamicTruncator(ctx);
const truncator = createDynamicTruncator(ctx, anthropicContext1MEnabled);
const toolExecuteAfter = async (input: ToolExecuteInput, output: ToolExecuteOutput) => {
const toolName = input.tool.toLowerCase();
+10 -5
View File
@@ -2,11 +2,13 @@ import { log } from "../shared/logger"
const DEFAULT_ACTUAL_LIMIT = 200_000
const ANTHROPIC_ACTUAL_LIMIT =
process.env.ANTHROPIC_1M_CONTEXT === "true" ||
process.env.VERTEX_ANTHROPIC_1M_CONTEXT === "true"
function getAnthropicActualLimit(anthropicContext1MEnabled: boolean): number {
return anthropicContext1MEnabled ||
process.env.ANTHROPIC_1M_CONTEXT === "true" ||
process.env.VERTEX_ANTHROPIC_1M_CONTEXT === "true"
? 1_000_000
: DEFAULT_ACTUAL_LIMIT
}
const PREEMPTIVE_COMPACTION_THRESHOLD = 0.78
@@ -43,7 +45,10 @@ type PluginInput = {
directory: string
}
export function createPreemptiveCompactionHook(ctx: PluginInput) {
export function createPreemptiveCompactionHook(
ctx: PluginInput,
anthropicContext1MEnabled = false,
) {
const compactionInProgress = new Set<string>()
const compactedSessions = new Set<string>()
const tokenCache = new Map<string, CachedCompactionState>()
@@ -60,7 +65,7 @@ export function createPreemptiveCompactionHook(ctx: PluginInput) {
const actualLimit =
isAnthropicProvider(cached.providerID)
? ANTHROPIC_ACTUAL_LIMIT
? getAnthropicActualLimit(anthropicContext1MEnabled)
: DEFAULT_ACTUAL_LIMIT
const lastTokens = cached.tokens
+5 -2
View File
@@ -29,8 +29,11 @@ interface EventInput {
const TRACKED_TOOLS = ["read", "write", "edit", "multiedit"];
export function createRulesInjectorHook(ctx: PluginInput) {
const truncator = createDynamicTruncator(ctx);
export function createRulesInjectorHook(
ctx: PluginInput,
anthropicContext1MEnabled?: boolean,
) {
const truncator = createDynamicTruncator(ctx, anthropicContext1MEnabled);
const { getSessionCache, clearSessionCache } = createSessionCacheStore();
const { processFilePathForInjection } = createRuleInjectionProcessor({
workspaceDirectory: ctx.directory,
+2 -1
View File
@@ -27,11 +27,12 @@ const TOOL_SPECIFIC_MAX_TOKENS: Record<string, number> = {
}
interface ToolOutputTruncatorOptions {
anthropicContext1MEnabled?: boolean
experimental?: ExperimentalConfig
}
export function createToolOutputTruncatorHook(ctx: PluginInput, options?: ToolOutputTruncatorOptions) {
const truncator = createDynamicTruncator(ctx)
const truncator = createDynamicTruncator(ctx, options?.anthropicContext1MEnabled)
const truncateAll = options?.experimental?.truncate_all_tool_outputs ?? false
const toolExecuteAfter = async (