Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/configuration.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | — | 成長ゲート:折りたたみ可能な範囲がベースライン成長をこのトークン数だけ上回った場合のみ、ナッジが発火する(設計上フラットであり、ウィンドウサイズに依存しない)。 |
Expand Down
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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). |
Expand Down
1 change: 1 addition & 0 deletions docs/configuration.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 数才发提醒(按设计恒定,与窗口大小无关)。 |
Expand Down
8 changes: 7 additions & 1 deletion src/compress-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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).
Expand Down
13 changes: 11 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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` +
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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<string, unknown>)[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<string, unknown>)[key] = typeof obj[key] === "string" ? (obj[key] as string).trim() : obj[key];
Expand Down
12 changes: 11 additions & 1 deletion src/server/budget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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"];
Expand Down
7 changes: 4 additions & 3 deletions src/server/prepare-anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions src/server/prepare-google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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`
Expand All @@ -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);
Expand Down
Loading
Loading