diff --git a/CONFIGURATION.md b/CONFIGURATION.md index fe422b9461..6d1effaf68 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -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. | @@ -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` diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index 116ca6fe51..967db95993 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -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 软保护、免于折叠。 | @@ -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` diff --git a/src/compress-settings.ts b/src/compress-settings.ts index d11de14ebb..05d59005ab 100644 --- a/src/compress-settings.ts +++ b/src/compress-settings.ts @@ -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), diff --git a/src/config.ts b/src/config.ts index e455f32e4b..db13c958cf 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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; @@ -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", @@ -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) { diff --git a/src/server/handle.ts b/src/server/handle.ts index f944ca4cac..fd8ceccdd9 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -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"; @@ -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, @@ -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). @@ -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). diff --git a/src/session.ts b/src/session.ts index dc5f83ac88..6058106292 100644 --- a/src/session.ts +++ b/src/session.ts @@ -12,6 +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"; export type BlockView = { text: string; count: number }; @@ -336,6 +337,12 @@ 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; }; id: string; /** Identity / descriptive metadata. Populated on first request. */ diff --git a/src/stream.ts b/src/stream.ts index 2796e67ff4..4816148001 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -1,5 +1,5 @@ import { createHash, randomBytes } from "node:crypto"; -import { collectBlockContent, countMessageTokens, defaultCountTokens, formatRanges, formatTagTime, hideConsumedCompressCalls, indexToRef, parseBoundary, storeCoveredOriginals, viableRanges, type CompressionCore, type Config, type CompressionBlock, type CoreMessage, type CompressionState, type NudgeDecision, type CompressParseDiagnostics, refToIndex } from "acp-kernel"; +import { collectBlockContent, countMessageTokens, defaultCountTokens, formatRanges, formatTagTime, hideConsumedCompressCalls, indexToRef, parseBoundary, storeCoveredOriginals, viableRanges, type CompressionCore, type Config, type CompressionBlock, type CompressibleRange, type CoreMessage, type CompressionState, type NudgeDecision, type CompressParseDiagnostics, refToIndex } from "acp-kernel"; import { loadConfigFile, resolveMessageTimestamps } from "./config.js"; import { excludeDeadRefRanges, handleAcpStatus, liveCompressionRanges } from "./acp-status.js"; import { handleAcpCache, prefixTokensBeforeRef, recordCacheFoldsFromBlocks } from "./cache-ledger.js"; @@ -19,6 +19,7 @@ import { attachSubagentSessions, subagentSessionNote, subagentSessionsOf, syncSu import { METADATA_DRIFT_ESCALATED } from "./fold-reconcile.js"; import { safePrefix, safeSuffix, scrubLoneSurrogates } from "./text-safe.js"; import { applyConfiguredCompression } from "./external-summary-compress.js"; +import { unpromptedGateOf } from "./unprompted-gate.js"; import { describeUncoveredRuns, shortTokenCount, uncoveredHistoryRuns } from "./uncovered.js"; export type RewriteCtx = { @@ -947,11 +948,107 @@ export function supersededRefRejection): Array<[number, number]> { + if (intervals.length === 0) return []; + const sorted = intervals.slice().sort((a, b) => a[0] - b[0] || a[1] - b[1]); + const out: Array<[number, number]> = [sorted[0]]; + for (let i = 1; i < sorted.length; i++) { + const last = out[out.length - 1]; + if (sorted[i][0] <= last[1] + 1) last[1] = Math.max(last[1], sorted[i][1]); + else out.push(sorted[i]); + } + return out; +} + +/** The recommended intervals for THIS call: this request's prepare-time offer + * when present (the exact list the nudge text / acp_status showed), else a + * live recompute (plugin-lane ctxs carry no prepare snapshot). Same filters as + * the advertised surfaces: viable + dead-ref exclusion + minCompressRange. */ +function recommendedIntervals(ctx: RewriteCtx): Array<[number, number]> { + let ranges: CompressibleRange[] | undefined; + if (ctx.lastNudge) { + ranges = excludeDeadRefRanges(viableRanges(ctx.lastNudge.compressibleRanges ?? []), ctx.session.state.deadRefs); + } else { + ranges = liveCompressionRanges(ctx)?.ranges; + } + const minChars = ctx.config.compress.minCompressRange; + const out: Array<[number, number]> = []; + for (const r of ranges ?? []) { + if (minChars > 0 && (r.chars ?? r.tokens * 4) < minChars) continue; + const s = refToIndex(r.startRef); + const e = refToIndex(r.endRef); + if (s !== null && e !== null && s <= e) out.push([s, e]); + } + return mergeIntervals(out); +} + +/** Ref-index runs covered by ACTIVE blocks — the refold carve-out set. */ +function coveredRefRuns(state: CompressionState): Array<[number, number]> { + const byRaw = state.messageRefs?.byRaw ?? {}; + const nums = new Set(); + for (const block of state.blocks) { + if (!block.active) continue; + for (const raw of block.effectiveMessageIds) { + const ref = byRaw[raw]; + if (!ref) continue; + const n = refToIndex(ref); + if (n !== null) nums.add(n); + } + } + const sorted = [...nums].sort((a, b) => a - b); + const runs: Array<[number, number]> = []; + for (const n of sorted) { + const last = runs[runs.length - 1]; + if (last && n === last[1] + 1) last[1] = n; + else runs.push([n, n]); + } + return runs; +} + +/** Submitted ranges whose LIVE portion escapes the recommendation set. Empty + * array = every span stays within bounds (or is a pure refold). Unresolvable + * refs are skipped — the downstream gates own those receipts. */ +function outOfRecommendedRanges(ranges: ParsedRange[], ctx: RewriteCtx): string[] { + const exempt = mergeIntervals([...recommendedIntervals(ctx), ...coveredRefRuns(ctx.session.state)]); + const offending: string[] = []; + for (const range of ranges) { + const s = refToIndex(range.startRef); + const e = refToIndex(range.endRef); + if (s === null || e === null || s > e) continue; + let pos = s; + let within = false; + for (const [a, b] of exempt) { + if (b < pos) continue; + if (a > pos) break; + pos = Math.max(pos, b) + 1; + if (pos > e) { within = true; break; } + } + if (!within) offending.push(`${range.startRef}–${range.endRef}`); + } + return offending; +} + export function applyRanges(parsed: ReturnType, ctx: RewriteCtx, opts?: { loopTracking?: boolean }): ProxyToolResult { // #2146: preflight overflow-compress is bili's OWN internal lane — its // failures have separate reporting (noteSkip) and must not arm the // model-facing circuit breaker on attempts the model never made. const trackLoop = opts?.loopTracking !== false; + // #5683: unprompted-compression policy gate (timing). Model-initiated calls + // only — bili's own overflow backstop folds ride loopTracking:false above + // and never reach this check, so availability is unaffected even when the + // nudge channel itself is suppressed. + const gate = trackLoop ? unpromptedGateOf(ctx.session) : undefined; + if (gate?.requireNudge && !gate.armed) { + ctx.log("[acp-proxy: refused unprompted compress — no nudge was injected this turn (compress.requireNudge)]"); + return compressResult("[Compression REFUSED: unprompted compression is disabled on this route (compress.requireNudge) — you may only call compress in a round where a compression nudge was actually injected, and no nudge accompanied this round. Continue the task without folding; when a nudge appears, follow its ranges.]", "refused", 0, "unprompted"); + } let { ranges, diagnostics } = parsed; if (ranges.length === 0) { ctx.log("[acp-proxy: compress call had no valid ranges; nothing compressed.]"); @@ -1043,11 +1140,24 @@ export function applyRanges(parsed: ReturnType, ctx: ctx.log(`[acp-proxy: refused compress citing superseded ref(s): ${supersededRes.rejection} (#4316 PR3/3)]`); return compressResult(`[Compression REFUSED: ${supersededRes.rejection} The refs in this call no longer identify the same messages and folding them would compress the wrong span. Re-issue the compress call with the updated labels.]`, "refused", 0, "superseded-ref"); } + // supersededRefRejection returns a fresh identity-preserving copy of EVERY + // range (a plain {...r} when nothing was translated) — adopt it + // unconditionally so the scope gate below judges the exact refs the fold + // will act on. + ranges = supersededRes.translatedRanges; if (supersededRes.translated > 0) { - ranges = supersededRes.translatedRanges; ctx.log(`[acp-proxy: translated ${supersededRes.translated} superseded ref(s) onto current labels (identity-preserving, #4797): ${ranges.map((r) => `${r.startRef}–${r.endRef}`).join(", ")}]`); - } else { - ranges = supersededRes.translatedRanges; + } + // #5683: out-of-bounds policy gate (scope). Runs only on live, normalized + // refs (post-#1001 order fix + post-#4316 superseded translation), so the + // coverage judgment compares like-for-like against the advertised set. + if (gate?.requireRecommendation) { + const offending = outOfRecommendedRanges(ranges, ctx); + if (offending.length > 0) { + const listed = offending.slice(0, 3).join(", ") + (offending.length > 3 ? ` (+${offending.length - 3} more)` : ""); + ctx.log(`[acp-proxy: refused out-of-recommendation compress — span(s) ${listed} outside the current recommendation set (compress.requireRecommendation)]`); + return compressResult(`[Compression REFUSED: out-of-bounds compression is disabled on this route (compress.requireRecommendation) — span(s) ${listed} contain content bili does not currently recommend folding. Call acp_status for the current compressible ranges and stay within them: subdividing a listed span into smaller chunks is fine, and re-folding an already-compressed block to update its summary is fine.]`, "refused", 0, "out-of-recommendation"); + } } if (ctx.messages.length > 0) { const ids = ctx.messages.slice(0, 10).map((m) => `${saltedMsgIdForLog(m.id)}(${(m.text ?? "").length}c)`).join(", "); diff --git a/src/unprompted-gate.ts b/src/unprompted-gate.ts new file mode 100644 index 0000000000..e38f900e38 --- /dev/null +++ b/src/unprompted-gate.ts @@ -0,0 +1,37 @@ +// #5683: unprompted-compression policy gates. Two opt-in knobs +// (compress.requireNudge / compress.requireRecommendation) refuse model-initiated +// compress calls that bili did not ask for. The per-round FACTS they judge on +// are stamped at prepare (src/server/handle.ts) onto session.transient — +// in-memory only, re-stamped every model round — and read here by applyRanges +// (src/stream.ts), the single execution point for every model-initiated compress +// across all lanes (proxy loop + POST /__bili/plugin/tool). +import type { Session } from "./session.js"; + +export interface UnpromptedGate { + /** A compression nudge actually reached the model in the latest round + * (post-gate delivery fact: nudgeOutcome === "injected" — advisory, + * decide-directive and emergency injections all count; "gated"/"vetoed"/ + * "skipped" never arm). */ + armed: boolean; + requireNudge: boolean; + requireRecommendation: boolean; +} + +const OFF: UnpromptedGate = { armed: false, requireNudge: false, requireRecommendation: false }; + +export function storeUnpromptedGate(session: Session, gate: UnpromptedGate): void { + const t = session.transient ?? (session.transient = {}); + t.unpromptedGate = { ...gate }; +} + +/** All-off when nothing was stamped yet (fresh process, unit-test ctxs, lanes + * without a prepare) — the default configuration changes no behavior. */ +export function unpromptedGateOf(session: Session): UnpromptedGate { + const g = session.transient?.unpromptedGate; + if (!g || typeof g !== "object") return OFF; + return { + armed: g.armed === true, + requireNudge: g.requireNudge === true, + requireRecommendation: g.requireRecommendation === true, + }; +} diff --git a/src/web/client.ts b/src/web/client.ts index b87d9e5b20..c90300d414 100644 --- a/src/web/client.ts +++ b/src/web/client.ts @@ -3910,6 +3910,8 @@ if (d.keepalive) kv(parts, t("det.keepalive"), d.keepalive.tierLabel + " · " + const ab = (cp && cp.absorb && typeof cp.absorb === "object" && !Array.isArray(cp.absorb)) ? cp.absorb : null; abRow.inp.checked = ab ? ab.enabled === true : false; cafRow.inp.checked = cp ? cp.compressAtFoldPoint === true : false; + rnRow.inp.checked = cp ? cp.requireNudge === true : false; + rrRow.inp.checked = cp ? cp.requireRecommendation === true : false; const pv = (cp && typeof cp.promptPack === "string") ? cp.promptPack : "default"; while (packSel.options.length > 0) packSel.removeChild(packSel.lastChild); (packNames.length ? packNames : ["default", "lean"]).forEach((name) => { @@ -4073,6 +4075,43 @@ if (d.keepalive) kv(parts, t("det.keepalive"), d.keepalive.tierLabel + " · " + abHint.className = "dim small"; abHint.textContent = t("cfg.q_absorb_hint"); abRow.ctl.appendChild(abHint); + // #5683: unprompted/out-of-bounds compression gates (compress.* booleans). + const rnRow = row("quick-require-nudge", t("cfg.q_require_nudge")); + qCtrls.push(rnRow.inp); + rnRow.inp.addEventListener("change", () => commit((d) => { + if (rnRow.inp.checked) { + if (!compressOf(d)) d.compress = {}; + d.compress.requireNudge = true; + } else { + const c = compressOf(d); + if (c) { + delete c.requireNudge; + if (Object.keys(c).length === 0) delete d.compress; + } + } + })); + const rnHint = document.createElement("span"); + rnHint.className = "dim small"; + rnHint.textContent = t("cfg.q_require_nudge_hint"); + rnRow.ctl.appendChild(rnHint); + const rrRow = row("quick-require-rec", t("cfg.q_require_rec")); + qCtrls.push(rrRow.inp); + rrRow.inp.addEventListener("change", () => commit((d) => { + if (rrRow.inp.checked) { + if (!compressOf(d)) d.compress = {}; + d.compress.requireRecommendation = true; + } else { + const c = compressOf(d); + if (c) { + delete c.requireRecommendation; + if (Object.keys(c).length === 0) delete d.compress; + } + } + })); + const rrHint = document.createElement("span"); + rrHint.className = "dim small"; + rrHint.textContent = t("cfg.q_require_rec_hint"); + rrRow.ctl.appendChild(rrHint); const packSel = document.createElement("select"); packSel.className = "field-input mono"; qCtrls.push(packSel); diff --git a/src/web/i18n.ts b/src/web/i18n.ts index 3d29b3ea3f..a585df54a0 100644 --- a/src/web/i18n.ts +++ b/src/web/i18n.ts @@ -572,6 +572,10 @@ export const MESSAGES: Record> = { "cfg.q_absorb_hint": "默认只吸含图结果(#3422);mediaOnly: false 可恢复吸全文。子项(minToolTokens、contextThresholdPct、excludeTools)请在下方 JSON 编辑框写", "cfg.q_cafp": "压缩折叠是否原位置替换(compressAtFoldPoint,默认关)——把折叠产生的 compress 调用对放到它自己的折叠点处替换,而不是留在历史尾部", "cfg.q_cafp_hint": "针对 qwen3-27b 这类注意力机制有问题的小模型很有用:避免模型把历史尾部的工具交换误读成新命令;取消勾选 = 回到旧版布局(字节级一致)。可选的“清未签名思考”开关(clearThinking)只能走 env / JSON", + "cfg.q_require_nudge": "禁止未提示压缩(compress.requireNudge)——只在 bili 实际注入压缩提醒的同一轮允许模型调用 compress,其余一律拒绝并回错误回执(默认关)", + "cfg.q_require_nudge_hint": "针对会自行乱压的模型(如 qwen 27b)。bili 自己的溢出兜底折叠不受影响;也可只对特定 provider/model 开启(JSON 编辑框)", + "cfg.q_require_rec": "禁止越界压缩(compress.requireRecommendation)——只允许压缩 bili 当前推荐的范围,越界即拒(默认关)", + "cfg.q_require_rec_hint": "推荐范围 = nudge / acp_status 展示的可压缩列表;把推荐跨度拆小段、重折已有块更新摘要都算合规", "cfg.q_pack": "压缩提示词包(compress.promptPack)", "cfg.q_nudge": "压缩增长阈值 tokens(compress.nudgeGrowthTokens)", "cfg.q_nudge_step": "步进 5000", @@ -1306,6 +1310,10 @@ export const MESSAGES: Record> = { "cfg.q_absorb_hint": "by default only image-bearing results are absorbed (#3422); set mediaOnly: false to absorb full text. Sub-fields (minToolTokens, contextThresholdPct, excludeTools) go in the JSON editor below", "cfg.q_cafp": "Fold replacement in place (compressAtFoldPoint, default off) — the fold's compress call/result pair replaces the region at its own fold point instead of staying tail-adjacent", "cfg.q_cafp_hint": "very useful for small models with attention issues such as qwen3-27b: stops them misreading the trailing tool exchange as a fresh command; uncheck = byte-identical legacy layout. The optional clear-thinking companion (clearThinking) stays env/JSON-only", + "cfg.q_require_nudge": "Forbid unprompted compression (compress.requireNudge) — compress may only be called in a round where a nudge was actually injected; anything else is refused with an error receipt (default off)", + "cfg.q_require_nudge_hint": "for models that fold on their own (e.g. qwen 27b). bili's own overflow backstop folds are unaffected; can also be scoped per provider/model in the JSON editor", + "cfg.q_require_rec": "Forbid out-of-bounds compression (compress.requireRecommendation) — only ranges bili currently recommends may be folded; anything else is refused (default off)", + "cfg.q_require_rec_hint": "recommended = the compressible list shown by nudge / acp_status; subdividing a listed span or re-folding an existing block to update its summary stays legal", "cfg.q_pack": "Compression prompt pack (compress.promptPack)", "cfg.q_nudge": "Compress growth threshold tokens (compress.nudgeGrowthTokens)", "cfg.q_nudge_step": "step of 5000", diff --git a/tests/e2e/e2e-native-pi.test.ts b/tests/e2e/e2e-native-pi.test.ts index e71682096d..7f0ee66eb3 100644 --- a/tests/e2e/e2e-native-pi.test.ts +++ b/tests/e2e/e2e-native-pi.test.ts @@ -458,6 +458,17 @@ function piRun( cwd: ctx.piCwd, env: { ...cleanEnv(), + // This suite documents the SPAWN-posture package-native contract: + // instance record + proxy origin, per-request wire stamps, the + // shared-proxy delegate-child ACP channel (#2268), /__bili/plugin/status + // over HTTP. #5428 flipped the pi lane DEFAULT to inline (in-process + // engine, no instance record) and this suite was not updated with it: + // primary-lane tests then pass for the wrong reason while the + // instance-record and delegate-child stamp assertions fail structurally + // (CI sentinel incident, #4599). Pin the legacy posture so this + // coverage stays deterministic; inline posture is covered by + // tests/pi-inline-ws.test.ts and tests/pi-native.test.ts. + BILI_PI_INLINE: "0", PI_CODING_AGENT_DIR: ctx.piAgentDir, XDG_CONFIG_HOME: ctx.xdg.config, XDG_CACHE_HOME: ctx.xdg.cache, diff --git a/tests/e2e/e2e-pi-codex-ws.test.ts b/tests/e2e/e2e-pi-codex-ws.test.ts index e73027ad06..a8df48ae7c 100644 --- a/tests/e2e/e2e-pi-codex-ws.test.ts +++ b/tests/e2e/e2e-pi-codex-ws.test.ts @@ -722,7 +722,14 @@ test("E: two subagent-style sessions share one proxy — distinct state, identic assert.ok(r.stdout.includes("WS_E2E_OK")); }; - ok("X warmup", await piRun(ctxX, "alpha marker ALPHA_X_SEED")); + // #5428 flipped the pi lane's DEFAULT posture to inline (in-process proxy): + // left unpinned, X's proxy would die with its pi process and unregister its + // instance record — nothing left to discover, nothing for Y to attach to. + // This scenario pins the legacy spawn/attach posture it was built on; + // scenarios A–D intentionally ride the new inline default. + const legacyPosture: NodeJS.ProcessEnv = { BILI_PI_INLINE: "0" }; + + ok("X warmup", await piRun(ctxX, "alpha marker ALPHA_X_SEED", { extraEnv: legacyPosture })); const origin = proxyOriginOf(ctxX); assert.ok(origin, "X's proxy origin discovered from its instance record"); // Y attaches to X's proxy (real subagent posture: inherited @@ -733,7 +740,7 @@ test("E: two subagent-style sessions share one proxy — distinct state, identic // Same tool args ({}) from both sessions, interleaved. const [xStatus, yStatus] = await Promise.all([ - piRun(ctxX, "WS_STATUS alpha status", { resume: true }), + piRun(ctxX, "WS_STATUS alpha status", { resume: true, extraEnv: legacyPosture }), piRun(ctxY, "WS_STATUS beta status", { resume: true, extraEnv: yEnv }), ]); ok("X status", xStatus); @@ -763,7 +770,7 @@ test("E: two subagent-style sessions share one proxy — distinct state, identic // Concurrent plain turns, same proxy, no cross-talk afterwards. const [xFin, yFin] = await Promise.all([ - piRun(ctxX, "concurrent X tail", { resume: true }), + piRun(ctxX, "concurrent X tail", { resume: true, extraEnv: legacyPosture }), piRun(ctxY, "concurrent Y tail", { resume: true, extraEnv: yEnv }), ]); ok("X concurrent", xFin); diff --git a/tests/issue5683-forbid-unprompted-compress.test.ts b/tests/issue5683-forbid-unprompted-compress.test.ts new file mode 100644 index 0000000000..d87f7544d5 --- /dev/null +++ b/tests/issue5683-forbid-unprompted-compress.test.ts @@ -0,0 +1,254 @@ +// #5683: forbid unprompted / out-of-bounds model-initiated compression. +// Two opt-in knobs (compress.requireNudge / compress.requireRecommendation) +// enforced in applyRanges — the single execution point for every model-initiated +// compress across lanes. Gate discipline (#3617): BOTH directions asserted per +// gate — fires when it should, stays silent when it should not. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { CoreMessage, CompressibleRange, NudgeDecision } from "acp-kernel"; +import { createCore, createInitialState, assignRefs, emptyRefMap, defaultConfig } from "acp-kernel"; +import { parseCompressInput } from "../src/compress-tool.ts"; +import { parseCompressSettings } from "../src/config.ts"; +import { mergeCompress } from "../src/compress-settings.ts"; +import { liveCompressionRanges } from "../src/acp-status.js"; +import { applyRanges, type RewriteCtx } from "../src/stream.ts"; +import { storeUnpromptedGate } from "../src/unprompted-gate.ts"; + +const SUM = "SUMMARY-PAYLOAD-LONG-ENOUGH-FOR-THE-KERNEL-MIN-LENGTH-CHECK-XYZ"; + +type Ctx = Omit & { log: (m: string) => void; logs: string[] }; + +function makeCtx(messages: CoreMessage[], overrides?: Record): Ctx { + const logs: string[] = []; + return { + core: createCore(), + config: defaultConfig(200000, overrides as never), + messages, + session: { + id: "issue5683-test", + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 100000, compressCreditTokens: 0, contextTokens: 0, retrieveCalls: 0, retrieveHits: 0, retrieveMisses: 0, storedBytes: 0, storeBytesSaved: 0, rangeRestores: 0 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + pendingRetrievals: [], + }, + log: (m: string) => { logs.push(m); }, + logs, + }; +} + +function withRefs(ctx: Ctx): Ctx { + const res = assignRefs(ctx.messages, { existing: emptyRefMap(), nextIndex: 0 }); + ctx.session.state.messageRefs = res.map; + return ctx; +} + +function textMsg(id: string, role: "user" | "assistant", text: string): CoreMessage { + return { id, role, contentType: "text", text }; +} + +/** Nine 5000-char messages; preserveRecentMessages 2 keeps m8-m9 protected, + * so m1-m7 are the live fold space. */ +function freshCtx(): Ctx { + const msgs = Array.from({ length: 9 }, (_, i) => textMsg(`raw_${i + 1}`, i % 2 === 0 ? "user" : "assistant", "x".repeat(5000))); + return withRefs(makeCtx(msgs, { preserveRecentMessages: 2, preserveRecentTokens: 0 })); +} + +function offerRange(startRef: string, endRef: string, count: number, tokens = 8000): CompressibleRange { + return { startRef, endRef, count, tokens, chars: tokens * 4, toolPct: 0, textPct: 100 }; +} + +function setOffer(ctx: Ctx, ranges: CompressibleRange[]): void { + ctx.lastNudge = { compressibleRanges: ranges, tier: null } as unknown as NudgeDecision; +} + +function runApply(ctx: Ctx, args: unknown, opts?: { loopTracking?: boolean }): string { + return applyRanges(parseCompressInput(args), ctx, opts).text; +} + +// --------------------------------------------------------------------------- +// Config plumbing: parse + three-level merge. +// --------------------------------------------------------------------------- + +test("#5683: parseCompressSettings accepts the two boolean gates", () => { + const parsed = parseCompressSettings({ requireNudge: true, requireRecommendation: false }); + assert.ok(parsed); + assert.equal(parsed.requireNudge, true); + assert.equal(parsed.requireRecommendation, false); +}); + +test("#5683: parseCompressSettings rejects non-boolean gate values (whole block)", () => { + assert.equal(parseCompressSettings({ requireNudge: "yes" }), undefined); + assert.equal(parseCompressSettings({ requireRecommendation: 1 }), undefined); +}); + +test("#5683: mergeCompress — deepest defined level wins per field (explicit false disables)", () => { + assert.equal(mergeCompress({ requireNudge: true }).requireNudge, true); + assert.equal(mergeCompress({ requireNudge: true }, { requireNudge: false }).requireNudge, false); + assert.equal(mergeCompress({ requireNudge: false }, undefined, { requireNudge: true }).requireNudge, true); + assert.equal(mergeCompress({ requireRecommendation: true }, { requireRecommendation: true }, {}).requireRecommendation, true); + assert.equal(mergeCompress().requireNudge, undefined); +}); + +// --------------------------------------------------------------------------- +// requireNudge — timing gate (nudge actually delivered this turn?). +// --------------------------------------------------------------------------- + +test("#5683: default (nothing stamped) — unprompted compress SUCCEEDS (behavior unchanged)", () => { + const ctx = freshCtx(); + const out = runApply(ctx, { content: [{ startId: "m00001", endId: "m00004", summary: SUM }] }); + assert.ok(out.startsWith("[Compressed"), `expected success, got: ${out.slice(0, 160)}`); +}); + +test("#5683: requireNudge ON + unarmed round — refused with policy receipt, nothing folded", () => { + const ctx = freshCtx(); + storeUnpromptedGate(ctx.session, { armed: false, requireNudge: true, requireRecommendation: false }); + const out = runApply(ctx, { content: [{ startId: "m00001", endId: "m00004", summary: SUM }] }); + assert.ok(out.startsWith("[Compression REFUSED"), `expected refusal, got: ${out.slice(0, 160)}`); + assert.ok(out.includes("requireNudge"), "receipt names the knob:\n" + out); + assert.equal(ctx.session.state.blocks.length, 0, "no block may be created"); + assert.ok(ctx.logs.some((l) => l.includes("refused unprompted compress")), "refusal logged"); +}); + +test("#5683: requireNudge ON + armed round (nudge injected) — allowed", () => { + const ctx = freshCtx(); + storeUnpromptedGate(ctx.session, { armed: true, requireNudge: true, requireRecommendation: false }); + const out = runApply(ctx, { content: [{ startId: "m00001", endId: "m00004", summary: SUM }] }); + assert.ok(out.startsWith("[Compressed"), `expected success, got: ${out.slice(0, 160)}`); +}); + +test("#5683: requireNudge ON + unarmed + preflight lane (loopTracking:false) — exempt, succeeds", () => { + const ctx = freshCtx(); + storeUnpromptedGate(ctx.session, { armed: false, requireNudge: true, requireRecommendation: false }); + const out = runApply(ctx, { content: [{ startId: "m00001", endId: "m00004", summary: SUM }] }, { loopTracking: false }); + assert.ok(out.startsWith("[Compressed"), `preflight self-save must stay available, got: ${out.slice(0, 160)}`); +}); + +test("#5683: both gates ON + unarmed — the timing refusal wins (checked first)", () => { + const ctx = freshCtx(); + setOffer(ctx, [offerRange("m00001", "m00004", 4)]); + storeUnpromptedGate(ctx.session, { armed: false, requireNudge: true, requireRecommendation: true }); + const out = runApply(ctx, { content: [{ startId: "m00001", endId: "m00004", summary: SUM }] }); + assert.ok(out.includes("requireNudge"), `timing gate reports first, got: ${out.slice(0, 160)}`); + assert.ok(!out.includes("requireRecommendation"), "scope gate must not double-report"); +}); + +// --------------------------------------------------------------------------- +// requireRecommendation — scope gate (stay within the recommended set). +// --------------------------------------------------------------------------- + +test("#5683: requireRecommendation ON + range inside the prepare-time offer — allowed", () => { + const ctx = freshCtx(); + setOffer(ctx, [offerRange("m00001", "m00004", 4)]); + storeUnpromptedGate(ctx.session, { armed: false, requireNudge: false, requireRecommendation: true }); + const out = runApply(ctx, { content: [{ startId: "m00001", endId: "m00004", summary: SUM }] }); + assert.ok(out.startsWith("[Compressed"), `expected success, got: ${out.slice(0, 160)}`); +}); + +test("#5683: requireRecommendation ON + SUBDIVIDING a recommended span — allowed (coverage, not equality)", () => { + const ctx = freshCtx(); + setOffer(ctx, [offerRange("m00001", "m00004", 4)]); + storeUnpromptedGate(ctx.session, { armed: false, requireNudge: false, requireRecommendation: true }); + const out = runApply(ctx, { content: [{ startId: "m00002", endId: "m00003", summary: SUM }] }); + assert.ok(out.startsWith("[Compressed"), `subdivided span must pass, got: ${out.slice(0, 160)}`); +}); + +test("#5683: requireRecommendation ON + live range OUTSIDE the offer — refused naming the span, nothing folded", () => { + const ctx = freshCtx(); + setOffer(ctx, [offerRange("m00001", "m00004", 4)]); + storeUnpromptedGate(ctx.session, { armed: false, requireNudge: false, requireRecommendation: true }); + // m5-m7 are live-compressible per the kernel but were NOT offered this round. + const out = runApply(ctx, { content: [{ startId: "m00005", endId: "m00007", summary: SUM }] }); + assert.ok(out.startsWith("[Compression REFUSED"), `expected refusal, got: ${out.slice(0, 160)}`); + assert.ok(out.includes("requireRecommendation"), "receipt names the knob:\n" + out); + assert.ok(out.includes("m00005–m00007"), "offending span named:\n" + out); + assert.equal(ctx.session.state.blocks.length, 0, "no block may be created"); +}); + +test("#5683: requireRecommendation ON + mixed range (part offered, part not) — refused as a whole", () => { + const ctx = freshCtx(); + setOffer(ctx, [offerRange("m00001", "m00004", 4)]); + storeUnpromptedGate(ctx.session, { armed: false, requireNudge: false, requireRecommendation: true }); + const out = runApply(ctx, { content: [{ startId: "m00003", endId: "m00006", summary: SUM }] }); + assert.ok(out.startsWith("[Compression REFUSED"), `mixed span must be refused whole, got: ${out.slice(0, 160)}`); + assert.ok(out.includes("m00003–m00006")); + assert.equal(ctx.session.state.blocks.length, 0, "atomic: nothing folded"); +}); + +test("#5683: requireRecommendation ON + offered span BELOW the minCompressRange floor — out of bounds", () => { + // The advertised surfaces (nudge submitGate, acp_status) drop sub-floor + // ranges, so the gate must agree: raising the floor above the offered + // span's char count (4 × 8000 tokens × 4 = 32000) removes it from the + // recommendation set even though it sits in lastNudge verbatim. + const ctx = freshCtx(); + setOffer(ctx, [offerRange("m00001", "m00004", 4)]); + ctx.config.compress.minCompressRange = 40000; + storeUnpromptedGate(ctx.session, { armed: false, requireNudge: false, requireRecommendation: true }); + const out = runApply(ctx, { content: [{ startId: "m00001", endId: "m00004", summary: SUM }] }); + assert.ok(out.startsWith("[Compression REFUSED"), `sub-floor span must be out of bounds, got: ${out.slice(0, 160)}`); + assert.ok(out.includes("out-of-bounds compression is disabled"), "must be OUR receipt, not a kernel one:\n" + out); + assert.equal(ctx.session.state.blocks.length, 0, "nothing folded"); +}); + +test("#5683: requireRecommendation ON + pure refold of an active block — NOT refused by the scope gate", () => { + // Fold m1-m4 first (gates off), then re-point the offer away and re-submit + // the SAME span: the covered messages never sit in fresh recommendation + // lists, so the carve-out must let the call through to the kernel's own + // refold handling (whatever verdict THAT gives, it is not our receipt). + const ctx = freshCtx(); + const first = runApply(ctx, { content: [{ startId: "m00001", endId: "m00004", summary: SUM }] }); + assert.ok(first.startsWith("[Compressed"), `setup fold failed: ${first.slice(0, 160)}`); + assert.ok(ctx.session.state.blocks.length > 0, "block exists after setup fold"); + setOffer(ctx, [offerRange("m00005", "m00007", 3)]); + storeUnpromptedGate(ctx.session, { armed: false, requireNudge: false, requireRecommendation: true }); + const out = runApply(ctx, { content: [{ startId: "m00001", endId: "m00004", summary: SUM + "-REFOLD-UPDATE" }] }); + assert.ok(!out.includes("out-of-bounds compression is disabled"), "scope gate must not fire on a refold:\n" + out); + assert.ok(!ctx.logs.some((l) => l.includes("refused out-of-recommendation")), "no scope-gate refusal logged"); +}); + +test("#5683: requireRecommendation ON + no offer and EMPTY live plan (every candidate dead) — strict: refused", () => { + // Without a prepare snapshot the scope falls back to the live recompute; + // killing every candidate makes that set empty ⇒ nothing is recommended + // ⇒ every span is out of bounds. (The non-empty fallback case is pinned by + // the plugin-lane positive test below.) + const ctx = freshCtx(); + ctx.session.state.deadRefs = ["m00001", "m00002", "m00003", "m00004", "m00005", "m00006", "m00007"]; + storeUnpromptedGate(ctx.session, { armed: false, requireNudge: false, requireRecommendation: true }); + const out = runApply(ctx, { content: [{ startId: "m00001", endId: "m00004", summary: SUM }] }); + assert.ok(out.startsWith("[Compression REFUSED"), `nothing recommended ⇒ nothing foldable, got: ${out.slice(0, 160)}`); + assert.ok(out.includes("requireRecommendation")); + assert.equal(ctx.session.state.blocks.length, 0, "nothing folded"); +}); + +// --------------------------------------------------------------------------- +// Plugin-lane shape: ctx carries NO prepare snapshot (lastNudge undefined) — +// the recommendation set falls back to the LIVE recompute (same source acp_status shows). +// --------------------------------------------------------------------------- + +test("#5683: plugin-lane ctx (no lastNudge) — a live-recommended span passes the scope gate", () => { + const ctx = freshCtx(); + const live = liveCompressionRanges(ctx); + assert.ok(live && live.ranges.length > 0, "fixture must yield a live recommendation set"); + const r = live.ranges[0]; + storeUnpromptedGate(ctx.session, { armed: false, requireNudge: false, requireRecommendation: true }); + const out = runApply(ctx, { content: [{ startId: r.startRef, endId: r.endRef, summary: SUM }] }); + assert.ok(!out.includes("out-of-bounds compression is disabled"), "live-recommended span must pass:\n" + out); +}); + +test("#5683: plugin-lane ctx (no lastNudge) — a span containing a DEAD ref is out of bounds", () => { + const ctx = freshCtx(); + // Client dropped m00006: every advertised span containing it is excluded, + // so submitting across it is definitively out-of-bounds (our receipt wins + // over any kernel-side rejection because the gate runs before apply). + ctx.session.state.deadRefs = ["m00006"]; + storeUnpromptedGate(ctx.session, { armed: false, requireNudge: false, requireRecommendation: true }); + const out = runApply(ctx, { content: [{ startId: "m00005", endId: "m00007", summary: SUM }] }); + assert.ok(out.startsWith("[Compression REFUSED"), `expected refusal, got: ${out.slice(0, 160)}`); + assert.ok(out.includes("out-of-bounds compression is disabled"), "must be OUR receipt, not a kernel one:\n" + out); + assert.equal(ctx.session.state.blocks.length, 0, "nothing folded"); +}); diff --git a/tests/wire-exit-matrix.test.ts b/tests/wire-exit-matrix.test.ts index 6611f3156b..71d66dc2d4 100644 --- a/tests/wire-exit-matrix.test.ts +++ b/tests/wire-exit-matrix.test.ts @@ -35,14 +35,53 @@ const SLOW_MS = 1500; // Raised to 10s after 6s still flaked three consecutive runs on windows-latest // node22 shard 2 (master c78ecb4b + ci-fixes c49bf8b1/12b988ff, all closed=0, // same cell, sibling shards green — load latency, not a teardown regression; -// recorded under #4599). If it flakes again at 10s, investigate a real teardown -// hang instead of raising further. -const ABORT_TEARDOWN_BUDGET_MS = 10000; +// recorded under #4599). Flaked once more at 10s (master 9dc4c945, run +// 41686972484, 2026-11-03: openai-sse + anthropic-sse closed=0 after the full +// poll, ~11s each; same code green on the ubuntu22/24 and windows24 shard-2 +// siblings, pass durations 23–352ms). The mandated "investigate a real +// teardown hang" audit found none: relay.ts arms res.on("close") -> +// clientAbort.abort() synchronously with no linger/backstop on this path, and +// no commit since d244ffff touched destroy/abort wiring — the residual latency +// is runner-side event-loop starvation on loaded Windows runners. Hence a +// platform-scoped budget instead of another flat raise: win32 doubles to 20s +// (every recorded starvation instance was windows-latest — node22+24, multiple +// shards, #3665/#4347 era — ubuntu never showed the signature), keeping the +// tight 10s gate everywhere else so a genuine teardown hang still fails fast. +// Flaked once more at 20s (ci-fixes c37293b6, run 41776902208, 2026-11-04: +// openai-sse closed=0 after the full poll, duration 20518ms; anthropic/ +// responses/json siblings 131–410ms in the same run; master main CI green +// 40min earlier at 23a826d3 — same load-starvation draw, not a regression). +// That closes out the flat-raise ladder: the poller and the upstream mock +// share ONE event loop with whatever is starving the runner, so a close event +// cannot be delivered faster than the loop itself — an absolute wall-clock +// budget measures the runner, not the teardown. The budget is therefore +// extended by the poll's OWN measured event-loop lag (each tick that lands +// late adds its lateness), capped at 2×base so a genuine hang still fails: on +// a healthy loop the gate stays byte-for-byte the flat budget (zero +// extension); under starvation the window grows with the measured lag up to +// 2×base (win32: 20s base ⇒ ≤40s — the same ceiling a flat 40s raise would +// give unconditionally, applied only when lag is actually observed). Same +// relative-invariant discipline as the #3228/#3152-C error-delivery cells. +const ABORT_TEARDOWN_BUDGET_MS = process.platform === "win32" ? 20000 : 10000; +const TEARDOWN_POLL_MS = 100; + +/** May the teardown poll keep going? base + measured event-loop lag, capped + * at 2×base — see the budget comment above for the starvation mechanics. */ +function teardownPollContinues(elapsedMs: number, baseMs: number, lagMs: number): boolean { + return elapsedMs < baseMs + Math.min(lagMs, baseMs); +} async function awaitUpstreamTeardown(socketsClosed: () => number): Promise { - const deadline = Date.now() + ABORT_TEARDOWN_BUDGET_MS; - while (socketsClosed() < 1 && Date.now() < deadline) { - await new Promise((r) => setTimeout(r, 100)); + const base = ABORT_TEARDOWN_BUDGET_MS; + const t0 = Date.now(); + let lagMs = 0; + let last = t0; + while (socketsClosed() < 1 && teardownPollContinues(Date.now() - t0, base, lagMs)) { + await new Promise((r) => setTimeout(r, TEARDOWN_POLL_MS)); + const now = Date.now(); + const lateBy = now - last - TEARDOWN_POLL_MS; + if (lateBy > 0) lagMs += lateBy; + last = now; } } @@ -393,3 +432,13 @@ test("error-delivery x proxy-openai-sse: preflight failure after the early commi await once(up.server, "close"); } }); + +test("abort x teardown-budget arithmetic: measured lag extends the window, capped at 2×base", () => { + const base = 10_000; + assert.equal(teardownPollContinues(9_999, base, 0), true, "healthy loop: inside the flat budget"); + assert.equal(teardownPollContinues(10_000, base, 0), false, "healthy loop: boundary unchanged (zero lag ⇒ old flat gate byte-for-byte)"); + assert.equal(teardownPollContinues(12_000, base, 5_000), true, "measured event-loop lag extends the window"); + assert.equal(teardownPollContinues(15_000, base, 5_000), false, "extension equals the measured lag, no more"); + assert.equal(teardownPollContinues(19_999, base, 999_999), true, "runaway lag cannot push the window past 2×base"); + assert.equal(teardownPollContinues(20_000, base, 999_999), false, "hard ceiling at 2×base guarantees termination"); +}); diff --git a/website/config-reference/compress.yaml b/website/config-reference/compress.yaml index a934738c79..9732ea8d03 100644 --- a/website/config-reference/compress.yaml +++ b/website/config-reference/compress.yaml @@ -92,6 +92,26 @@ entries: desc_zh: "nudgeModelDecided 所用模型决策 side call 的输出预算(token)。必须 > 0。" site_en: "Output budget (tokens) of the model-decision side call (default 200). Must be > 0." site_zh: "模型决策 side call 的输出预算(token,默认 200)。必须 > 0。" + - key: compress.requireNudge + type: boolean + default: "off (unset)" + group: compress + id: cw.rnug + render: compress-row + desc_en: "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." + desc_zh: "禁止未提示的模型主动压缩(#5683):只有当前轮次压缩提醒真正送达模型时(advisory、模型自决指令、紧急提醒都算;\"gated\"/\"vetoed\"/\"skipped\" 不算)才允许 compress,否则拒绝并回错误回执;同一轮内的多次 loop 请求保持武装,下一个未提示轮次自动解除。bili 自己的溢出兜底折叠不受影响。针对会自行乱压的模型(如 qwen 27b),可按 provider/model 单独开启。默认关闭。" + site_en: "Forbid unprompted compression (off by default): a model compress call is refused unless a nudge actually reached the model this turn; the refusal returns an error receipt instead of folding." + site_zh: "禁止未提示压缩(默认关闭):只有本轮压缩提醒真正送达模型时才允许模型调用 compress,否则拒绝并回错误回执。" + - key: compress.requireRecommendation + type: boolean + default: "off (unset)" + group: compress + id: cw.rrec + render: compress-row + desc_en: "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." + desc_zh: "禁止越界的模型主动压缩(#5683):提交的每个范围必须落在 bili 当前推荐折叠的集合内——即 nudge 文案与 acp_status 展示的同一份实时可压缩列表(有本轮 prepare 快照用快照,否则实时重算)。按「覆盖」判定而非精确相等:把推荐跨度拆小段通过;已折进活跃块的消息豁免(重折已有块更新摘要合法)。范围里含任何其他仍存活的消息则整体拒绝,回执点名越界跨度并指向 acp_status。默认关闭。" + site_en: "Forbid out-of-bounds compression (off by default): only ranges bili currently recommends may be folded; subdividing a listed span or refolding an existing block stays legal." + site_zh: "禁止越界压缩(默认关闭):只允许折叠 bili 当前推荐的范围;拆分推荐跨度、重折已有块更新摘要仍合法。" - key: compress.streamSummary type: boolean default: "false (unset)" diff --git a/website/docs/index.html b/website/docs/index.html index 31f6fe80df..b9f6a8db9f 100644 --- a/website/docs/index.html +++ b/website/docs/index.html @@ -188,6 +188,8 @@

compress — every knob

minPressureBenefitTokensnumber (≥ 0)Minimum reclaimable tokens for a pressure-band nudge to fire (default max(5000, limit × 1%)); explicit 0 restores legacy any-pending behavior (#3211). nudgeModelDecidedbooleanModel-decided nudge timing (off by default): an armed tier-1 nudge first asks the model, over the session's cached prefix, whether compressing now helps the current task; a strict-JSON yes injects a directive with a program-finalized span, anything else injects nothing this round. nudgeDecisionMaxTokensnumberOutput budget (tokens) of the model-decision side call (default 200). Must be > 0. + requireNudgebooleanForbid unprompted compression (off by default): a model compress call is refused unless a nudge actually reached the model this turn; the refusal returns an error receipt instead of folding. + requireRecommendationbooleanForbid out-of-bounds compression (off by default): only ranges bili currently recommends may be folded; subdividing a listed span or refolding an existing block stays legal. streamSummarybooleanForce streaming (SSE) summarization from the first attempt — stream-mandating upstreams now self-heal via a one-shot SSE probe on any non-streaming 400 (#2494); kept as the escape hatch for gateways that time out long non-streaming calls (e.g. Cloudflare 524), which the self-learn cannot detect, or to skip the one probe round-trip. preserveRecentMessagesnumberKernel default 3 (tuned 5 → 3, #3058): the most recent messages stay soft-protected from folds. preserveRecentTokensnumberKernel default ≈5000: the most recent tokens stay soft-protected from folds. diff --git a/website/main.js b/website/main.js index 3c71292de7..8aae3fceb8 100644 --- a/website/main.js +++ b/website/main.js @@ -70,6 +70,8 @@ "cw.mpbt": "Minimum reclaimable tokens for a pressure-band nudge to fire (default max(5000, limit × 1%)); explicit 0 restores legacy any-pending behavior (#3211).", "cw.nmd": "Model-decided nudge timing (off by default): an armed tier-1 nudge first asks the model, over the session's cached prefix, whether compressing now helps the current task; a strict-JSON yes injects a directive with a program-finalized span, anything else injects nothing this round.", "cw.ndmt": "Output budget (tokens) of the model-decision side call (default 200). Must be > 0.", + "cw.rnug": "Forbid unprompted compression (off by default): a model compress call is refused unless a nudge actually reached the model this turn; the refusal returns an error receipt instead of folding.", + "cw.rrec": "Forbid out-of-bounds compression (off by default): only ranges bili currently recommends may be folded; subdividing a listed span or refolding an existing block stays legal.", "cw.ssm": "Force streaming (SSE) summarization from the first attempt — stream-mandating upstreams now self-heal via a one-shot SSE probe on any non-streaming 400 (#2494); kept as the escape hatch for gateways that time out long non-streaming calls (e.g. Cloudflare 524), which the self-learn cannot detect, or to skip the one probe round-trip.", "cw.prm": "Kernel default 3 (tuned 5 → 3, #3058): the most recent messages stay soft-protected from folds.", "cw.prt": "Kernel default ≈5000: the most recent tokens stay soft-protected from folds.", @@ -321,6 +323,8 @@ "cw.mpbt": "压力带 nudge 触发所需的最小可回收 token 数(默认 max(5000, 上限×1%));显式 0 恢复老的任意 pending 行为(#3211)。", "cw.nmd": "模型自决的压缩时机(默认关闭):tier-1 提醒触发时,先通过会话缓存前缀上的短 side call 问模型现在压缩是否划算;严格 JSON 的 yes 注入带程序最终确定范围的指令,其余情况本轮不注入。", "cw.ndmt": "模型决策 side call 的输出预算(token,默认 200)。必须 > 0。", + "cw.rnug": "禁止未提示压缩(默认关闭):只有本轮压缩提醒真正送达模型时才允许模型调用 compress,否则拒绝并回错误回执。", + "cw.rrec": "禁止越界压缩(默认关闭):只允许折叠 bili 当前推荐的范围;拆分推荐跨度、重折已有块更新摘要仍合法。", "cw.ssm": "强制摘要从首次起走流式(SSE)——要求流式的上游现在通过任何非流式 400 上的一次性 SSE 探测自愈(#2494);保留作为会掐断长非流式调用的网关(如 Cloudflare 524)的逃生通道(自学习无法感知该场景),也可用于省去一次探测往返。", "cw.prm": "内核默认 3(5 → 3,#3058):最近消息软保护、免于折叠。", "cw.prt": "内核默认约 5000:最近的 token 软保护、免于折叠。",