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
4 changes: 2 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ This index is generated from `website/config-reference/*.yaml` — edit the seed
| `compress.maxEstimateHoldRounds` | number | 2 | — | Backstop for the phantom-pressure guard (#5865 ruling): how many CONSECUTIVE estimate-grade rounds the pressure band may wait before firing on the estimate anyway. Capped so an upstream that never reports usage (anonymous zero-baseline forks, silent backends — where the local estimate is the only pressure signal) still gets automatic compression. 0 = no waiting at all. Only consulted while requireUsageSampleForPressure is on. |
| `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.requireNudge` | boolean | off (unset) | — | Forbid UNPROMPTED model-initiated compression (#5683/#5943): a compress call is refused unless a NUDGE CREDIT is live — a window of 20 MODEL rounds minted by the last compression nudge that actually reached the model (advisory, decide-directive and emergency injections all mint; "gated"/"vetoed"/"skipped" never do), refreshed by each new injection and decayed one per un-nudged model round (non-model request shapes hold it open). Successful folds do NOT consume the credit, so finishing a nudge's recommended cleanup over several rounds stays legal. 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. |
Expand Down Expand Up @@ -1439,7 +1439,7 @@ For each request, the proxy resolves the settings by longest-URL-prefix match (t
- **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.
- `requireNudge` (timing gate): a model-initiated compress call is refused unless a NUDGE CREDIT is live (#5943) — a window of 20 MODEL ROUNDS minted by the last compression nudge that actually reached the model (the post-gate delivery fact `nudgeOutcome === "injected"`: advisory nudges, model-decided directives and emergency nudges all mint it, while "gated"/"vetoed"/"skipped" rounds never do). Each new injection refreshes the window; every un-nudged MODEL round decays it by one; non-model request shapes (countTokens / responsesCompact / local forges) hold it open without refreshing, and loop re-requests within one turn do not re-stamp, so they burn nothing either. Successful folds do NOT consume the credit — finishing a nudge's recommended cleanup over several rounds (the multi-round-cleanup refusal storm #5943 reports) is exactly what the nudge asked for; long-term strictness survives because nudges are growth-driven, so quiet sessions lapse back to strict refusal after 20 un-nudged model rounds while growing ones keep refreshing the window. The refusal is an error receipt telling the model that unprompted compression is disabled here until a nudge appears.
- `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.

Expand Down
4 changes: 2 additions & 2 deletions docs/configuration.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@
| `compress.maxEstimateHoldRounds` | number | 2 | — | 幻影压力守卫的兜底(#5865 裁定):压力带最多连等多少轮估算读数就必须照估算动手。兜底保证从不回传计费的会话(匿名零基线 fork、静默后端——本地估算是它们唯一的压力信号)也能拿到自动压缩。0=完全不等待。仅在 requireUsageSampleForPressure 开启时生效。 |
| `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.requireNudge` | boolean | off (unset) | — | 禁止未提示的模型主动压缩(#5683/#5943):只有 nudge 信用存活时才允许 compress——即距最近一次真正送达模型的压缩提醒不超过 20 个 MODEL 轮的窗口(advisory、模型自决指令、紧急提醒都算铸券;"gated"/"vetoed"/"skipped" 不算);每次新注入刷新窗口,每个未提示的模型轮衰减 1(非模型请求形态既不刷新也不衰减)。成功折叠**不消耗**信用,分几轮压完 nudge 推荐的剩余范围仍然合法。否则拒绝并回错误回执;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 | — | 最近的消息软保护、免于折叠。 |
Expand Down Expand Up @@ -1436,7 +1436,7 @@ ACP 原生 agent(当前为 `pi` 扩展)会在每个进程内向代理上报
- **默认值:** *(关闭,除非显式设置)*
- **状态:** ACTIVE (#5683)
- **说明:** 针对会自行主动压缩的模型的策略门禁(现场案例:qwen 27b 没有任何提示就调用 compress)。两者都是宿主侧字段——不传入内核——与其他 compress 字段一样按字段在 global → provider → model 三级合并,可在 web 面板快速配置卡片或任意配置层级编辑,从下一个请求起热生效。
- `requireNudge`(时序门禁):只有**当前轮次**压缩提醒真正送达过模型(post-gate 投递事实 `nudgeOutcome === "injected"`——advisory 提醒、模型自决指令、紧急提醒都算;"gated"/"vetoed"/"skipped" 不算)才允许模型发起 compress,否则拒绝并回一条错误回执,告知未提示压缩在本线路被禁用、等提醒出现再压。武装在同一轮内的 loop 重请求间保持(一轮响应一个提醒可连续折几次),到下一个未提示轮次自动解除。
- `requireNudge`(时序门禁):只有 **nudge 信用**存活(#5943)才允许模型发起 compress——即距最近一次真正送达模型的压缩提醒(post-gate 投递事实 `nudgeOutcome === "injected"`——advisory 提醒、模型自决指令、紧急提醒都算铸券;"gated"/"vetoed"/"skipped" 不算)不超过 20 个 MODEL 轮的窗口。每次新注入刷新窗口;每个未提示的 MODEL 轮衰减 1;非模型请求形态(countTokens / responsesCompact / 本地 forge)既不刷新也不衰减;同一轮内的 loop 重请求不重新盖章、同样不烧轮次。成功折叠**不消耗**信用——分几轮压完 nudge 推荐的剩余范围正是提醒所要求的(#5943 报告的「隔轮清理被连拒」病理即由此修复);长期严格性由增长驱动的提醒保证:安静会话连续 20 个未提示模型轮后回到严格拒绝,增长中的会话则持续刷新窗口。拒绝时回一条错误回执,告知未提示压缩在本线路被禁用、等提醒出现再压。
- `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 而不是全局开启。无新增环境变量;未显式开启任何一级时行为不变。

Expand Down
29 changes: 19 additions & 10 deletions src/server/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2947,19 +2947,28 @@ 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.
// #5683/#5943: stamp this round's compression-prompt facts for
// the unprompted/out-of-bounds gates (applyRanges). nudgeInjected
// tracks the SAME post-gate delivery fact as the self-heal credit
// above — only an actually delivered nudge ("injected") mints;
// "gated" rounds held by the emergency backoff never did reach
// the model. #5943: the gate consumes a CREDIT, not this-round
// arm — an injected round re-mints NUDGE_CREDIT_ROUNDS model
// rounds of eligibility and every un-nudged model round decays
// one, so a model that comes back one or several rounds later to
// finish a nudge's recommended cleanup is still inside its window
// instead of refused (the single-round arm refused all of them).
// Stamped on EVERY round (not just nudge-carrying ones) so
// un-nudged rounds keep decaying; loop re-requests within the
// same turn do not re-run prepare, so the window persists across
// them. Non-model shapes pass modelRound:false — they hold the
// window open without refreshing it, mirroring the preflight
// exclusion predicate below.
storeUnpromptedGate(session, {
armed: prepared.nudgeOutcome === "injected",
nudgeInjected: prepared.nudgeOutcome === "injected",
requireNudge: preparedCs?.requireNudge === true,
requireRecommendation: preparedCs?.requireRecommendation === true,
modelRound: !countTokens && !responsesCompact && !prepared.codexForge && !prepared.threadsLane,
});
// A locally-served codex compaction forge never reaches an
// upstream: preflight would burn summarization rounds for a
Expand Down
16 changes: 9 additions & 7 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { WireProtocol } from "./util.js";
import type { InboundSlot, SeamSlot } from "./cache-ledger.js";
import { isResponsesTurnSeparatorId } from "./turn-separator.js";
import type { FoldReconcileResult } from "./fold-reconcile.js";
import type { UnpromptedGate } from "./unprompted-gate.js";
import type { UnpromptedGateState } from "./unprompted-gate.js";

export type BlockView = { text: string; count: number };

Expand Down Expand Up @@ -337,12 +337,14 @@ export type Session = {
* In-memory only: the abort→resume causality spans one process
* lifetime, and a restart replants nothing. */
abortReanchor?: { anchor: number; at: number };
/** #5683: the conversation's latest model-round compression-prompt facts
* for the unprompted/out-of-bounds gates (src/unprompted-gate.ts).
* Re-stamped on every prepare; in-memory only — a restart reads the
* all-off default until the first round re-stamps, which is safe: no
* tool call can reach applyRanges before its own round's prepare. */
unpromptedGate?: UnpromptedGate;
/** #5683/#5943: the conversation's live nudge CREDIT plus the policy
* flags, for the unprompted/out-of-bounds gates (src/unprompted-gate.ts).
* Re-stamped on every prepare (injected rounds re-mint the window,
* un-nudged model rounds decay it); in-memory only — a restart lapses
* any live credit to the all-off default until the first round
* re-stamps, which is safe: no tool call can reach applyRanges before
* its own round's prepare. */
unpromptedGate?: UnpromptedGateState;
/** #5981: the last model stream on this conversation was CUT before its
* terminal event (src/plugin.ts emitUpstreamTruncation sites). A tool
* call dispatched out of such a stream can carry EMPTY arguments —
Expand Down
Loading
Loading