diff --git a/docs/configuration.ja.md b/docs/configuration.ja.md index 215a6ffc3e..03919ba9d6 100644 --- a/docs/configuration.ja.md +++ b/docs/configuration.ja.md @@ -278,6 +278,7 @@ |-----|------|---------|-----|-------------| | `compress.modelContextLimit` | number \| "N%" | native window (model-declared) | — | 有効コンテキストウィンドウ(トークン数または N%):使用率の分母であり、事前チェックのハードウォール。最優先のウィンドウソース。 | | `compress.maxContextLimit` | number \| "N%" | "75%" | — | 強制圧縮のナッジ閾値:履歴がこのウィンドウの割合を超えると、ナッジが即座に発火し、成長ゲートをバイパスする。ハードキャップではない。 | +| `compress.nudgeEscalationPct` | number \| "N%" | 0.7 | — | ホスト側のエスカレーション・ナッジライン:使用率がこの比率を超えた時点で、カーネルの周期が沈黙している間でも圧縮ナッジを注入する(maxContextLimit 未満の上昇をカバー)。ホスト専用;maxContextLimit 以上で設定した場合、カーネルの圧力帯が先に引き継ぐ。 | | `compress.emergencyThresholdPercent` | number \| % | "95%" | — | 履歴がこのウィンドウの割合を超えた場合の、過大ツール出力の緊急切り詰め(maxContextLimit 以上である必要がある)。 | | `compress.outputHeadroomMaxPct` | number \| % | 0.25 | — | max_tokens 経由で出力に確保されるウィンドウの割合の上限。 | | `compress.nudgeGrowthTokens` | number | 50000 (kernel flat cadence) | — | 成長ゲート:折りたたみ可能な範囲がベースライン成長をこのトークン数だけ上回った場合のみ、ナッジが発火する(設計上フラットであり、ウィンドウサイズに依存しない)。 | diff --git a/docs/configuration.md b/docs/configuration.md index d2bddf9df9..9002a0fe8e 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -281,6 +281,7 @@ This index is generated from `website/config-reference/*.yaml` — edit the seed |-----|------|---------|-----|-------------| | `compress.modelContextLimit` | number \| "N%" | native window (model-declared) | — | Effective context window (tokens or N%): denominator of the usage ratio and a hard preflight wall; highest-priority window source. | | `compress.maxContextLimit` | number \| "N%" | "75%" | — | Forced-compression nudge threshold: once history passes this share of the window the nudge fires immediately, bypassing growth gates. Not a hard cap. | +| `compress.nudgeEscalationPct` | number \| "N%" | 0.7 | — | Host-side escalation-nudge line: injects a compression nudge once usage crosses this ratio even while the kernel cadence stays silent (covers the climb below maxContextLimit). Host-only; if set at/above maxContextLimit the kernel pressure band takes over first. | | `compress.emergencyThresholdPercent` | number \| % | "95%" | — | Emergency truncation of oversized tool outputs when history passes this share of the window (must be >= maxContextLimit). | | `compress.outputHeadroomMaxPct` | number \| % | 0.25 | — | Cap on the share of the window reserved for output via max_tokens. | | `compress.nudgeGrowthTokens` | number | 50000 (kernel flat cadence) | — | Growth gate: nudges fire only when a foldable range exceeds baseline growth by this many tokens (flat by design, independent of window size). | diff --git a/docs/configuration.zh-CN.md b/docs/configuration.zh-CN.md index 3ad7b20267..b126364350 100644 --- a/docs/configuration.zh-CN.md +++ b/docs/configuration.zh-CN.md @@ -281,6 +281,7 @@ |-----|------|---------|-----|-------------| | `compress.modelContextLimit` | number \| "N%" | native window (model-declared) | — | 有效上下文窗口(token 数或 N%):使用率的分母,也是硬性 preflight 墙;窗口来源中优先级最高。 | | `compress.maxContextLimit` | number \| "N%" | "75%" | — | 强制压缩提醒阈值:历史超过窗口该占比时提醒立即触发,绕过增长门槛。不是硬上限。 | +| `compress.nudgeEscalationPct` | number \| "N%" | 0.7 | — | 宿主侧升级提醒线:使用率越过该比例时,即使内核节奏仍静默也注入一次压缩提醒(覆盖 maxContextLimit 之下的爬升段)。仅宿主侧;若设到 >= maxContextLimit,内核压力带会先触发。 | | `compress.emergencyThresholdPercent` | number \| % | "95%" | — | 历史超过窗口该占比时对超大工具输出做紧急截断(必须 >= maxContextLimit)。 | | `compress.outputHeadroomMaxPct` | number \| % | 0.25 | — | max_tokens 输出预留占窗口的最大比例。 | | `compress.nudgeGrowthTokens` | number | 50000 (kernel flat cadence) | — | 增长门槛:可折叠片段超出基线增长达到该 token 数才发提醒(按设计恒定,与窗口大小无关)。 | diff --git a/src/compress-settings.ts b/src/compress-settings.ts index 687f10f32d..6b9357f2ca 100644 --- a/src/compress-settings.ts +++ b/src/compress-settings.ts @@ -71,6 +71,7 @@ export function mergeCompress( modelContextLimit: pick("modelContextLimit"), outputHeadroomMaxPct: pick("outputHeadroomMaxPct"), maxContextLimit: pick("maxContextLimit"), + nudgeEscalationPct: pick("nudgeEscalationPct"), emergencyThresholdPercent: pick("emergencyThresholdPercent"), nudgeGrowthTokens: pick("nudgeGrowthTokens"), minPressureBenefitTokens: pick("minPressureBenefitTokens"), @@ -297,7 +298,7 @@ export function hasCompressSettings(s: CompressSettings): boolean { // built and typed here regardless of the installed kernel's Config so both // release windows compile identically. Older kernels ignore the extra key // (validateConfig does not flag unknown keys). -export type ResolvedKernelConfig = Config & { preserveRecentTools?: string[]; externalSummary?: ExternalSummarySettings }; +export type ResolvedKernelConfig = Config & { preserveRecentTools?: string[]; externalSummary?: ExternalSummarySettings; nudgeEscalationPct?: number }; // Single source of truth for raw->resolved absorb: applyCompressSettings and the // plugin-lane base stamp (#1359) both go through this or they drift apart. @@ -399,6 +400,11 @@ export function applyCompressSettings(base: Config, limit: number, s: CompressSe // HERE, at resolve time — the executor still receives a fully // expanded ExternalSummarySettings and never sees references. externalSummary: s.externalSummary !== undefined ? expandExternalSummaryChainTolerant(s.externalSummary, recipes) : (base as ResolvedKernelConfig).externalSummary, + // Host-only escalation-nudge line (#5230): never passed to the kernel. + // Deepest-wins via mergeCompress; unset inherits base unchanged so a + // re-resolve without the field keeps its prior value; unset everywhere + // leaves it absent → budget.ts falls back to the 0.7 default. + nudgeEscalationPct: s.nudgeEscalationPct !== undefined ? parsePercent(s.nudgeEscalationPct) : (base as ResolvedKernelConfig).nudgeEscalationPct, ...(absorb !== undefined ? { absorb } : {}), // #5765: one-switch opt-in — absent `s.blockOps` leaves base untouched // (off); a present block normalizes enabled (absent/false = off). diff --git a/src/config.ts b/src/config.ts index ef473e7a98..1743aa0604 100644 --- a/src/config.ts +++ b/src/config.ts @@ -242,6 +242,15 @@ export type CompressSettings = { * (bypasses growth-gate + cadence). Accepts a ratio (0.75) or percent * string ("75%"). Maps to kernel `nudge.maxContextLimitPct`. */ maxContextLimit?: number | string; + /** Host-side escalation-nudge line: force the tier-1 nudge every turn once + * billed input reaches this fraction of the (output-headroom-adjusted) + * window — filling the cadence-silent climb BELOW `maxContextLimit` seen + * in #453/#14. Previously hardcoded at 0.7 (`EMERGENCY_NUDGE_ESCALATION_PCT`). + * Accepts a ratio (0.7) or percent string ("70%"). Default: 0.7 (unchanged). + * Host-only — never passed to the kernel. If set >= `maxContextLimit` the + * kernel's own pressure branch fires first, so this line never arms. Deepest + * level wins PER FIELD across global → provider → model (#5230). */ + nudgeEscalationPct?: number | string; /** Context usage percentage that triggers emergency truncation of large * tool outputs. Accepts a ratio (0.95) or percent string ("95%"). Must * be >= maxContextLimit. Maps to kernel `nudge.emergencyThresholdPct` + @@ -2557,7 +2566,7 @@ const KNOWN_TOP_LEVEL_KEYS = new Set([ // an unknown top-level key that appears here almost certainly belongs one // level down under "compress". Keep in sync with parseCompressSettings. const COMPRESS_SETTING_FIELDS = new Set([ - "modelContextLimit", "maxContextLimit", "emergencyThresholdPercent", + "modelContextLimit", "maxContextLimit", "nudgeEscalationPct", "emergencyThresholdPercent", "nudgeGrowthTokens", "minPressureBenefitTokens", "requireUsageSampleForPressure", "maxEstimateHoldRounds", "tierNudgeTokens", "nudgeModelDecided", "nudgeDecisionMaxTokens", "requireNudge", "requireRecommendation", "preserveRecentMessages", "preserveRecentTokens", @@ -3276,7 +3285,7 @@ export function parseCompressSettings(v: unknown): (CompressSettings & { injectT if (typeof obj[key] !== "number" || !Number.isFinite(obj[key] as number)) ok = false; else (out as Record)[key] = obj[key]; }; - for (const key of ["modelContextLimit", "maxContextLimit", "emergencyThresholdPercent"] as const) { + for (const key of ["modelContextLimit", "maxContextLimit", "nudgeEscalationPct", "emergencyThresholdPercent"] as const) { if (!(key in obj)) continue; if (!numberOrPercent(obj[key])) { ok = false; continue; } (out as Record)[key] = typeof obj[key] === "string" ? (obj[key] as string).trim() : obj[key]; diff --git a/src/server/budget.ts b/src/server/budget.ts index 06275265f2..0a2305ae8d 100644 --- a/src/server/budget.ts +++ b/src/server/budget.ts @@ -109,6 +109,15 @@ const OUTPUT_LEDGER_FLOOR_GIANT_REQUEST_PCT = 0.25; // host-side: renderNudgeText does not depend on shouldInject. const EMERGENCY_NUDGE_ESCALATION_PCT = 0.7; +// #5230: resolve the host-side escalation line off a resolved kernel config. +// The value rides the ResolvedKernelConfig rail (host-only, never sent to the +// kernel); an absent or non-positive value keeps the 0.7 default, so behavior +// is byte-identical to pre-#5230 unless a level sets compress.nudgeEscalationPct. +export function resolveNudgeEscalationPct(config: unknown): number { + const v = (config as { nudgeEscalationPct?: unknown } | null | undefined)?.nudgeEscalationPct; + return typeof v === "number" && Number.isFinite(v) && v > 0 ? v : EMERGENCY_NUDGE_ESCALATION_PCT; +} + /** #2391: a budget view only; never forward this projection. OpenAI tool search * defers individual function schemas, while namespace descriptions stay visible. * https://developers.openai.com/api/docs/guides/tools-tool-search */ @@ -479,8 +488,9 @@ export function emergencyNudgeArmed( minCompressRange: number, tokenCount: number, growthStep?: number, + escalationPct?: number, ): boolean { - if (!emergencyNudge(nudge, undefined, minCompressRange)) return false; + if (!emergencyNudge(nudge, escalationPct, minCompressRange)) return false; const now = Date.now(); const key = reductionKeyOf(session); const raw = session.metadata["emergencyNudgeGate"]; diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index e840165aa6..2875af4983 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -40,7 +40,7 @@ import { logRawStripCounts, markerArtifactCount, stripRawStage } from "../marker import { imageRescaleMaxDim, renderNone as knobRenderNone } from "../knobs.js"; import { applyImageRescalePass } from "../image-rescale.js"; import { isAutoModeClassifier } from "../auto-mode-forge.js"; -import { countSystemAndToolsTokens, emergencyNudge, emergencyNudgeArmed, projectThinkingMass } from "./budget.js"; +import { countSystemAndToolsTokens, emergencyNudge, emergencyNudgeArmed, projectThinkingMass, resolveNudgeEscalationPct } from "./budget.js"; import { effectiveAbsorbBlock } from "./prepare-responses.js"; import { effectiveBlockOpsEnabled } from "./prepare-responses.js"; import { injectSystem, injectTool } from "./inject.js"; @@ -314,6 +314,7 @@ export async function prepareAnthropic( // stamped per-request — strip `ccr` from the loop config when disarmed // (plugin mode / no tool channel) so placeholders never hit the wire. const loopConfig = ccrLoopConfig(session, { ...config, absorb: absorbActive ? absorbBlock : undefined }); + const nudgeEscalationPct = resolveNudgeEscalationPct(loopConfig); // #2432/#2658: dsh desktop's native compaction can LAND without ever // transiting bili — its summarizer calls ctx.llm.stream() directly and // manual /compact / idle-session paths have no ALS attribution, so the @@ -433,7 +434,7 @@ export async function prepareAnthropic( // #3461: pressure admitted by every standing gate but held by the backoff // is still an active nudge round — self-heal credit (#2155/#3004) must // not starve because we chose not to re-inject this round. - const emergencyPressureHeld = nudgeEligible && !!turn.nudge && !turn.nudge.shouldInject && emergencyNudge(turn.nudge, undefined, loopConfig.compress.minCompressRange); + const emergencyPressureHeld = nudgeEligible && !!turn.nudge && !turn.nudge.shouldInject && emergencyNudge(turn.nudge, nudgeEscalationPct, loopConfig.compress.minCompressRange); // #3482 Step 2.5: the ONE nudge gate. This lane has no compaction // trigger and no intercept health window — those differences are // explicit inputs on the responses lane. @@ -446,7 +447,7 @@ export async function prepareAnthropic( breakerArmed: compressBreakerArmed(session), autoFoldEngaged: autoFoldEngaged(loopConfig, session), growthFoldingArmed: growthFoldingArmed(loopConfig, undefined, autoFoldFloor(session)), - modelWantsIt: turn.nudge !== undefined && (turn.nudge.shouldInject || emergencyNudgeArmed(session, turn.nudge, loopConfig.compress.minCompressRange, tokenCount, loopConfig.nudge.growthFloor)), + modelWantsIt: turn.nudge !== undefined && (turn.nudge.shouldInject || emergencyNudgeArmed(session, turn.nudge, loopConfig.compress.minCompressRange, tokenCount, loopConfig.nudge.growthFloor, nudgeEscalationPct)), }); const caLine = recordNudgeObservation(sessionId, turn, tokenCount, config.modelContextLimit, willInjectNudge, decideTier1Arm && !decideFallbackOverride, surfaceOffDetail(laneDecisions, opts.compress.injectTool)); if (caLine) log("info", caLine); diff --git a/src/server/prepare-google.ts b/src/server/prepare-google.ts index d12f08249d..6988e5c63a 100644 --- a/src/server/prepare-google.ts +++ b/src/server/prepare-google.ts @@ -35,7 +35,7 @@ import { reconcileSystemAnchor } from "../system-anchor.js"; import { logRawStripCounts, markerArtifactCount, stripRawStage } from "../marker/engine.js"; import { imageRescaleMaxDim, renderNone as knobRenderNone } from "../knobs.js"; import { applyImageRescalePass } from "../image-rescale.js"; -import { clampOutgoingOutput, countSystemAndToolsTokens, dshLedgerFloorTokens, emergencyNudge, emergencyNudgeArmed } from "./budget.js"; +import { clampOutgoingOutput, countSystemAndToolsTokens, dshLedgerFloorTokens, emergencyNudge, emergencyNudgeArmed, resolveNudgeEscalationPct } from "./budget.js"; import { estimateCoreMessages } from "../preflight.js"; import { effectiveAbsorbBlock, effectiveBlockOpsEnabled } from "./prepare-responses.js"; import { injectGoogleTool, injectTool } from "./inject.js"; @@ -164,6 +164,7 @@ export async function prepareGoogle( // config stripping — only tool availability matters. const rulesActive = rulesEnabled(config) && shouldInject; const loopConfig = ccrLoopConfig(session, { ...config, absorb: absorbActive ? absorbBlock : undefined }); + const nudgeEscalationPct = resolveNudgeEscalationPct(loopConfig); // #2432/#2658: same post-landing recovery as the openai/responses/ // anthropic lanes — dsh desktop's native compaction can land without // transiting bili (direct-bypass / future gate mismatch); first notice = @@ -260,7 +261,7 @@ export async function prepareGoogle( // #3461: pressure admitted by every standing gate but held by the backoff // is still an active nudge round — self-heal credit (#2155/#3004) must // not starve because we chose not to re-inject this round. - const emergencyPressureHeld = nudgeEligible && !!turn.nudge && !turn.nudge.shouldInject && emergencyNudge(turn.nudge, undefined, loopConfig.compress.minCompressRange); + const emergencyPressureHeld = nudgeEligible && !!turn.nudge && !turn.nudge.shouldInject && emergencyNudge(turn.nudge, nudgeEscalationPct, loopConfig.compress.minCompressRange); // #3482 Step 2.5: the ONE nudge gate (lane-decisions). No compaction trigger // and no intercept health window on this wire — those differences are // explicit inputs on the responses lane. The standalone `shouldInject` @@ -275,7 +276,7 @@ export async function prepareGoogle( breakerArmed: compressBreakerArmed(session), autoFoldEngaged: autoFoldEngaged(loopConfig, session), growthFoldingArmed: growthFoldingArmed(loopConfig, undefined, autoFoldFloor(session)), - modelWantsIt: turn.nudge !== undefined && (turn.nudge.shouldInject || emergencyNudgeArmed(session, turn.nudge, loopConfig.compress.minCompressRange, tokenCount, loopConfig.nudge.growthFloor)), + modelWantsIt: turn.nudge !== undefined && (turn.nudge.shouldInject || emergencyNudgeArmed(session, turn.nudge, loopConfig.compress.minCompressRange, tokenCount, loopConfig.nudge.growthFloor, nudgeEscalationPct)), }); const caLine = recordNudgeObservation(sessionId, turn, tokenCount, config.modelContextLimit, willInjectNudge, decideTier1Arm && !decideFallbackOverride, surfaceOffDetail(laneDecisions, opts.compress.injectTool)); if (caLine) log("info", caLine); diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index b69e92d319..311a6f131b 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -43,7 +43,7 @@ import { reconcileSystemAnchor } from "../system-anchor.js"; import { logRawStripCounts, markerArtifactCount, stripRawStage, type RawStripCounts } from "../marker/engine.js"; import { imageRescaleMaxDim, renderNone as knobRenderNone } from "../knobs.js"; import { applyImageRescalePass } from "../image-rescale.js"; -import { clampOutgoingOutput, countSystemAndToolsTokens, dshLedgerFloorTokens, emergencyNudge, emergencyNudgeArmed, outputBudgetField, readOutputBudget, writeOutputBudget } from "./budget.js"; +import { clampOutgoingOutput, countSystemAndToolsTokens, dshLedgerFloorTokens, emergencyNudge, emergencyNudgeArmed, outputBudgetField, readOutputBudget, resolveNudgeEscalationPct, writeOutputBudget } from "./budget.js"; import { effectiveAbsorbBlock, effectiveBlockOpsEnabled } from "./prepare-responses.js"; import { injectOpenaiTool, injectTool } from "./inject.js"; import { nudgeOn, resolveLaneDecisions, surfaceOffDetail } from "./lane-decisions.js"; @@ -247,6 +247,7 @@ export async function prepareOpenai( const blockOpsActive = effectiveBlockOpsEnabled(pluginMode, config, opts.compress.blockOps) && shouldInject; const rulesActive = rulesEnabled(config) && shouldInject; const loopConfig = ccrLoopConfig(session, { ...config, absorb: absorbActive ? absorbBlock : undefined }); + const nudgeEscalationPct = resolveNudgeEscalationPct(loopConfig); // #2432: dsh desktop's native compaction can LAND without ever // transiting bili — its summarizer calls ctx.llm.stream() directly, // and manual /compact / idle-session paths have no ALS attribution so @@ -400,7 +401,7 @@ export async function prepareOpenai( // #3461: pressure admitted by every standing gate but held by the backoff // is still an active nudge round — self-heal credit (#2155/#3004) must // not starve because we chose not to re-inject this round. - const emergencyPressureHeld = nudgeEligible && !!turn.nudge && !turn.nudge.shouldInject && emergencyNudge(turn.nudge, undefined, loopConfig.compress.minCompressRange); + const emergencyPressureHeld = nudgeEligible && !!turn.nudge && !turn.nudge.shouldInject && emergencyNudge(turn.nudge, nudgeEscalationPct, loopConfig.compress.minCompressRange); // #3482 Step 2.5: the ONE nudge gate (lane-decisions). No compaction trigger // and no intercept health window on this wire — those differences are // explicit inputs on the responses lane. The standalone `shouldInject` @@ -415,7 +416,7 @@ export async function prepareOpenai( breakerArmed: compressBreakerArmed(session), autoFoldEngaged: autoFoldEngaged(loopConfig, session), growthFoldingArmed: growthFoldingArmed(loopConfig, undefined, autoFoldFloor(session)), - modelWantsIt: turn.nudge !== undefined && (turn.nudge.shouldInject || emergencyNudgeArmed(session, turn.nudge, loopConfig.compress.minCompressRange, tokenCount, loopConfig.nudge.growthFloor)), + modelWantsIt: turn.nudge !== undefined && (turn.nudge.shouldInject || emergencyNudgeArmed(session, turn.nudge, loopConfig.compress.minCompressRange, tokenCount, loopConfig.nudge.growthFloor, nudgeEscalationPct)), }); const caLine = recordNudgeObservation(sessionId, turn, tokenCount, config.modelContextLimit, willInjectNudge, decideTier1Arm && !decideFallbackOverride, surfaceOffDetail(laneDecisions, opts.compress.injectTool)); if (caLine) log("info", caLine); diff --git a/src/server/prepare-responses.ts b/src/server/prepare-responses.ts index 94e16bfb2f..4d12897f3c 100644 --- a/src/server/prepare-responses.ts +++ b/src/server/prepare-responses.ts @@ -46,7 +46,7 @@ import { reconcilePluginFoldCallIds } from "../plugin.js"; import { logRawStripCounts, markerArtifactCount, stripRawStage } from "../marker/engine.js"; import { imageRescaleMaxDim, keepResponseId as knobKeepResponseId, noCompressPrompt as knobNoCompressPrompt, noInjectTool as knobNoInjectTool, renderNone as knobRenderNone } from "../knobs.js"; import { applyImageRescaleItems } from "../image-rescale.js"; -import { clampOutgoingOutput, countLoadedToolTokens, countSystemAndToolsTokens, dshLedgerFloorTokens, emergencyNudge, emergencyNudgeArmed, modelVisibleTools } from "./budget.js"; +import { clampOutgoingOutput, countLoadedToolTokens, countSystemAndToolsTokens, dshLedgerFloorTokens, emergencyNudge, emergencyNudgeArmed, modelVisibleTools, resolveNudgeEscalationPct } from "./budget.js"; import { FORCE_TEXT_PROTOCOL, injectResponsesTool, injectTool } from "./inject.js"; import { nudgeOn, resolveLaneDecisions, surfaceOffDetail } from "./lane-decisions.js"; import { pluginAgentHeader } from "../plugin.js"; @@ -291,6 +291,7 @@ export async function prepareResponses( const blockOpsActive = effectiveBlockOpsEnabled(pluginMode, config, opts.compress.blockOps) && shouldInject && !isCompactionTrigger; const rulesActive = rulesEnabled(config) && shouldInject && !isCompactionTrigger && !responsesTextProtocol; const loopConfig = ccrLoopConfig(session, { ...config, absorb: absorbActive ? absorbBlock : undefined }); + const nudgeEscalationPct = resolveNudgeEscalationPct(loopConfig); // #2372/#2432: LOCAL auto-compaction — the client compacts on its own // and the call never transits the proxy; the first the proxy hears of // it is this request replaying [compaction summary, retained tail…]. @@ -430,7 +431,7 @@ reconcileFoldCoverage(session, msgs, { mode: foldMode, sessionId, log, markerStr // #3461: pressure admitted by every standing gate but held by the backoff // is still an active nudge round — self-heal credit (#2155/#3004) must // not starve because we chose not to re-inject this round. - const emergencyPressureHeld = nudgeEligible && !!turn.nudge && !turn.nudge.shouldInject && emergencyNudge(turn.nudge, undefined, loopConfig.compress.minCompressRange); + const emergencyPressureHeld = nudgeEligible && !!turn.nudge && !turn.nudge.shouldInject && emergencyNudge(turn.nudge, nudgeEscalationPct, loopConfig.compress.minCompressRange); // #3482 Step 2.5: the ONE nudge gate (lane-decisions). The responses lane's // two DELIBERATE differences are explicit inputs: the compaction // trigger (#2155/#2432 — a trigger request never carries the nudge) @@ -444,7 +445,7 @@ reconcileFoldCoverage(session, msgs, { mode: foldMode, sessionId, log, markerStr breakerArmed: compressBreakerArmed(session), autoFoldEngaged: autoFoldEngaged(loopConfig, session), growthFoldingArmed: growthFoldingArmed(loopConfig, codexLane && codexCompactMode() === "intercept" ? loopConfig.modelContextLimit * CODEX_COMPACT_HEALTH_RATIO : undefined, autoFoldFloor(session)), - modelWantsIt: turn.nudge !== undefined && (turn.nudge.shouldInject || emergencyNudgeArmed(session, turn.nudge, loopConfig.compress.minCompressRange, tokenCount, loopConfig.nudge.growthFloor)), + modelWantsIt: turn.nudge !== undefined && (turn.nudge.shouldInject || emergencyNudgeArmed(session, turn.nudge, loopConfig.compress.minCompressRange, tokenCount, loopConfig.nudge.growthFloor, nudgeEscalationPct)), }); const caLine = recordNudgeObservation(sessionId, turn, tokenCount, config.modelContextLimit, willInjectNudge, decideTier1Arm && !decideFallbackOverride, surfaceOffDetail(laneDecisions, opts.compress.injectTool)); if (caLine) log("info", caLine); diff --git a/tests/issue5230-nudge-escalation-pct.test.ts b/tests/issue5230-nudge-escalation-pct.test.ts new file mode 100644 index 0000000000..6dedc771ba --- /dev/null +++ b/tests/issue5230-nudge-escalation-pct.test.ts @@ -0,0 +1,76 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { defaultConfig, type NudgeDecision } from "acp-kernel"; +import { mergeCompress, applyCompressSettings, resolveRequestConfig } from "../src/compress-settings.ts"; +import { resolveNudgeEscalationPct, emergencyNudge } from "../src/server/budget.ts"; + +// #5230: the host-side escalation-nudge line (EMERGENCY_NUDGE_ESCALATION_PCT, +// hardcoded at 0.7) had no config surface. compress.nudgeEscalationPct adds one: +// deepest-wins across global/provider/model, rides the ResolvedKernelConfig rail +// to the prepare call sites, and an absent/non-positive value keeps the 0.7 +// default byte-for-byte (behavior unchanged unless a level opts in). + +function mkNudge(contextUsage: number): NudgeDecision { + const breakdown = { usage: 0, growth: 0, growthReference: 0, effectiveThreshold: 0, nudgeGrowthTokens: 0, growthFloor: 0, hasPendingNudge: 0, overLimit: 0, emergencyOverride: 0, pendingT1: 0, pendingT2: 0, pendingT3: 0, maxPending: 0 }; + return { + shouldInject: false, + reason: "test", + contextUsage, + tier: null, + compressibleRanges: [{ startRef: "a", endRef: "b", count: 1, tokens: 4000, toolPct: 0, textPct: 1 }], + breakdown, + }; +} + +test("#5230: resolveNudgeEscalationPct defaults to 0.7 when absent or invalid", () => { + assert.equal(resolveNudgeEscalationPct(undefined), 0.7); + assert.equal(resolveNudgeEscalationPct(null), 0.7); + assert.equal(resolveNudgeEscalationPct({}), 0.7); + assert.equal(resolveNudgeEscalationPct({ nudgeEscalationPct: undefined }), 0.7); + assert.equal(resolveNudgeEscalationPct({ nudgeEscalationPct: 0 }), 0.7, "non-positive falls back"); + assert.equal(resolveNudgeEscalationPct({ nudgeEscalationPct: -0.5 }), 0.7, "negative falls back"); + assert.equal(resolveNudgeEscalationPct({ nudgeEscalationPct: Number.NaN }), 0.7, "NaN falls back"); + assert.equal(resolveNudgeEscalationPct({ nudgeEscalationPct: "0.85" }), 0.7, "non-number falls back (percent parsing happens upstream)"); +}); + +test("#5230: resolveNudgeEscalationPct honors an explicit positive value", () => { + assert.equal(resolveNudgeEscalationPct({ nudgeEscalationPct: 0.85 }), 0.85); + assert.equal(resolveNudgeEscalationPct({ nudgeEscalationPct: 0.99 }), 0.99); +}); + +test("#5230: applyCompressSettings maps nudgeEscalationPct onto the resolved config", () => { + const base = defaultConfig(200_000); + assert.equal(applyCompressSettings(base, 200_000, { nudgeEscalationPct: 0.85 }).nudgeEscalationPct, 0.85); + assert.equal(applyCompressSettings(base, 200_000, { nudgeEscalationPct: "75%" }).nudgeEscalationPct, 0.75, "percent string parses"); + assert.equal(applyCompressSettings(base, 200_000, {}).nudgeEscalationPct, undefined, "unset leaves it absent -> budget default"); +}); + +test("#5230: mergeCompress deepest-wins for nudgeEscalationPct", () => { + assert.equal(mergeCompress({ nudgeEscalationPct: 0.6 }, { nudgeEscalationPct: 0.7 }, { nudgeEscalationPct: 0.85 }).nudgeEscalationPct, 0.85); + assert.equal(mergeCompress({ nudgeEscalationPct: 0.6 }, { nudgeEscalationPct: 0.7 }, {}).nudgeEscalationPct, 0.7); + assert.equal(mergeCompress({ nudgeEscalationPct: 0.6 }, {}, {}).nudgeEscalationPct, 0.6); +}); + +test("#5230: resolveRequestConfig carries nudgeEscalationPct onto the resolved rail", () => { + const base = defaultConfig(200_000); + const tuned = resolveRequestConfig(base, {}, undefined, "m", 200_000, { nudgeEscalationPct: 0.8 }); + assert.equal(tuned.nudgeEscalationPct, 0.8); + assert.equal(resolveNudgeEscalationPct(tuned), 0.8); + const plain = resolveRequestConfig(base, {}, undefined, "m", 200_000, {}); + assert.equal(resolveNudgeEscalationPct(plain), 0.7, "unset everywhere -> base unchanged -> default 0.7"); +}); + +test("#5230: the knob moves the fire line (user scenario: default 0.7 vs raised 0.85)", () => { + const base = defaultConfig(200_000); + const defaultLine = resolveNudgeEscalationPct(base); + const raisedLine = resolveNudgeEscalationPct(applyCompressSettings(base, 200_000, { nudgeEscalationPct: 0.85 })); + assert.equal(defaultLine, 0.7); + assert.equal(raisedLine, 0.85); + const nudge = mkNudge(0.75); + assert.equal(emergencyNudge(nudge, defaultLine), true, "0.75 >= 0.7 default fires"); + assert.equal(emergencyNudge(nudge, raisedLine), false, "0.75 < 0.85 raised stays silent"); + assert.equal(emergencyNudge(mkNudge(0.9), raisedLine), true, "0.9 >= 0.85 raised fires"); +}); diff --git a/website/config-reference/compress.yaml b/website/config-reference/compress.yaml index 700ac82737..d95b2e016d 100644 --- a/website/config-reference/compress.yaml +++ b/website/config-reference/compress.yaml @@ -24,6 +24,17 @@ entries: desc_ja: "強制圧縮のナッジ閾値:履歴がこのウィンドウの割合を超えると、ナッジが即座に発火し、成長ゲートをバイパスする。ハードキャップではない。" site_en: "Forced-compression nudge threshold (default \"75%\"): once history passes this share of the window the nudge fires immediately, bypassing the growth gates. Not a hard cap." site_zh: "强制压缩提醒阈值(默认 \"75%\"):历史超过窗口的该占比时提醒立即触发,绕过增长门槛。不是硬上限。" + - key: compress.nudgeEscalationPct + type: "number | \"N%\"" + default: 0.7 + group: compress + id: cw.nesc + render: compress-row + desc_en: "Host-side escalation-nudge line: injects a compression nudge once usage crosses this ratio even while the kernel cadence stays silent (covers the climb below maxContextLimit). Host-only; if set at/above maxContextLimit the kernel pressure band takes over first." + desc_zh: "宿主侧升级提醒线:使用率越过该比例时,即使内核节奏仍静默也注入一次压缩提醒(覆盖 maxContextLimit 之下的爬升段)。仅宿主侧;若设到 >= maxContextLimit,内核压力带会先触发。" + desc_ja: "ホスト側のエスカレーション・ナッジライン:使用率がこの比率を超えた時点で、カーネルの周期が沈黙している間でも圧縮ナッジを注入する(maxContextLimit 未満の上昇をカバー)。ホスト専用;maxContextLimit 以上で設定した場合、カーネルの圧力帯が先に引き継ぐ。" + site_en: "Host-side escalation-nudge line (default 0.7): injects a compression nudge once usage crosses this ratio even while the kernel cadence stays silent — covers the climb below maxContextLimit (#5230). Host-only, never sent to the kernel; an absent or non-positive value keeps the 0.7 default byte-for-byte." + site_zh: "宿主侧升级提醒线(默认 0.7):使用率越过该比例时,即使内核节奏仍静默也注入一次压缩提醒——覆盖 maxContextLimit 之下的爬升段(#5230)。仅宿主使用,不下发内核;缺省或非正值保持 0.7 默认(逐字节不变)。" - key: compress.emergencyThresholdPercent type: "number | %" default: "\"95%\"" diff --git a/website/docs/index.html b/website/docs/index.html index 4b1172324e..b7f877faf0 100644 --- a/website/docs/index.html +++ b/website/docs/index.html @@ -183,6 +183,7 @@

compress — every knob

modelContextLimitnumber | "N%"Effective context window (tokens or "N%"): denominator of the usage ratio and a hard preflight wall. Highest-priority window source — overrides per-route / per-model declarations plus the top-level default. maxContextLimitnumber | "N%"Forced-compression nudge threshold (default "75%"): once history passes this share of the window the nudge fires immediately, bypassing the growth gates. Not a hard cap. + nudgeEscalationPctnumber | "N%"Host-side escalation-nudge line (default 0.7): injects a compression nudge once usage crosses this ratio even while the kernel cadence stays silent — covers the climb below maxContextLimit (#5230). Host-only, never sent to the kernel; an absent or non-positive value keeps the 0.7 default byte-for-byte. emergencyThresholdPercentnumber | %Default "95%": emergency truncation of oversized tool outputs when history passes this share of the window (must be ≥ maxContextLimit). outputHeadroomMaxPctnumber | %Cap (0–1, default 0.25) on the share of the window reserved for output via max_tokens. nudgeGrowthTokensnumberGrowth gate (default 50000): nudges fire only when a foldable range exceeds baseline growth by this many tokens. diff --git a/website/main.js b/website/main.js index 19461eee64..19bcbbf4f0 100644 --- a/website/main.js +++ b/website/main.js @@ -63,6 +63,7 @@ /* bili:gen site-dict-en */ "cw.mcl": "Effective context window (tokens or \"N%\"): denominator of the usage ratio and a hard preflight wall. Highest-priority window source — overrides per-route / per-model declarations plus the top-level default.", "cw.mxcl": "Forced-compression nudge threshold (default \"75%\"): once history passes this share of the window the nudge fires immediately, bypassing the growth gates. Not a hard cap.", + "cw.nesc": "Host-side escalation-nudge line (default 0.7): injects a compression nudge once usage crosses this ratio even while the kernel cadence stays silent — covers the climb below maxContextLimit (#5230). Host-only, never sent to the kernel; an absent or non-positive value keeps the 0.7 default byte-for-byte.", "cw.etc": "Default \"95%\": emergency truncation of oversized tool outputs when history passes this share of the window (must be ≥ maxContextLimit).", "cw.ohmp": "Cap (0–1, default 0.25) on the share of the window reserved for output via max_tokens.", "cw.ngt": "Growth gate (default 50000): nudges fire only when a foldable range exceeds baseline growth by this many tokens.", @@ -321,6 +322,7 @@ /* bili:gen site-dict-zh */ "cw.mcl": "有效上下文窗口(token 数或 \"N%\"):使用率的分母,也是硬性 preflight 墙。窗口来源中优先级最高——覆盖路由/模型声明与顶层默认值。", "cw.mxcl": "强制压缩提醒阈值(默认 \"75%\"):历史超过窗口的该占比时提醒立即触发,绕过增长门槛。不是硬上限。", + "cw.nesc": "宿主侧升级提醒线(默认 0.7):使用率越过该比例时,即使内核节奏仍静默也注入一次压缩提醒——覆盖 maxContextLimit 之下的爬升段(#5230)。仅宿主使用,不下发内核;缺省或非正值保持 0.7 默认(逐字节不变)。", "cw.etc": "默认 \"95%\":历史超过窗口该占比时,对超大工具输出做紧急截断(必须 ≥ maxContextLimit)。", "cw.ohmp": "max_tokens 输出预留占窗口的最大比例(0–1,默认 0.25)。", "cw.ngt": "增长门槛(默认 50000):可折叠片段超出基线增长达到该 token 数才发提醒。",