Skip to content
Closed
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
16 changes: 16 additions & 0 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ This index is generated from `website/config-reference/*.yaml` — edit the seed
| `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. |
| `compress.minCompressRangeChars` | number (deprecated alias: minCompressRange) | kernel ≈5000 | — | Smallest foldable range, in characters; shorter ranges never fold. |
| `compress.maxSummaryLength` | number | kernel 20000 | — | Hard character cap on ONE generated summary; longer replies are rejected whole (no truncation, no retry). External summarizers are told this exact cap up front (#3635). 0 = unbounded. |
| `compress.minSummaryLength` | number | kernel 50 | — | Character floor for generated summaries; shorter replies are rejected whole. 0 = disabled. External summarizers are held to the same floor (#3635). |
| `compress.reconcile` | "off" \| "warn" \| "repair" | "repair" | BILI_FOLD_RECONCILE | Reconcile folded state when the client rewinds or rewrites history between turns. |
| `compress.promptPack` | string (builtin: "default", "lean") | builtin "default" | — | Compression prompt pack, resolved project pack → user pack → builtin; not gated by acknowledgePromptsRisk. |
| `compress.tiers` | boolean | true | — | Tiered T1→T3 distillation spreads folding cost across generations. |
Expand Down Expand Up @@ -1375,6 +1377,20 @@ For each request, the proxy resolves the settings by longest-URL-prefix match (t
- **Status:** DEPRECATED (alias of `minCompressRangeChars`, kept for backward compatibility)
- **Description:** Legacy name for `minCompressRangeChars` — same kernel mapping (`compress.minCompressRange`), same unit (characters). When both keys are set at the same level the canonical name wins; across levels the deeper level wins regardless of which name it uses.

#### `maxSummaryLength`

- **Type:** `number`
- **Default:** *(kernel default `20000`)*
- **Status:** ACTIVE (#3635)
- **Description:** Hard cap, in **characters**, on ONE generated summary. Longer replies are rejected whole — by the kernel's all-or-nothing length check for in-model summaries and by the external-summary executor for configured chains (no truncation, no retry). Both preflight and model-tool compression paths tell external summarizers this exact cap up front (preflight since #1775, the compress path since #3635), so overshoot is a model-compliance failure with a named log reason instead of an opaque discard. `0` disables the cap. Maps to the kernel field `compress.maxSummaryLength`. Before #3635 the key was accepted nowhere: the panel PUT silently dropped it and file edits never reached the resolved config, so the effective value was always the kernel default.

#### `minSummaryLength`

- **Type:** `number`
- **Default:** *(kernel default `50`)*
- **Status:** ACTIVE (#3635)
- **Description:** Floor, in **characters**, for generated summaries; shorter replies are rejected whole. `0` disables the floor. Maps to the kernel field `compress.minSummaryLength`. Same acceptance history as `maxSummaryLength` (#3635).

#### `tiers`

- **Type:** `boolean`
Expand Down
16 changes: 16 additions & 0 deletions CONFIGURATION.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@
| `compress.preserveRecentMessages` | number | 3 | — | 最近的消息软保护、免于折叠。 |
| `compress.preserveRecentTokens` | number | kernel ≈5000 | — | 最近的 token 软保护、免于折叠。 |
| `compress.minCompressRangeChars` | number (deprecated alias: minCompressRange) | kernel ≈5000 | — | 可折叠片段的最小字符数;更短的永不折叠。 |
| `compress.maxSummaryLength` | number | kernel 20000 | — | 单条生成摘要的硬字符上限;超长的回复被整段拒绝(不截断、不重试)。外部摘要模型会被提前告知这个确切上限(#3635)。0 = 不限制。 |
| `compress.minSummaryLength` | number | kernel 50 | — | 生成摘要的字符下限;过短的回复被整段拒绝。0 = 禁用。外部摘要模型受同一下限约束(#3635)。 |
| `compress.reconcile` | "off" \| "warn" \| "repair" | "repair" | BILI_FOLD_RECONCILE | 客户端在轮次之间回退或改写历史时校准已折叠状态。 |
| `compress.promptPack` | string (builtin: "default", "lean") | builtin "default" | — | 压缩提示词包,按 项目包 → 用户包 → 内置 解析;不受 acknowledgePromptsRisk 门控。 |
| `compress.tiers` | boolean | true | — | T1→T3 分级蒸馏把折叠成本摊到多代。 |
Expand Down Expand Up @@ -1378,6 +1380,20 @@ ACP 原生 agent(当前为 `pi` 扩展)会在每个进程内向代理上报
- **状态:** DEPRECATED(`minCompressRangeChars` 的弃用别名,向后兼容保留)
- **说明:** `minCompressRangeChars` 的旧名,内核映射(`compress.minCompressRange`)与单位(字符)完全相同。同层两键并存时新名优先;跨层时更深层优先,与键名无关。

#### `maxSummaryLength`

- **类型:** `number`
- **默认值:** *(内核默认值 `20000`)*
- **状态:** ACTIVE(#3635)
- **说明:** 单条生成摘要的硬上限,单位为**字符**。超长的回复被整段拒绝——模型自产摘要由内核的全有或全无长度检查拒绝,配置的摘要链由外部摘要执行器拒绝(不截断、不重试)。preflight 与模型工具两条压缩路径都会把这个确切上限提前告知外部摘要模型(preflight 自 #1775 起,compress 路径自 #3635 起),因此超长从「无声丢弃」变成有明确日志原因的模型合规问题。`0` 禁用上限。映射到内核字段 `compress.maxSummaryLength`。#3635 之前该键在任何层面都不被接受:面板 PUT 会静默丢弃它,手改配置文件也到不了生效配置,实际生效值永远是内核默认值。

#### `minSummaryLength`

- **类型:** `number`
- **默认值:** *(内核默认值 `50`)*
- **状态:** ACTIVE(#3635)
- **说明:** 生成摘要的下限,单位为**字符**;过短的回复被整段拒绝。`0` 禁下限。映射到内核字段 `compress.minSummaryLength`。与 `maxSummaryLength` 相同的接受历史(#3635)。

#### `tiers`

- **类型:** `boolean`
Expand Down
6 changes: 6 additions & 0 deletions src/compress-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ export function mergeCompress(
preserveRecentMessages: pick("preserveRecentMessages"),
preserveRecentTokens: pick("preserveRecentTokens"),
minCompressRangeChars: rangeOf(model) ?? rangeOf(provider) ?? rangeOf(global),
maxSummaryLength: pick("maxSummaryLength"),
minSummaryLength: pick("minSummaryLength"),
tiers: pick("tiers"),
protectedLatestTools: pick("protectedLatestTools"),
protectedTools: pick("protectedTools"),
Expand Down Expand Up @@ -337,6 +339,10 @@ export function applyCompressSettings(base: Config, limit: number, s: CompressSe
compress: {
...base.compress,
minCompressRange: s.minCompressRangeChars ?? s.minCompressRange ?? base.compress.minCompressRange,
// #3635: previously unmapped, so a configured cap never reached the
// resolved kernel config (panel PUT and file edits were both inert).
maxSummaryLength: s.maxSummaryLength ?? base.compress.maxSummaryLength,
minSummaryLength: s.minSummaryLength ?? base.compress.minSummaryLength,
},
protectedLatestTools: s.protectedLatestTools ?? base.protectedLatestTools,
protectedTools: s.protectedTools ?? base.protectedTools,
Expand Down
20 changes: 20 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,15 @@ export type CompressSettings = {
* wins; across levels the deeper level wins regardless of which name it
* uses. */
minCompressRange?: number;
/** Hard character cap on ONE generated summary; longer replies are
* rejected whole by the kernel and the external-summary executor. Maps to
* kernel `compress.maxSummaryLength` (default 20000; 0 = unbounded).
* #3635: external summarizers are told this exact cap up front. */
maxSummaryLength?: number;
/** Character floor for generated summaries; shorter replies are rejected
* whole. Maps to kernel `compress.minSummaryLength` (default 50;
* 0 = disabled). #3635. */
minSummaryLength?: number;
/** Enable multi-tier (T2/T3) distillation (kernel `tiers.enabled`). */
tiers?: boolean;
/** Tool-name patterns whose LATEST tool-call + paired result are never
Expand Down Expand Up @@ -2185,6 +2194,8 @@ const COMPRESS_SETTING_FIELDS = new Set([
"acknowledgePromptsRisk", "absorb", "ccr", "search",
"prompts", "promptPack", "reasoningGuard", "outputSteering", "priceProfile",
"reconcile", "streamSummary",
// #3635: summary-length caps — previously accepted nowhere, silently dropped.
"maxSummaryLength", "minSummaryLength",
]);

// Deduped per unique key set per process (same pattern as
Expand Down Expand Up @@ -2848,6 +2859,15 @@ export function parseCompressSettings(v: unknown): (CompressSettings & { injectT
for (const key of ["nudgeGrowthTokens", "preserveRecentMessages", "preserveRecentTokens", "minCompressRange", "minCompressRangeChars"] as const) {
takeNumber(key);
}
// #3635: summary-length caps map onto kernel compress.{max,min}SummaryLength.
// 0 is meaningful (unbounded cap / disabled floor), negative is not — so
// validate stricter than takeNumber instead of reusing it.
for (const key of ["maxSummaryLength", "minSummaryLength"] as const) {
if (!(key in obj)) continue;
const v = obj[key];
if (typeof v !== "number" || !Number.isFinite(v) || v < 0) ok = false;
else (out as Record<string, unknown>)[key] = v;
}
// #2228: the decision side-call budget must be positive — 0/negative would
// starve the call, so validate stricter than takeNumber.
if ("nudgeDecisionMaxTokens" in obj) {
Expand Down
18 changes: 12 additions & 6 deletions src/external-summary-compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { ResolvedKernelConfig } from "./compress-settings.js";
import { applyRanges, noVisibleContextRefusal, normalizeRangeOrder, recoverEvictedView, type RewriteCtx } from "./stream.js";
import { compressResult, type ProxyToolResult } from "./proxy-tool-result.js";
import { imagePlaceholders } from "./image-note.js";
import type { SummaryWork } from "./external-summary.js";
import { describeExternalSummaryFailure, summaryLengthBudgetClause, type SummaryWork } from "./external-summary.js";
import { newExternalCallId } from "./external-summary-marker.js";

// Re-exported for callers that historically imported it here; the canonical
Expand Down Expand Up @@ -86,9 +86,13 @@ export async function applyConfiguredCompression(input: unknown, ctx: RewriteCtx
.map(({ topic, summary }) => ({ topic, summary })),
});
valid.push(range);
work.push({ content, reference, minSummaryChars: ctx.config.compress.minSummaryLength,
maxSummaryChars: Math.min(range.summaryMaxChars ?? Infinity, ctx.config.compress.maxSummaryLength),
instructions: `${ctx.session.meta.summaryInstructions ?? buildCompressSystemPrompt()}\nWrite a faithful summary of the selected range ${range.startRef}-${range.endRef}. Preserve task status, constraints, decisions, exact code identifiers, paths and errors, and unfinished objectives with their source message refs. Submitted summary hints are non-authoritative. Never summarize or change the read-only reference.` });
// #3635: state the EXACT cap the validator enforces in the instructions
// — the preflight path has told its summarizers this number since #1775;
// without it the compress path asked for output it could never accept.
const maxSummaryChars = Math.min(range.summaryMaxChars ?? Infinity, ctx.config.compress.maxSummaryLength);
work.push({ content, reference, minSummaryChars: ctx.config.compress.minSummaryLength, maxSummaryChars,
instructions: `${ctx.session.meta.summaryInstructions ?? buildCompressSystemPrompt()}\nWrite a faithful summary of the selected range ${range.startRef}-${range.endRef}. Preserve task status, constraints, decisions, exact code identifiers, paths and errors, and unfinished objectives with their source message refs. Submitted summary hints are non-authoritative. Never summarize or change the read-only reference.`
+ summaryLengthBudgetClause(maxSummaryChars, ctx.config.compress.minSummaryLength) });
}
if (work.length === 0) return compressResult("[Compression FAILED: no selected range passed the existing kernel protection checks. Nothing compressed.]", "refused", 0);
const batch = await plan.summarize(work, signal);
Expand All @@ -97,8 +101,10 @@ export async function applyConfiguredCompression(input: unknown, ctx: RewriteCtx
for (const [index, result] of batch.results.entries()) {
if (result.status === "success") successful.push({ ...valid[index], summary: result.summary, compressCallId: newExternalCallId() });
}
ctx.log(`[external-summary] completed ${successful.length}/${parsed.ranges.length} range(s); batch=${batch.status}`);
if (successful.length === 0) return compressResult("[Compression FAILED: all configured external summary candidates failed or exceeded their budget. Nothing compressed; do not retry unchanged configuration.]", "refused", 0);
// #3635: full failure description (parity with the preflight log line) so
// an over-cap rejection is visible with its length reason, not bare "batch=finished".
ctx.log(`[external-summary] completed ${successful.length}/${parsed.ranges.length} range(s); ${describeExternalSummaryFailure(batch)}`);
if (successful.length === 0) return compressResult("[Compression FAILED: all configured external summary candidates failed or exceeded their budget. Nothing compressed; do not retry unchanged configuration — summaries longer than the configured cap are rejected whole, so raise compress.maxSummaryLength or request less via per-range summaryMaxChars.]", "refused", 0);
const applied = applyRanges({ ...parsed, ranges: successful }, ctx);
// Client call arguments carry only an optional hint, never the generated summary.
// In-place refolds retain their old call id in the kernel; keep their anchor too.
Expand Down
47 changes: 46 additions & 1 deletion src/external-summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ interface SummaryPoolOptions {
interface SummaryAttempt {
readonly targetIndex: number;
readonly outcome: "success" | "error" | "timeout" | "invalid_summary" | "cancelled";
/** #3635: WHY an invalid_summary reply was rejected wholesale — before, a
* discarded over-cap summary left only this bare outcome as a trace. */
readonly detail?: string;
}

type ExternalSummaryResult =
Expand All @@ -50,6 +53,48 @@ function positiveInteger(value: number): boolean {
return Number.isSafeInteger(value) && value > 0;
}

/** #3635: name WHY a candidate reply failed the length/shape gate. Mirrors the
* validator's check order below so the stated reason always matches the cause
* that rejected the reply. Pure numbers + fixed words — never echoes payload. */
function invalidSummaryDetail(value: unknown, limits: SummaryBudget, request: Readonly<SummaryWork>): string {
if (typeof value !== "string" || !value.trim()) return "empty or non-string response";
const bytes = Buffer.byteLength(value, "utf8");
if (bytes > limits.maxSummaryBytes) return `response ${bytes} bytes exceeds byte cap ${limits.maxSummaryBytes}`;
const chars = value.trim().length;
const floor = request.minSummaryChars ?? 1;
if (chars < floor) return `summary ${chars} chars below floor ${floor}`;
return `summary ${chars} chars exceeds cap ${request.maxSummaryChars}`;
}

/** #1775/#3635: the length-budget clause every external-summarizer prompt
* carries — rendered up front with the EXACT cap the validator enforces, so
* the first attempt fits instead of paying full latency for a wholesale-
* discarded overshoot. Omitted below the minimum-summary floor (the kernel
* minimum-summary gate rejects such output anyway, so the instruction would
* be counterproductive) and when the cap is unbounded (<= 0 / Infinity). */
export function summaryLengthBudgetClause(budget: number | undefined, minChars: number): string {
if (budget === undefined || !Number.isFinite(budget) || budget <= 0 || budget < minChars) return "";
return `\n\nLENGTH BUDGET: Your ENTIRE response must be AT MOST ${budget} characters total — longer output is rejected by the pipeline. Be dense: compact bullets, no filler or repetition.`;
}

/** Describe why an external-summary batch yielded no usable summary (#2484):
* the batch stop reason plus each candidate attempt's outcome, so an operator
* can tell timeout / error / cancelled / invalid-plan apart instead of seeing
* one opaque "failed or exceeded their budget" line. #3635: invalid_summary
* attempts carry their rejection reason in parentheses. */
export function describeExternalSummaryFailure(batch: ExternalSummaryBatchResult): string {
if (batch.status === "failed") return `batch=failed reason=${batch.reason}`;
// #2678: multi-range batches report the FIRST failing range, not slot 0 —
// slot 0 may have succeeded while a later range is what stopped the batch.
// Single-range callers see identical output (find falls back to results[0]).
const result = batch.results.find((entry) => entry.status !== "success") ?? batch.results[0];
if (!result) return `batch=${batch.status} (no result)`;
const parts = [`batch=${batch.status}`, `result=${result.status}`];
if (result.status === "failed") parts.push(`reason=${result.reason}`);
parts.push(`attempts=[${result.attempts.map((a) => `t${a.targetIndex}:${a.outcome}${a.detail ? ` (${a.detail})` : ""}`).join(",")}]`);
return parts.join(" ");
}

function validBudget(budget: SummaryBudget): boolean {
return positiveInteger(budget.totalTimeoutMs) && budget.totalTimeoutMs <= 2_147_483_647
&& positiveInteger(budget.targetTimeoutMs) && budget.targetTimeoutMs <= 2_147_483_647
Expand Down Expand Up @@ -212,7 +257,7 @@ export class ExternalSummaryExecutor {
|| Buffer.byteLength(result.value, "utf8") > limits.maxSummaryBytes
|| result.value.trim().length < (request.minSummaryChars ?? 1)
|| ((request.maxSummaryChars ?? 0) > 0 && result.value.trim().length > request.maxSummaryChars!)) {
attempts.push({ targetIndex, outcome: "invalid_summary" });
attempts.push({ targetIndex, outcome: "invalid_summary", detail: invalidSummaryDetail(result.value, limits, request) });
} else {
attempts.push({ targetIndex, outcome: "success" });
return { status: "success", summary: result.value, targetIndex, attempts };
Expand Down
Loading
Loading