diff --git a/CONFIGURATION.md b/CONFIGURATION.md index a9de25ce50..ba9a6d0e5e 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -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. | @@ -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` diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index 9f295fed8f..978323b001 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -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 分级蒸馏把折叠成本摊到多代。 | @@ -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` diff --git a/src/compress-settings.ts b/src/compress-settings.ts index 65b74b5cfa..34e53ca3ed 100644 --- a/src/compress-settings.ts +++ b/src/compress-settings.ts @@ -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"), @@ -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, diff --git a/src/config.ts b/src/config.ts index b338b0955c..2f1c010275 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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 @@ -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 @@ -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)[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) { diff --git a/src/external-summary-compress.ts b/src/external-summary-compress.ts index 7551176f43..892910f627 100644 --- a/src/external-summary-compress.ts +++ b/src/external-summary-compress.ts @@ -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 @@ -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); @@ -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. diff --git a/src/external-summary.ts b/src/external-summary.ts index 4023372a1e..fc823a6ce8 100644 --- a/src/external-summary.ts +++ b/src/external-summary.ts @@ -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 = @@ -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): 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 @@ -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 }; diff --git a/src/preflight.ts b/src/preflight.ts index 18fafd452c..1fe42180cd 100644 --- a/src/preflight.ts +++ b/src/preflight.ts @@ -26,7 +26,10 @@ import { safePrefix } from "./text-safe.js"; import { applyEstimateCalibration, currentCalibrationFactor } from "./util.js"; import { configuredSummaryPlan, type ConfiguredSummaryPlan } from "./external-summary-runtime.js"; import { isCodexClient } from "./codex-compact.js"; -import type { ExternalSummaryBatchResult } from "./external-summary.js"; +import { describeExternalSummaryFailure, summaryLengthBudgetClause } from "./external-summary.js"; +// #3635: the failure description moved to the leaf module next to its data +// type; re-exported here for importers that historically reached it via preflight. +export { describeExternalSummaryFailure }; import type { ResolvedKernelConfig } from "./compress-settings.js"; import { newExternalCallId } from "./external-summary-marker.js"; @@ -1105,23 +1108,6 @@ function emptyCompletionDetail(json: Record): string | null { return parts.length > 0 ? ` (${parts.join(", ")})` : ""; } -/** 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. */ -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}`).join(",")}]`); - return parts.join(" "); -} - async function summarizeRange(deps: PreflightDeps, content: string, startRef: string, endRef: string, lengthBudget?: number, systemOverride?: string): Promise { // #1775: the cap used to be enforced only after the fact — an over-cap // assembly was discarded wholesale (the #1775 incident). Tell the model the @@ -1132,9 +1118,7 @@ async function summarizeRange(deps: PreflightDeps, content: string, startRef: st // place of the default tier-1 TASK; absent → byte-identical default below. const system = systemOverride ?? buildCompressSystemPrompt(deps.prompts, deps.surface?.promptSections) + `\n\nTASK: The conversation segment below (messages ${startRef}–${endRef}) must be compressed because the session context exceeds the current model's window. Write a tier-1 compression summary of the segment following every rule above. Output ONLY the summary text — no preamble, no closing remarks, no tool calls.` + - (lengthBudget !== undefined && lengthBudget >= MIN_SUMMARY_CHARS - ? `\n\nLENGTH BUDGET: Your ENTIRE response must be AT MOST ${lengthBudget} characters total — longer output is rejected by the pipeline. Be dense: compact bullets, no filler or repetition.` - : ""); + summaryLengthBudgetClause(lengthBudget, MIN_SUMMARY_CHARS); if (deps.externalSummary) { const batch = await deps.externalSummary.summarize([{ instructions: system, content, minSummaryChars: MIN_SUMMARY_CHARS, maxSummaryChars: deps.config.compress.maxSummaryLength }], deps.signal); @@ -2320,9 +2304,7 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa const instructions = buildCompressSystemPrompt(deps.prompts, deps.surface?.promptSections) + `\n\nTASK: The conversation segment below (messages ${startRef}–${endRef}) must be compressed because the session context exceeds the current model's window. Write a tier-1 compression summary of the segment following every rule above. Output ONLY the summary text — no preamble, no closing remarks, no tool calls.` + - (perChunkBudget !== undefined && perChunkBudget >= MIN_SUMMARY_CHARS - ? `\n\nLENGTH BUDGET: Your ENTIRE response must be AT MOST ${perChunkBudget} characters total — longer output is rejected by the pipeline. Be dense: compact bullets, no filler or repetition.` - : ""); + summaryLengthBudgetClause(perChunkBudget, MIN_SUMMARY_CHARS); nominations.push({ skipKey, startRef, endRef, content, chunks, instructions, effectiveMessageIds: [...planned.effectiveMessageIds], spanUnits, offset: workTotal }); workTotal += chunks.length; } diff --git a/tests/issue3635-summary-length-cap.test.ts b/tests/issue3635-summary-length-cap.test.ts new file mode 100644 index 0000000000..f563069cb4 --- /dev/null +++ b/tests/issue3635-summary-length-cap.test.ts @@ -0,0 +1,163 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import http from "node:http"; +import { once } from "node:events"; +import { mkdtempSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { createCore, defaultConfig, type Config, type CoreMessage } from "acp-kernel"; +import { ExternalSummaryExecutor, describeExternalSummaryFailure, summaryLengthBudgetClause } from "../src/external-summary.ts"; +import { applyConfiguredCompression } from "../src/external-summary-compress.ts"; +import { SummaryCredentialStore } from "../src/external-summary-credentials.ts"; +import { parseCompressSettings } from "../src/config.ts"; +import { mergeCompress, applyCompressSettings } from "../src/compress-settings.ts"; +import { createSession } from "../src/session.ts"; +import type { RewriteCtx } from "../src/stream.ts"; +import { rmrf } from "./tmp-rm.ts"; + +process.env.NODE_ENV = "test"; + +const BUDGET = { totalTimeoutMs: 2000, targetTimeoutMs: 1000, maxSummaryBytes: 10_000 }; +const CLAUSE = "\n\nLENGTH BUDGET: Your ENTIRE response must be AT MOST 20000 characters total — longer output is rejected by the pipeline. Be dense: compact bullets, no filler or repetition."; + +function candidate(reply: string) { + return { summarize: async () => reply }; +} + +test("invalid_summary attempts name the exact rejection reason (#3635)", async () => { + const executor = new ExternalSummaryExecutor(1); + const overCap = await executor.execute( + { content: "x".repeat(100), instructions: "summarize", maxSummaryChars: 10 }, + [candidate("y".repeat(15))], BUDGET); + assert.deepEqual(overCap, { status: "failed", reason: "exhausted", attempts: [{ targetIndex: 0, outcome: "invalid_summary", detail: "summary 15 chars exceeds cap 10" }] }); + + const tooShort = await executor.execute( + { content: "x".repeat(100), instructions: "summarize", minSummaryChars: 100, maxSummaryChars: 1000 }, + [candidate("short")], BUDGET); + assert.deepEqual(tooShort, { status: "failed", reason: "exhausted", attempts: [{ targetIndex: 0, outcome: "invalid_summary", detail: "summary 5 chars below floor 100" }] }); + + // The byte cap fires before the char checks (validator order). + const overBytes = await executor.execute( + { content: "x".repeat(100), instructions: "summarize" }, + [candidate("\u4e2d".repeat(10))], { ...BUDGET, maxSummaryBytes: 20 }); + assert.deepEqual(overBytes, { status: "failed", reason: "exhausted", attempts: [{ targetIndex: 0, outcome: "invalid_summary", detail: "response 30 bytes exceeds byte cap 20" }] }); + + const empty = await executor.execute( + { content: "x".repeat(100), instructions: "summarize" }, + [candidate(" \n")], BUDGET); + assert.deepEqual(empty, { status: "failed", reason: "exhausted", attempts: [{ targetIndex: 0, outcome: "invalid_summary", detail: "empty or non-string response" }] }); +}); + +test("describeExternalSummaryFailure renders the detail and keeps its legacy form without one (#3635/#2484)", () => { + assert.equal( + describeExternalSummaryFailure({ status: "finished", results: [{ status: "failed", reason: "exhausted", attempts: [{ targetIndex: 0, outcome: "invalid_summary", detail: "summary 20059 chars exceeds cap 20000" }] }] }), + "batch=finished result=failed reason=exhausted attempts=[t0:invalid_summary (summary 20059 chars exceeds cap 20000)]"); + // No detail → byte-identical to the pre-#3635 rendering (pinned by tests/issue2484-overflow-and-summary.test.ts). + assert.equal( + describeExternalSummaryFailure({ status: "finished", results: [{ status: "failed", reason: "exhausted", attempts: [{ targetIndex: 0, outcome: "timeout" }] }] }), + "batch=finished result=failed reason=exhausted attempts=[t0:timeout]"); +}); + +test("summaryLengthBudgetClause gates on a finite budget at or above the floor (#3635)", () => { + assert.equal(summaryLengthBudgetClause(20000, 50), CLAUSE); + assert.ok(summaryLengthBudgetClause(50, 50).includes("AT MOST 50 characters")); + assert.equal(summaryLengthBudgetClause(undefined, 50), ""); + assert.equal(summaryLengthBudgetClause(Number.POSITIVE_INFINITY, 50), ""); + assert.equal(summaryLengthBudgetClause(0, 50), ""); + assert.equal(summaryLengthBudgetClause(49, 50), ""); + // 0 = unbounded cap must stay omitted even with the floor disabled — never "AT MOST 0". + assert.equal(summaryLengthBudgetClause(0, 0), ""); +}); + +test("parseCompressSettings accepts the summary-length caps; 0 stays meaningful (#3635)", () => { + assert.equal(parseCompressSettings({ maxSummaryLength: 32000, minSummaryLength: 80 })?.maxSummaryLength, 32000); + assert.equal(parseCompressSettings({ maxSummaryLength: 32000, minSummaryLength: 80 })?.minSummaryLength, 80); + assert.equal(parseCompressSettings({ maxSummaryLength: 0 })?.maxSummaryLength, 0); + assert.equal(parseCompressSettings({ minSummaryLength: 0 })?.minSummaryLength, 0); + assert.equal(parseCompressSettings({ maxSummaryLength: -1 }), undefined); + assert.equal(parseCompressSettings({ minSummaryLength: -1 }), undefined); + assert.equal(parseCompressSettings({ maxSummaryLength: "x" }), undefined); + assert.equal(parseCompressSettings({ maxSummaryLength: Number.NaN }), undefined); +}); + +test("mergeCompress carries the summary-length caps across levels (#3635)", () => { + const merged = mergeCompress({ maxSummaryLength: 100 }, { minSummaryLength: 20 }, { maxSummaryLength: 300 }); + assert.equal(merged.maxSummaryLength, 300); + assert.equal(merged.minSummaryLength, 20); +}); + +test("applyCompressSettings maps the caps onto the resolved kernel config (#3635)", () => { + const base = defaultConfig(100_000); + assert.equal(base.compress.maxSummaryLength, 20000); + assert.equal(base.compress.minSummaryLength, 50); + const applied = applyCompressSettings(base, 100_000, { maxSummaryLength: 32000, minSummaryLength: 80 }); + assert.equal(applied.compress.maxSummaryLength, 32000); + assert.equal(applied.compress.minSummaryLength, 80); + const untouched = applyCompressSettings(base, 100_000, {}); + assert.equal(untouched.compress.maxSummaryLength, 20000); + assert.equal(untouched.compress.minSummaryLength, 50); +}); + +const WIRE_SUMMARY = "Historical build completed. Preserve src/example.ts:27, error E_TEST, the no-deploy constraint, and the unfinished testing task."; +const WIRE_RAW = "Historical source src/example.ts:27 E_TEST; do not deploy; still add tests. ".repeat(90); + +function wireContext(settings: unknown, cap?: number): RewriteCtx { + const core = createCore(); + const config = defaultConfig(400_000) as Config & { externalSummary?: unknown }; + config.externalSummary = settings; + config.preserveRecentMessages = 0; + config.preserveRecentTokens = 0; + config.compress.minCompressRange = 100; + if (cap !== undefined) config.compress.maxSummaryLength = cap; + const session = createSession(`issue3635-${Math.random()}`); + const messages: CoreMessage[] = [ + { id: "history", role: "assistant", contentType: "text", text: WIRE_RAW }, + { id: "current-task", role: "user", contentType: "text", text: "Current task: finish tests without deployment." }, + ]; + session.state = core.processTurn({ messages, state: session.state, config, tokenCount: 10000, renderTags: "text-only" }).state; + return { core, config, session, messages, log: () => {} }; +} +function wireArgs(ctx: RewriteCtx): Record { + return { content: [{ startId: ctx.session.state.messageRefs.byRaw.history, endId: ctx.session.state.messageRefs.byRaw.history }] }; +} + +// #3635 root cause on the wire: the model-tool compress path now states the +// EXACT cap its validator enforces to the external summarizer (the preflight +// path has done this since #1775), and a configured cap flows through to it. +test("the compress path tells external summarizers the exact enforced cap on the wire (#3635)", async () => { + for (const [cap, expected] of [[undefined, "AT MOST 20000 characters"], [32000, "AT MOST 32000 characters"]] as const) { + const bodies: string[] = []; + const root = mkdtempSync(join(tmpdir(), "bili-3635-wire-")); + const previous = process.env.BILI_CONFIG_FILE; + process.env.BILI_CONFIG_FILE = join(root, "config.json"); + const server = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (chunk: Buffer) => chunks.push(chunk)); + req.on("end", () => { + bodies.push(Buffer.concat(chunks).toString("utf8")); + res.setHeader("content-type", "application/json"); + res.end(JSON.stringify({ status: "completed", output_text: WIRE_SUMMARY })); + }); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const base = `http://127.0.0.1:${(server.address() as import("node:net").AddressInfo).port}`; + const store = new SummaryCredentialStore(); + store.set("primary", "test-primary-key"); + const settings = { enabled: true, targets: [{ name: "primary", protocol: "responses", url: `${base}/primary`, model: "summary-primary", credentialRef: "secret:primary" }] }; + try { + const ctx = wireContext(settings, cap); + const result = await applyConfiguredCompression(wireArgs(ctx), ctx); + assert.equal(result.outcome, "applied", result.text); + assert.equal(bodies.length, 1); + assert.ok(bodies[0].includes(expected), `cap ${String(cap)}: clause missing from the wire body`); + assert.ok(bodies[0].includes("rejected by the pipeline")); + } finally { + server.closeAllConnections(); + await new Promise((resolve) => server.close(() => resolve())); + if (previous === undefined) delete process.env.BILI_CONFIG_FILE; + else process.env.BILI_CONFIG_FILE = previous; + rmrf(root); + } + } +}); diff --git a/website/config-reference/compress.yaml b/website/config-reference/compress.yaml index 87b4db99c0..677fb4235d 100644 --- a/website/config-reference/compress.yaml +++ b/website/config-reference/compress.yaml @@ -132,6 +132,26 @@ entries: desc_zh: "可折叠片段的最小字符数;更短的永不折叠。" site_en: "Smallest foldable range, in characters (kernel default ≈5000) — shorter ranges never fold." site_zh: "可折叠片段的最小字符数(内核默认约 5000),更短的永不折叠。" + - key: compress.maxSummaryLength + type: number + default: "kernel 20000" + group: compress + id: cw.msl + render: compress-row + desc_en: "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." + desc_zh: "单条生成摘要的硬字符上限;超长的回复被整段拒绝(不截断、不重试)。外部摘要模型会被提前告知这个确切上限(#3635)。0 = 不限制。" + site_en: "Hard cap, in characters, on one generated summary (kernel default 20000): longer replies are rejected whole by the kernel check or the external-summary executor. External summarizers receive this exact cap in their instructions (#3635); 0 disables the cap." + site_zh: "单条生成摘要的硬上限,单位为字符(内核默认 20000):超长的回复由内核检查或外部摘要执行器整段拒绝。外部摘要模型的指令中会写明这个确切上限(#3635);0 禁用上限。" + - key: compress.minSummaryLength + type: number + default: "kernel 50" + group: compress + id: cw.mnl + render: compress-row + desc_en: "Character floor for generated summaries; shorter replies are rejected whole. 0 = disabled. External summarizers are held to the same floor (#3635)." + desc_zh: "生成摘要的字符下限;过短的回复被整段拒绝。0 = 禁用。外部摘要模型受同一下限约束(#3635)。" + site_en: "Floor, in characters, for generated summaries (kernel default 50): shorter replies are rejected whole; 0 disables the floor. External summarizers are held to the same floor (#3635)." + site_zh: "生成摘要的下限,单位为字符(内核默认 50):过短的回复被整段拒绝;0 禁下限。外部摘要模型受同一下限约束(#3635)。" - key: compress.reconcile type: "\"off\" | \"warn\" | \"repair\"" default: "\"repair\"" diff --git a/website/docs/index.html b/website/docs/index.html index 5e50a4492a..eeb6427605 100644 --- a/website/docs/index.html +++ b/website/docs/index.html @@ -191,6 +191,8 @@

compress — every knob

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. minCompressRangeCharsnumber (deprecated alias: minCompressRange)Smallest foldable range, in characters (kernel default ≈5000) — shorter ranges never fold. + maxSummaryLengthnumberHard cap, in characters, on one generated summary (kernel default 20000): longer replies are rejected whole by the kernel check or the external-summary executor. External summarizers receive this exact cap in their instructions (#3635); 0 disables the cap. + minSummaryLengthnumberFloor, in characters, for generated summaries (kernel default 50): shorter replies are rejected whole; 0 disables the floor. External summarizers are held to the same floor (#3635). reconcile"off" | "warn" | "repair""off" | "warn" | "repair" (default repair): reconcile folded state when the client rewinds or rewrites history between turns. promptPackstring (builtin: "default", "lean")Compression prompt pack, resolved project pack → user pack → builtin. Builtins: "default" and "lean" (one-line tool descriptions). Not gated by acknowledgePromptsRisk. tiersbooleanOn by default: tiered T1→T3 distillation spreads folding cost across generations. diff --git a/website/main.js b/website/main.js index bf7b1cc7c8..39c1c6435f 100644 --- a/website/main.js +++ b/website/main.js @@ -74,6 +74,8 @@ "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.", "cw.mrc": "Smallest foldable range, in characters (kernel default ≈5000) — shorter ranges never fold.", + "cw.msl": "Hard cap, in characters, on one generated summary (kernel default 20000): longer replies are rejected whole by the kernel check or the external-summary executor. External summarizers receive this exact cap in their instructions (#3635); 0 disables the cap.", + "cw.mnl": "Floor, in characters, for generated summaries (kernel default 50): shorter replies are rejected whole; 0 disables the floor. External summarizers are held to the same floor (#3635).", "cw.rec": "\"off\" | \"warn\" | \"repair\" (default repair): reconcile folded state when the client rewinds or rewrites history between turns.", "cw.ppk": "Compression prompt pack, resolved project pack → user pack → builtin. Builtins: \"default\" and \"lean\" (one-line tool descriptions). Not gated by acknowledgePromptsRisk.", "cw.tiers": "On by default: tiered T1→T3 distillation spreads folding cost across generations.", @@ -316,6 +318,8 @@ "cw.prm": "内核默认 3(5 → 3,#3058):最近消息软保护、免于折叠。", "cw.prt": "内核默认约 5000:最近的 token 软保护、免于折叠。", "cw.mrc": "可折叠片段的最小字符数(内核默认约 5000),更短的永不折叠。", + "cw.msl": "单条生成摘要的硬上限,单位为字符(内核默认 20000):超长的回复由内核检查或外部摘要执行器整段拒绝。外部摘要模型的指令中会写明这个确切上限(#3635);0 禁用上限。", + "cw.mnl": "生成摘要的下限,单位为字符(内核默认 50):过短的回复被整段拒绝;0 禁下限。外部摘要模型受同一下限约束(#3635)。", "cw.rec": "\"off\" | \"warn\" | \"repair\"(默认 repair):客户端在轮次之间回退或改写历史时校准已折叠状态。", "cw.ppk": "压缩提示词包,按 项目包 → 用户包 → 内置 解析。内置:\"default\" 与 \"lean\"(单行工具描述)。不受 acknowledgePromptsRisk 门控。", "cw.tiers": "默认开:T1→T3 分级蒸馏,把折叠成本摊到多代。",