Skip to content
Merged
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
12 changes: 12 additions & 0 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ This index is generated from `website/config-reference/*.yaml` — edit the seed
| `compress.minPressureBenefitTokens` | number (≥ 0) | max(5000, limit × 1%) | — | Minimum reclaimable tokens a pressure-band nudge must have to fire; on inflated-meter routes raise it so folds batch into big events instead of high-frequency small-yield churn (#3211). Explicit 0 = legacy any-pending. |
| `compress.nudgeModelDecided` | boolean | off (unset) | — | Model-decided nudge timing (#2228): an armed tier-1 nudge first asks the model — via a short side call over the session's cached prefix — whether compressing NOW helps the current task. A strict-JSON "yes" injects an explicit directive with a program-finalized span; "no", a malformed answer, or a timeout injects nothing this round. EMERGENCY arms and tier≥2 distillation always keep the legacy advisory. Off unless explicitly enabled. |
| `compress.nudgeDecisionMaxTokens` | number | 200 | — | Output budget (tokens) of the model-decision side call used by nudgeModelDecided. Must be > 0. |
| `compress.requireNudge` | boolean | off (unset) | — | Forbid UNPROMPTED model-initiated compression (#5683): a compress call is refused unless a compression nudge actually reached the model in the current turn (advisory, decide-directive and emergency injections all arm; loop rounds of the same turn keep the arm, the next un-nudged turn disarms). Refusal sends the model an error receipt instead of folding; bili's own overflow backstop folds are exempt. For models that fold on their own (e.g. qwen 27b); can be scoped per provider/model. Off unless explicitly enabled. |
| `compress.requireRecommendation` | boolean | off (unset) | — | Forbid OUT-OF-BOUNDS model-initiated compression (#5683): every submitted range must stay within what bili currently recommends folding — the same live compressible set the nudge text and acp_status show (this request's prepare-time offer when present, else a live recompute). Judgment is by COVERAGE: subdividing a recommended span passes, and messages already folded into active blocks (refolds updating an existing summary) are exempt. A range carrying any other live message is refused whole with an error receipt naming the offending span(s). Off unless explicitly enabled. |
| `compress.streamSummary` | boolean | false (unset) | — | Force preflight summarization to run as a streaming (SSE) call from the first attempt. The error-driven self-learn handles stream-mandating upstreams on its own (any 400 on a non-streaming summary triggers a one-shot SSE probe, #2494) but NEVER arms on gateway timeouts (e.g. Cloudflare HTTP 524): set this where such a gateway sits between bili and the origin, or simply to skip the one probe round-trip. |
| `compress.preserveRecentMessages` | number | 3 | — | The most recent messages stay soft-protected from folds. |
| `compress.preserveRecentTokens` | number | kernel ≈5000 | — | The most recent tokens stay soft-protected from folds. |
Expand Down Expand Up @@ -1414,6 +1416,16 @@ For each request, the proxy resolves the settings by longest-URL-prefix match (t
- **Status:** ACTIVE (#2228)
- **Description:** Output budget, in tokens, of the model-decision side call made when `nudgeModelDecided` is enabled. Must be > 0 (invalid values are rejected at config load).

#### `requireNudge` / `requireRecommendation`

- **Type:** `boolean` (each)
- **Default:** *(off unless set)*
- **Status:** ACTIVE (#5683)
- **Description:** Policy gates for models that compress on their own initiative (field case: qwen 27b calling compress without any prompt). Both are host-only fields — not passed to the kernel — merged PER FIELD across global → provider → model like every other compress field, editable in the web panel's quick-config card or any config level, hot-applied from the next request onward.
- `requireNudge` (timing gate): a model-initiated compress call is refused unless a compression nudge actually reached the model in the CURRENT TURN — the post-gate delivery fact (`nudgeOutcome === "injected"`), so advisory nudges, model-decided directives and emergency nudges all arm it, while "gated"/"vetoed"/"skipped" rounds never did. The arm persists across loop re-requests within the same turn (one turn may fold several times in response to one nudge) and dies with the next un-nudged turn. The refusal is an error receipt telling the model that unprompted compression is disabled here and to wait for a nudge.
- `requireRecommendation` (scope gate): every submitted range must stay within what bili currently RECOMMENDS folding — the same live compressible set the nudge text and `acp_status` show (this request's prepare-time offer when present, else a live recompute, with the usual viable + dead-ref + `minCompressRangeChars` filters). Judgment is by COVERAGE, not equality: subdividing a recommended span into smaller chunks passes (bili's own first-sight guidance encourages splitting), and messages already folded into active blocks are exempt so refolds updating an existing block's summary stay legal. A range carrying any other still-live message is refused whole, naming the offending span(s) and pointing at `acp_status`.
- Exemptions & interactions: bili's OWN overflow backstop folds (preflight / auto-fold — no model call involved) are exempt from both gates, so a gated session never dies of context overflow; availability is unaffected even while the nudge channel itself is suppressed (self-heal D1). Hosts that drive compression through their own mechanisms (e.g. codex's compaction trigger, which is not a bili nudge) fall outside the timing gate's notion of "prompted" — scope these settings to the affected provider/model rather than globally if such lanes share the route. No new environment variables; no behavior change unless a level explicitly enables a flag.

#### `preserveRecentMessages`

- **Type:** `number`
Expand Down
12 changes: 12 additions & 0 deletions CONFIGURATION.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@
| `compress.minPressureBenefitTokens` | number (≥ 0) | max(5000, limit × 1%) | — | 压力带 nudge 触发所需的最小可回收 token 数;虚报表线路调大它,让折叠攒成大批量而非高频小折(#3211)。显式 0=老的任意 pending 行为。 |
| `compress.nudgeModelDecided` | boolean | off (unset) | — | 模型自决的压缩时机(#2228):tier-1 提醒触发时,先通过一次走会话缓存前缀的短 side call 问模型——以当前任务为前提,现在压缩是否划算。严格 JSON 的 "yes" 会注入带程序最终确定范围的明确压缩指令;"no"、格式错误或超时则本轮不注入任何内容。EMERGENCY 档与 tier≥2 蒸馏始终保留原有 advisory。默认关闭,需显式开启。 |
| `compress.nudgeDecisionMaxTokens` | number | 200 | — | nudgeModelDecided 所用模型决策 side call 的输出预算(token)。必须 > 0。 |
| `compress.requireNudge` | boolean | off (unset) | — | 禁止未提示的模型主动压缩(#5683):只有当前轮次压缩提醒真正送达模型时(advisory、模型自决指令、紧急提醒都算;"gated"/"vetoed"/"skipped" 不算)才允许 compress,否则拒绝并回错误回执;同一轮内的多次 loop 请求保持武装,下一个未提示轮次自动解除。bili 自己的溢出兜底折叠不受影响。针对会自行乱压的模型(如 qwen 27b),可按 provider/model 单独开启。默认关闭。 |
| `compress.requireRecommendation` | boolean | off (unset) | — | 禁止越界的模型主动压缩(#5683):提交的每个范围必须落在 bili 当前推荐折叠的集合内——即 nudge 文案与 acp_status 展示的同一份实时可压缩列表(有本轮 prepare 快照用快照,否则实时重算)。按「覆盖」判定而非精确相等:把推荐跨度拆小段通过;已折进活跃块的消息豁免(重折已有块更新摘要合法)。范围里含任何其他仍存活的消息则整体拒绝,回执点名越界跨度并指向 acp_status。默认关闭。 |
| `compress.streamSummary` | boolean | false (unset) | — | 强制 preflight 摘要从首次尝试起就走流式(SSE)请求。要求流式的上游现在由错误驱动自学习自动处理(非流式摘要的任何 400 都会触发一次性 SSE 探测,#2494),但网关超时(如 Cloudflare HTTP 524)永远无法触发自学习:在此类网关位于 bili 与源站之间时设置此项,或用于省去一次探测往返。 |
| `compress.preserveRecentMessages` | number | 3 | — | 最近的消息软保护、免于折叠。 |
| `compress.preserveRecentTokens` | number | kernel ≈5000 | — | 最近的 token 软保护、免于折叠。 |
Expand Down Expand Up @@ -1411,6 +1413,16 @@ ACP 原生 agent(当前为 `pi` 扩展)会在每个进程内向代理上报
- **状态:** ACTIVE (#2228)
- **说明:** `nudgeModelDecided` 开启时,模型决策 side call 的输出预算(token)。必须 > 0(非法值在配置加载时被拒绝)。

#### `requireNudge` / `requireRecommendation`

- **类型:** `boolean`(各自独立)
- **默认值:** *(关闭,除非显式设置)*
- **状态:** ACTIVE (#5683)
- **说明:** 针对会自行主动压缩的模型的策略门禁(现场案例:qwen 27b 没有任何提示就调用 compress)。两者都是宿主侧字段——不传入内核——与其他 compress 字段一样按字段在 global → provider → model 三级合并,可在 web 面板快速配置卡片或任意配置层级编辑,从下一个请求起热生效。
- `requireNudge`(时序门禁):只有**当前轮次**压缩提醒真正送达过模型(post-gate 投递事实 `nudgeOutcome === "injected"`——advisory 提醒、模型自决指令、紧急提醒都算;"gated"/"vetoed"/"skipped" 不算)才允许模型发起 compress,否则拒绝并回一条错误回执,告知未提示压缩在本线路被禁用、等提醒出现再压。武装在同一轮内的 loop 重请求间保持(一轮响应一个提醒可连续折几次),到下一个未提示轮次自动解除。
- `requireRecommendation`(范围门禁):提交的每个范围必须落在 bili 当前**推荐**折叠的集合内——与 nudge 文案、`acp_status` 展示的同一份实时可压缩列表(有本轮 prepare 快照用快照,否则实时重算,带常规的 viable + dead-ref + `minCompressRangeChars` 过滤)。按「覆盖」判定而非精确相等:把推荐跨度拆成小段通过(bili 自己的首见消化指南就鼓励拆分);已折进活跃块的消息豁免,重折已有块更新摘要保持合法。范围里含任何其他仍存活消息则整体拒绝,回执点名越界跨度并指向 `acp_status`。
- 豁免与交互:bili **自己**的溢出兜底折叠(preflight / auto-fold——不涉及模型调用)两个门禁都豁免,被门禁的会话不会因上下文溢出而死掉;即使 nudge 通道本身被自愈机制抑制(D1),可用性也不受影响。通过宿主自身机制驱动压缩的车道(如 codex 的 compaction trigger——它不是 bili 的提醒)不在时序门禁「已提示」的定义内——若这类车道与目标共用路由,请把这些设置限定到受影响的 provider/model 而不是全局开启。无新增环境变量;未显式开启任何一级时行为不变。

#### `preserveRecentMessages`

- **类型:** `number`
Expand Down
2 changes: 2 additions & 0 deletions src/compress-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ export function mergeCompress(
tierNudgeTokens: tierNudgeLevels.length > 0 ? Object.assign({}, ...tierNudgeLevels) : undefined,
nudgeModelDecided: pick("nudgeModelDecided"),
nudgeDecisionMaxTokens: pick("nudgeDecisionMaxTokens"),
requireNudge: pick("requireNudge"),
requireRecommendation: pick("requireRecommendation"),
preserveRecentMessages: pick("preserveRecentMessages"),
preserveRecentTokens: pick("preserveRecentTokens"),
minCompressRangeChars: rangeOf(model) ?? rangeOf(provider) ?? rangeOf(global),
Expand Down
32 changes: 32 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,30 @@ export type CompressSettings = {
/** #2228: output budget (tokens) of the decision side call. Default 200
* (same ceiling class as SIDE_REQUEST_MAX_TOKENS). Must be > 0. */
nudgeDecisionMaxTokens?: number;
/** #5683: forbid UNPROMPTED model-initiated compression. When true at some
* level, a compress call is refused unless a compression nudge actually
* reached the model in the current turn (post-gate delivery fact: the
* round's nudgeOutcome was "injected" — advisory, decide-directive and
* emergency injections all arm it; loop rounds within the same turn keep
* the arm, the next un-nudged turn disarms). Refusal sends the model an
* error receipt instead of folding. bili's own overflow backstop folds
* (preflight/auto-fold, no model call involved) are exempt — availability
* is unaffected. Host-only (not passed to the kernel); off unless
* explicitly enabled at some level. Deepest level wins PER FIELD across
* global → provider → model, like every other compress field. */
requireNudge?: boolean;
/** #5683: forbid OUT-OF-BOUNDS model-initiated compression. When true at
* some level, every submitted range must stay within what bili currently
* recommends folding — the same live compressible set the nudge text and
* acp_status show (this request's prepare-time offer when present, else a
* live recompute; viable + dead-ref + minCompressRange filters). Judgment
* is by COVERAGE, not equality: subdividing a recommended span into
* smaller chunks passes, and messages already folded into active blocks
* (refolds updating an existing summary) are exempt. A range carrying any
* other live message is refused whole with an error receipt naming the
* offending span(s). Host-only; off unless explicitly enabled at some
* level. Deepest level wins PER FIELD across global → provider → model. */
requireRecommendation?: boolean;
/** Trailing messages never offered for compression
* (kernel `preserveRecentMessages`). */
preserveRecentMessages?: number;
Expand Down Expand Up @@ -2416,6 +2440,7 @@ const KNOWN_TOP_LEVEL_KEYS = new Set([
const COMPRESS_SETTING_FIELDS = new Set([
"modelContextLimit", "maxContextLimit", "emergencyThresholdPercent",
"nudgeGrowthTokens", "minPressureBenefitTokens", "tierNudgeTokens", "nudgeModelDecided", "nudgeDecisionMaxTokens",
"requireNudge", "requireRecommendation",
"preserveRecentMessages", "preserveRecentTokens",
"minCompressRange", "minCompressRangeChars",
"outputHeadroomMaxPct", "tiers", "protectedLatestTools", "protectedTools",
Expand Down Expand Up @@ -3186,6 +3211,13 @@ export function parseCompressSettings(v: unknown): (CompressSettings & { injectT
}
out[key] = obj[key];
}
// #5683: unprompted/out-of-bounds compression policy gates — plain booleans,
// off unless explicitly set (an explicit false at a deeper level disables).
for (const key of ["requireNudge", "requireRecommendation"] as const) {
if (!(key in obj)) continue;
if (typeof obj[key] !== "boolean") ok = false;
else out[key] = obj[key];
}
// #3211: an explicit 0 is meaningful (legacy any-pending), so validate
// >= 0 instead of takeNumber's plain finite check.
if ("minPressureBenefitTokens" in obj) {
Expand Down
21 changes: 20 additions & 1 deletion src/server/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import path from "node:path";
import { performance } from "node:perf_hooks";
import { defaultPrompts, type CompressionCore, type Config, type PackSurface, type Prompts } from "acp-kernel";
import { conversationSignalAnthropic, conversationSignalGoogle, conversationSignalOpenai, conversationIdentityResponses, conversationSignalResponses, googleSystemText, type AnthropicRequestBody, type GoogleRequestBody, type OpenAIRequestBody, type ResponsesRequestBody } from "acp-kernel/wire";
import { resolveCompress, resolveCompressPrompts, resolveCompressSurfaceDetailed, resolveRequestConfig } from "../compress-settings.js";
import { resolveCompress, resolveCompressPrompts, resolveCompressSurfaceDetailed, resolveRequestConfig, type CompressSettings } from "../compress-settings.js";
import { storeUnpromptedGate } from "../unprompted-gate.js";
import { FALLBACK_EFFECTIVE_WINDOW_FLOOR, findRoute, findRouteKey, lookupContextLimit, resolveConfiguredOutputLimit, resolveCompressProtocol, resolveDeclaredProtocol, resolveModelEntry, type ProxyOptions } from "../config.js";
import { resolveProxyDecision } from "../upstream-proxy.js";
import { contextFromRegistry, peekRegistryContext, peekRegistryOutputLimit, peekRegistryPriceProfile } from "../registry.js";
Expand Down Expand Up @@ -2495,6 +2496,9 @@ async function handleRequest(
// #2155: self-heal evaluates once per HTTP request — the refold
// re-runs prepare below but must not double-count a round.
let selfHealRoundDone = false;
// #5683: this round's merged three-level compress settings, captured
// so the post-prepare gate stamp below can read the policy knobs.
let preparedCs: CompressSettings | undefined;
const runPreparedPipeline = async (
respondFailFast: boolean,
overflowWindow?: number,
Expand All @@ -2504,6 +2508,7 @@ async function handleRequest(
// validity (the last one deletes it — see session.ts).
tickPostRebuildAnchor(session);
const cs = resolveCompress(opts.routes, providerDestUrl, requestModel, opts.compress);
preparedCs = cs;
// #2228: model-decided nudge timing — resolved through the standard
// three-level compress cascade; presence of the object enables the
// side-call decision path at tier-1 arms (off unless explicitly set).
Expand Down Expand Up @@ -2737,6 +2742,20 @@ async function handleRequest(
degradeAvailable: opts.compress.injectTool && !knobNoInjectTool(),
}, log);
}
// #5683: stamp this round's compression-prompt facts for the
// unprompted/out-of-bounds gates (applyRanges). armed tracks the
// SAME post-gate delivery fact as the self-heal credit above —
// only an actually delivered nudge ("injected") arms; "gated"
// rounds held by the emergency backoff never did reach the model.
// Stamped on EVERY round (not just nudge-carrying ones) so an
// un-nudged round disarms a stale arm; loop re-requests within
// the same turn do not re-run prepare, so the arm persists across
// rounds of one turn and dies with the next un-nudged turn.
storeUnpromptedGate(session, {
armed: prepared.nudgeOutcome === "injected",
requireNudge: preparedCs?.requireNudge === true,
requireRecommendation: preparedCs?.requireRecommendation === true,
});
// A locally-served codex compaction forge never reaches an
// upstream: preflight would burn summarization rounds for a
// reply that is already forged (#3008 prompt form).
Expand Down
Loading
Loading