From 65db8b58846f95e97c412d67045c8e439bdf9409 Mon Sep 17 00:00:00 2001 From: Jonathan Correa Paiva Date: Thu, 5 Nov 2026 01:14:00 -0300 Subject: [PATCH] fix(config): default context limits to a share of the model window maxContextLimit/minContextLimit defaulted to 100000/50000 tokens: absolute numbers that only made sense for 128k windows. On models with larger windows the plugin compares the live token count against those fixed numbers, so a 1M-token model sitting at 250k tokens is reported as 250% of the limit. Every turn then injects "CRITICAL WARNING: MAX CONTEXT LIMIT REACHED" and pushes hard compression while three quarters of the window are still free. The percent form of both knobs already existed and is resolved against state.modelContextLimit, so only the default had to change: 70% / 35%. A 128k window resolves to ~90k / ~45k, keeping the behaviour the old numbers were written for, while large windows now scale instead of tripping early. When the model window is unknown the percent form resolves to undefined and the nudge fails open instead of nagging on a guessed budget. Schema, README and a regression test are updated to match. Per-model maxContextLimit/minContextLimit overrides, nudgeFrequency and iterationNudgeThreshold are untouched. Co-authored-by: Sinder --- README.md | 6 +- dcp.schema.json | 8 +- lib/config.ts | 4 +- tests/context-limit-defaults.test.ts | 134 +++++++++++++++++++++++++++ 4 files changed, 144 insertions(+), 8 deletions(-) create mode 100644 tests/context-limit-defaults.test.ts diff --git a/README.md b/README.md index d5b3ec87f..5a6d2ae30 100644 --- a/README.md +++ b/README.md @@ -130,11 +130,13 @@ Each level overrides the previous, so project settings take priority over global // Soft upper threshold: above this, DCP keeps injecting strong // compression nudges (based on nudgeFrequency), so compression is // much more likely. Accepts: number or "X%" of model context window. - "maxContextLimit": 100000, + // Defaults to 70% of the model window so large-context models are + // not nudged as if they were close to a fixed 100k limit. + "maxContextLimit": "70%", // Soft lower threshold for reminder nudges: below this, turn/iteration // reminders are off (compression less likely). At/above this, reminders // are on. Accepts: number or "X%" of model context window. - "minContextLimit": 50000, + "minContextLimit": "35%", // Optional per-model override for maxContextLimit by providerID/modelID. // If present, this wins over the global maxContextLimit. // Accepts: number or "X%". diff --git a/dcp.schema.json b/dcp.schema.json index 474d5babb..40e0d3daf 100644 --- a/dcp.schema.json +++ b/dcp.schema.json @@ -158,7 +158,7 @@ }, "maxContextLimit": { "description": "Soft upper threshold. Above this, DCP keeps sending strong compression nudges (based on nudgeFrequency), so the model is pushed to compress. Accepts number or \"X%\" of the model context window.", - "default": 100000, + "default": "70%", "oneOf": [ { "type": "number" @@ -171,7 +171,7 @@ }, "minContextLimit": { "description": "Soft lower threshold for reminder nudges. Below this, turn/iteration reminders are off (compression is less likely). At or above this, reminders are on. Accepts number or \"X%\" of the model context window.", - "default": 50000, + "default": "35%", "oneOf": [ { "type": "number" @@ -254,8 +254,8 @@ "permission": "allow", "showCompression": false, "summaryBuffer": true, - "maxContextLimit": 100000, - "minContextLimit": 50000, + "maxContextLimit": "70%", + "minContextLimit": "35%", "nudgeFrequency": 5, "iterationNudgeThreshold": 15, "nudgeForce": "soft", diff --git a/lib/config.ts b/lib/config.ts index 792b7ca3c..46ea43803 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -691,8 +691,8 @@ const defaultConfig: PluginConfig = { permission: "allow", showCompression: false, summaryBuffer: true, - maxContextLimit: 100000, - minContextLimit: 50000, + maxContextLimit: "70%", + minContextLimit: "35%", nudgeFrequency: 5, iterationNudgeThreshold: 15, nudgeForce: "soft", diff --git a/tests/context-limit-defaults.test.ts b/tests/context-limit-defaults.test.ts new file mode 100644 index 000000000..01ecf93e1 --- /dev/null +++ b/tests/context-limit-defaults.test.ts @@ -0,0 +1,134 @@ +import assert from "node:assert/strict" +import { readFileSync } from "node:fs" +import { join } from "node:path" +import test from "node:test" +import type { PluginConfig } from "../lib/config" +import { isContextOverLimits } from "../lib/messages/inject/utils" +import { createSessionState, type WithParts } from "../lib/state" + +// `lib/config` cannot be imported from tests (its `jsonc-parser` ESM subpath has no +// named exports under tsx), so the shipped defaults are asserted against the schema +// and the config source instead of against the runtime object. +const repoRoot = join(import.meta.dirname, "..") + +function percentageConfig(maxContextLimit: string, minContextLimit: string): PluginConfig { + return { + compress: { + mode: "range", + permission: "allow", + showCompression: false, + summaryBuffer: true, + maxContextLimit: maxContextLimit as `${number}%`, + minContextLimit: minContextLimit as `${number}%`, + nudgeFrequency: 5, + iterationNudgeThreshold: 15, + nudgeForce: "soft", + protectedTools: [], + protectTags: false, + protectUserMessages: false, + }, + } as PluginConfig +} + +function buildMessages(inputTokens: number): WithParts[] { + const sessionID = "ses_context_limit_defaults" + + return [ + { + info: { + id: "msg-user", + role: "user", + sessionID, + agent: "assistant", + time: { created: 1 }, + model: { providerID: "opencode-go", modelID: "big-window" }, + } as WithParts["info"], + parts: [ + { + id: "msg-user-part", + messageID: "msg-user", + sessionID, + type: "text" as const, + text: "continue please", + }, + ], + }, + { + info: { + id: "msg-assistant", + role: "assistant", + sessionID, + agent: "assistant", + time: { created: 2 }, + tokens: { + input: inputTokens, + output: 10, + reasoning: 0, + cache: { + read: 0, + write: 0, + }, + }, + } as WithParts["info"], + parts: [ + { + id: "msg-assistant-part", + messageID: "msg-assistant", + sessionID, + type: "text" as const, + text: "done", + }, + ], + }, + ] +} + +function limitsFor(modelContextLimit: number | undefined, tokens: number) { + const state = createSessionState("ses_context_limit_defaults") + state.modelContextLimit = modelContextLimit + return isContextOverLimits( + percentageConfig("70%", "35%"), + state, + "opencode-go", + "big-window", + buildMessages(tokens), + ) +} + +test("shipped default limits are percentages, not absolute token budgets", () => { + const schema = JSON.parse(readFileSync(join(repoRoot, "dcp.schema.json"), "utf-8")) + const compress = schema.properties.compress.properties + + assert.equal(compress.maxContextLimit.default, "70%") + assert.equal(compress.minContextLimit.default, "35%") + assert.equal(schema.properties.compress.default.maxContextLimit, "70%") + assert.equal(schema.properties.compress.default.minContextLimit, "35%") + + const configSource = readFileSync(join(repoRoot, "lib", "config.ts"), "utf-8") + assert.match(configSource, /maxContextLimit: "70%",/) + assert.match(configSource, /minContextLimit: "35%",/) +}) + +test("percentage defaults stay quiet well past 100k on a 1M window", () => { + const at250k = limitsFor(1_000_000, 250_000) + assert.equal(at250k.overMaxLimit, false) + assert.equal(at250k.overMinLimit, false) + + const at750k = limitsFor(1_000_000, 750_000) + assert.equal(at750k.overMaxLimit, true) + assert.equal(at750k.overMinLimit, true) +}) + +test("percentage defaults keep the historical thresholds on a 128k window", () => { + const at80k = limitsFor(128_000, 80_000) + assert.equal(at80k.overMaxLimit, false) + assert.equal(at80k.overMinLimit, true) + + const at95k = limitsFor(128_000, 95_000) + assert.equal(at95k.overMaxLimit, true) +}) + +test("percentage defaults fail open when the model window is unknown", () => { + const unknown = limitsFor(undefined, 500_000) + assert.equal(unknown.overMaxLimit, false) +})