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 @@ -283,6 +283,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 @@ -286,6 +286,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 @@ -286,6 +286,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 @@ -74,6 +74,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 @@ -306,7 +307,7 @@ export function hasCompressSettings(s: CompressSettings): boolean {
// clone-safe DATA half of the isMessageProtected predicate: the stamp
// (storeEffectiveConfig) strips function fields, and effectiveConfig rebuilds
// the predicate from this list on the session-stamp read path.
export type ResolvedKernelConfig = Config & { preserveRecentTools?: string[]; externalSummary?: ExternalSummarySettings; protectedUserPatterns?: string[] };
export type ResolvedKernelConfig = Config & { preserveRecentTools?: string[]; externalSummary?: ExternalSummarySettings; protectedUserPatterns?: string[]; nudgeEscalationPct?: number };

/** User-text half of the host predicate: literal substring match on
* user-role text messages only. Both the resolve-time builder
Expand Down Expand Up @@ -446,6 +447,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 @@ -2658,7 +2667,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", "postComPressTail",
"preserveRecentMessages", "preserveRecentTokens",
Expand Down Expand Up @@ -3383,7 +3392,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
32 changes: 24 additions & 8 deletions src/server/budget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ export function resolveEmergencyEscalationPct(maxContextLimitPct?: number): numb
return EMERGENCY_NUDGE_ESCALATION_PCT;
}

// #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 explicit positive compress.nudgeEscalationPct wins; an absent or
// non-positive value falls back to the #7605 auto-track
// (resolveEmergencyEscalationPct), which keeps the 0.70 default and tracks
// maxContextLimitPct - 0.05 once an operator raises the over-limit line — so
// behavior is byte-identical to pre-#5230 unless a level sets the field.
export function resolveNudgeEscalationPct(config: unknown): number {
const c = config as { nudgeEscalationPct?: unknown; nudge?: { maxContextLimitPct?: number } } | null | undefined;
const v = c?.nudgeEscalationPct;
if (typeof v === "number" && Number.isFinite(v) && v > 0) return v;
return resolveEmergencyEscalationPct(c?.nudge?.maxContextLimitPct);
}

/** #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 @@ -478,11 +492,11 @@ export function emergencyNudge(nudge: NudgeDecision | null | undefined, escalati

export function emergencyNudgeForConfig(
nudge: NudgeDecision | null | undefined,
loopConfig: { compress: { minCompressRange: number }; nudge: { maxContextLimitPct?: number } },
loopConfig: { compress: { minCompressRange: number }; nudge: { maxContextLimitPct?: number }; nudgeEscalationPct?: number },
): boolean {
return emergencyNudge(
nudge,
resolveEmergencyEscalationPct(loopConfig.nudge.maxContextLimitPct),
resolveNudgeEscalationPct(loopConfig),
loopConfig.compress.minCompressRange,
);
}
Expand All @@ -509,18 +523,20 @@ export function emergencyNudgeForConfig(
* call sites OR them in BEFORE the emergency arm, so normal cadence is
* untouched. State lives in session.metadata.emergencyNudgeGate (survives
* restarts with the snapshot, visible on /__bili/sessions like selfHeal).
* The escalation line is config-aware (#7605): resolveEmergencyEscalationPct
* keeps the default 0.70 and tracks maxContextLimitPct - 0.05 once an operator
* raises the over-limit line above the 0.75 default. */
export function emergencyNudgeArmed(
* The escalation line is config-aware: an explicit compress.nudgeEscalationPct
* (#5230) wins when set; otherwise resolveEmergencyEscalationPct (#7605) keeps
* the default 0.70 and tracks maxContextLimitPct - 0.05 once an operator raises
* the over-limit line above the 0.75 default. Call sites pass the resolved
* loop config; resolution goes through resolveNudgeEscalationPct. */
export function emergencyNudgeArmed(
session: Session,
nudge: NudgeDecision | null | undefined,
minCompressRange: number,
tokenCount: number,
growthStep?: number,
maxContextLimitPct?: number,
loopConfig?: { nudge?: { maxContextLimitPct?: number }; nudgeEscalationPct?: number },
): boolean {
if (!emergencyNudge(nudge, resolveEmergencyEscalationPct(maxContextLimitPct), minCompressRange)) return false;
if (!emergencyNudge(nudge, resolveNudgeEscalationPct(loopConfig), minCompressRange)) return false;
const now = Date.now();
const key = reductionKeyOf(session);
const raw = session.metadata["emergencyNudgeGate"];
Expand Down
2 changes: 1 addition & 1 deletion src/server/prepare-anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,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, loopConfig.nudge.maxContextLimitPct)),
modelWantsIt: turn.nudge !== undefined && (turn.nudge.shouldInject || emergencyNudgeArmed(session, turn.nudge, loopConfig.compress.minCompressRange, tokenCount, loopConfig.nudge.growthFloor, loopConfig)),
});
const caLine = recordNudgeObservation(sessionId, turn, tokenCount, config.modelContextLimit, willInjectNudge, decideTier1Arm && !decideFallbackOverride, surfaceOffDetail(laneDecisions, opts.compress.injectTool));
if (caLine) log("info", caLine);
Expand Down
2 changes: 1 addition & 1 deletion src/server/prepare-google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,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, loopConfig.nudge.maxContextLimitPct)),
modelWantsIt: turn.nudge !== undefined && (turn.nudge.shouldInject || emergencyNudgeArmed(session, turn.nudge, loopConfig.compress.minCompressRange, tokenCount, loopConfig.nudge.growthFloor, loopConfig)),
});
const caLine = recordNudgeObservation(sessionId, turn, tokenCount, config.modelContextLimit, willInjectNudge, decideTier1Arm && !decideFallbackOverride, surfaceOffDetail(laneDecisions, opts.compress.injectTool));
if (caLine) log("info", caLine);
Expand Down
2 changes: 1 addition & 1 deletion src/server/prepare-openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,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, loopConfig.nudge.maxContextLimitPct)),
modelWantsIt: turn.nudge !== undefined && (turn.nudge.shouldInject || emergencyNudgeArmed(session, turn.nudge, loopConfig.compress.minCompressRange, tokenCount, loopConfig.nudge.growthFloor, loopConfig)),
});
const caLine = recordNudgeObservation(sessionId, turn, tokenCount, config.modelContextLimit, willInjectNudge, decideTier1Arm && !decideFallbackOverride, surfaceOffDetail(laneDecisions, opts.compress.injectTool));
if (caLine) log("info", caLine);
Expand Down
2 changes: 1 addition & 1 deletion src/server/prepare-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,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, loopConfig.nudge.maxContextLimitPct)),
modelWantsIt: turn.nudge !== undefined && (turn.nudge.shouldInject || emergencyNudgeArmed(session, turn.nudge, loopConfig.compress.minCompressRange, tokenCount, loopConfig.nudge.growthFloor, loopConfig)),
});
const caLine = recordNudgeObservation(sessionId, turn, tokenCount, config.modelContextLimit, willInjectNudge, decideTier1Arm && !decideFallbackOverride, surfaceOffDetail(laneDecisions, opts.compress.injectTool));
if (caLine) log("info", caLine);
Expand Down
4 changes: 2 additions & 2 deletions tests/emergency-nudge-escalation-scale.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ test("emergencyNudgeForConfig and emergencyNudgeArmed honor raised maxContextLim
const at81 = makeNudge(0.81);

assert.equal(emergencyNudgeForConfig(at74, cfg), false);
assert.equal(emergencyNudgeArmed(makeSession(), at74, 1000, 74_000, undefined, 0.85), false);
assert.equal(emergencyNudgeArmed(makeSession(), at74, 1000, 74_000, undefined, cfg), false);

assert.equal(emergencyNudgeForConfig(at81, cfg), true);
assert.equal(emergencyNudgeArmed(makeSession(), at81, 1000, 81_000, undefined, 0.85), true);
assert.equal(emergencyNudgeArmed(makeSession(), at81, 1000, 81_000, undefined, cfg), true);
});
Loading
Loading