Skip to content
Open
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
8 changes: 7 additions & 1 deletion src/compress-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export function mergeCompress(
emergencyThresholdPercent: pick("emergencyThresholdPercent"),
nudgeGrowthTokens: pick("nudgeGrowthTokens"),
minPressureBenefitTokens: pick("minPressureBenefitTokens"),
minFoldBenefitTokens: pick("minFoldBenefitTokens"),
requireUsageSampleForPressure: pick("requireUsageSampleForPressure"),
maxEstimateHoldRounds: pick("maxEstimateHoldRounds"),
tierNudgeTokens: tierNudgeLevels.length > 0 ? Object.assign({}, ...tierNudgeLevels) : undefined,
Expand Down Expand Up @@ -298,7 +299,7 @@ export function hasCompressSettings(s: CompressSettings): boolean {
// built and typed here regardless of the installed kernel's Config so both
// release windows compile identically. Older kernels ignore the extra key
// (validateConfig does not flag unknown keys).
export type ResolvedKernelConfig = Config & { preserveRecentTools?: string[]; externalSummary?: ExternalSummarySettings };
export type ResolvedKernelConfig = Config & { preserveRecentTools?: string[]; externalSummary?: ExternalSummarySettings; minFoldBenefitTokens?: number };

// Single source of truth for raw->resolved absorb: applyCompressSettings and the
// plugin-lane base stamp (#1359) both go through this or they drift apart.
Expand Down Expand Up @@ -400,6 +401,11 @@ export function applyCompressSettings(base: Config, limit: number, s: CompressSe
// HERE, at resolve time — the executor still receives a fully
// expanded ExternalSummarySettings and never sees references.
externalSummary: s.externalSummary !== undefined ? expandExternalSummaryChainTolerant(s.externalSummary, recipes) : (base as ResolvedKernelConfig).externalSummary,
// #7333: host-only min-benefit floor for the model-initiated compress
// commit gate (applyRanges) — never on the raw kernel Config, carried
// like externalSummary. Absent (undefined) = the gate's default
// (AUTO_FOLD_MIN_BENEFIT_TOKENS) applies.
minFoldBenefitTokens: s.minFoldBenefitTokens ?? (base as ResolvedKernelConfig).minFoldBenefitTokens,
...(absorb !== undefined ? { absorb } : {}),
// #5765: one-switch opt-in — absent `s.blockOps` leaves base untouched
// (off); a present block normalizes enabled (absent/false = off).
Expand Down
15 changes: 15 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,21 @@ export type CompressSettings = {
* level wins PER FIELD across global → provider → model, like every
* other compress field. */
minPressureBenefitTokens?: number;
/** #7333: minimum net-reclaim (tokens) a model-initiated compress must
* achieve to be committed, BELOW the pressure band. Every committed fold
* rewrites the context prefix and re-bills the prompt-cache tail, so a
* burst of tiny model-initiated folds (one nudge → many small folds)
* re-bills the tail repeatedly for almost no reclaim. This floor refuses
* such sub-benefit folds with a teaching receipt (batch the listed ranges
* into ONE call, or continue) instead of committing them. At/above
* `maxContextLimit` or in emergency the floor drops to 1 — survival folds
* are never blocked. Default: `AUTO_FOLD_MIN_BENEFIT_TOKENS` (4096) — the
* same bar bili's own auto-fold producer already applies (#3444), so both
* fold producers share one benefit floor. Set 0 to restore the legacy
* any-positive-shrink gate. Host-side only (the model-initiated
* `applyRanges` commit gate); does NOT map to a kernel `nudge.*` field.
* Deepest level wins PER FIELD, like every other compress field. */
minFoldBenefitTokens?: number;
/** Phantom-pressure guard: when the decision meter for this round is a
* LOCAL ESTIMATE rather than a billing sample (the host passed no usage
* report — e.g. right after an aborted turn or a short cache TTL that
Expand Down
61 changes: 54 additions & 7 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ 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 type { ResolvedKernelConfig } from "./compress-settings.js";
import { AUTO_FOLD_MIN_BENEFIT_TOKENS } from "./external-summary-settings.js";
import { NUDGE_CREDIT_ROUNDS, unpromptedGateOf } from "./unprompted-gate.js";
import { describeUncoveredRuns, shortTokenCount, uncoveredHistoryRuns } from "./uncovered.js";

Expand Down Expand Up @@ -1158,6 +1160,27 @@ function outOfRecommendedRanges(ranges: ParsedRange[], ctx: RewriteCtx): string[
return offending;
}

/** #7333: is this request in the pressure band (usage ≥ maxContextLimit) or
* emergency (usage ≥ emergencyThreshold)? Mirrors the kernel's own check
* (kernel/src/nudge-text.ts: `!!breakdown?.emergencyOverride ||
* !!breakdown?.overLimit`). The min-benefit floor drops to 1 in-band so
* survival folds are never blocked. Fails safe to "not in band" (apply the
* floor) when the prepare-time nudge is absent — a band fold is typically
* large, and `minFoldBenefitTokens = 0` is the escape hatch. */
function inPressureBandOrEmergency(ctx: RewriteCtx): boolean {
const b = ctx.lastNudge?.breakdown;
return !!b?.emergencyOverride || !!b?.overLimit;
}

/** #7333: the minimum net-reclaim (tokens) a model-initiated compress must
* achieve to commit, below the pressure band. Default
* AUTO_FOLD_MIN_BENEFIT_TOKENS (4096) — the same bar bili's own auto-fold
* producer applies (#3444), so both fold producers share one benefit floor.
* 0 restores the legacy any-positive-shrink gate (the caller clamps to ≥ 1). */
function minFoldBenefitTokens(config: Config): number {
return (config as ResolvedKernelConfig).minFoldBenefitTokens ?? AUTO_FOLD_MIN_BENEFIT_TOKENS;
}

export function applyRanges(parsed: ReturnType<typeof parseCompressInput>, 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
Expand Down Expand Up @@ -1499,23 +1522,47 @@ export function applyRanges(parsed: ReturnType<typeof parseCompressInput>, ctx:
netDelta += defaultCountTokens(prev) - defaultCountTokens(b.summary);
}
}
if (r.blocksCreated > 0 && touchedBlocks > 0 && netDelta <= 0) {
// #7333: minimum-benefit floor — generalizes the #2748 no-net-shrink
// gate. A committed fold rewrites the context prefix and re-bills the
// prompt-cache tail, so a burst of tiny model-initiated folds (one
// nudge → many small folds) re-bills the tail repeatedly for almost no
// reclaim. Below the pressure band, refuse sub-floor folds with a
// teaching receipt (batch the listed ranges into ONE call, or continue)
// instead of committing them. At/above maxContextLimit or in emergency
// the floor drops to 1 — survival folds are never blocked.
// minFoldBenefitTokens = 0 restores the legacy any-positive-shrink gate
// (floor clamps to 1). Refold-only calls (blocksCreated = 0) keep
// skipping this gate, exactly as today.
// The floor is a MODEL-FACING bar: it gates the model-initiated path
// (trackLoop) only. Bili's own overflow backstop folds ride
// loopTracking:false and must not be gated by a benefit floor — for
// them the floor stays at 1, i.e. the original no-net-shrink gate.
const minBenefitFloor = trackLoop
? (inPressureBandOrEmergency(ctx) ? 1 : Math.max(1, minFoldBenefitTokens(ctx.config)))
: 1;
if (r.blocksCreated > 0 && touchedBlocks > 0 && netDelta < minBenefitFloor) {
const gateReason = netDelta <= 0 ? "gate:no-net-shrink" : "gate:below-min-benefit";
const detail = ranges.map((rg) => `${rg.startRef}–${rg.endRef}`).join(", ");
const revNote = revs.length > 0
? ` Note: startId > endId in range(s) ${revs.map((rg) => `${rg.startRef}→${rg.endRef}`).join(", ")} — your refs were reversed and normalized to ascending order before evaluation.`
: "";
const netDesc = netDelta < 0
? `committing it would grow the context by ${-netDelta} token(s)`
: `committing it leaves the context size unchanged`;
// #847 repeat-guard: re-issuing the SAME bloated range escalates
// advice; deliberately NOT fed into the #2146 circuit breaker — a
// too-long summary is recoverable (write a shorter one), so arming
// "stop calling compress entirely" would wrongly kill legit folds.
const specKey = normalizedSpecKey(ranges);
const repeatGuard = recordCompressFailure(ctx.session, specKey);
ctx.log(`[acp-proxy: compress REFUSED ${detail} — no net shrink (net delta=${netDelta} tok across ${touchedBlocks} touched block(s)); state left uncommitted, prefix cache untouched]`);
const receipt = `[Compression refused: no net shrink — the summary you supplied is not smaller than the range it replaces (${netDesc}). Nothing was folded: no block was created and the context prefix/cache were left untouched.${revNote} Do not re-issue this same range and summary; supply a much shorter summary for the range, target a larger span so the summary shrinks it, or continue your task without compressing.${repeatGuard !== "" ? " " + repeatGuard : ""}]`;
return compressResult(receipt, "refused", 0, "gate:no-net-shrink");
ctx.log(`[acp-proxy: compress REFUSED ${detail} — ${gateReason} (net delta=${netDelta} tok across ${touchedBlocks} touched block(s), floor=${minBenefitFloor}); state left uncommitted, prefix cache untouched]`);
let receipt: string;
if (gateReason === "gate:below-min-benefit") {
receipt = `[Compression refused: below minimum benefit — this call would reclaim only ${netDelta} token(s), below compress.minFoldBenefitTokens (${minBenefitFloor}). Every committed fold rewrites the context prefix and re-bills the prompt-cache tail, so a burst of tiny folds re-bills the tail for almost no reclaim.${revNote} Put the other listed ranges into ONE compress call so the summary covers them together, or continue your task without compressing. Nothing was folded and the cache is untouched.${repeatGuard !== "" ? " " + repeatGuard : ""}]`;
} else {
const netDesc = netDelta < 0
? `committing it would grow the context by ${-netDelta} token(s)`
: `committing it leaves the context size unchanged`;
receipt = `[Compression refused: no net shrink — the summary you supplied is not smaller than the range it replaces (${netDesc}). Nothing was folded: no block was created and the context prefix/cache were left untouched.${revNote} Do not re-issue this same range and summary; supply a much shorter summary for the range, target a larger span so the summary shrinks it, or continue your task without compressing.${repeatGuard !== "" ? " " + repeatGuard : ""}]`;
}
return compressResult(receipt, "refused", 0, gateReason);
}
ctx.session.state = res.state;
// #2627 backstop: no committed state may carry outbound-only turn-
Expand Down
5 changes: 3 additions & 2 deletions tests/agent-providers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { SessionStore, _setStoreForTest } from "../src/persist.ts";
import { _setForTest as setRegistryForTest } from "../src/registry.ts";
import type { RewriteCtx } from "../src/stream.ts";
import { rmrf } from "./tmp-rm.ts";
import type { ResolvedKernelConfig } from "../src/compress-settings.ts";

process.env.NODE_ENV = "test";

Expand Down Expand Up @@ -160,7 +161,7 @@ test("#2336 endpoint + web GET/PUT contract through a real server", async () =>
proxyMode: "direct",
proxySource: "direct",
modelContextLimit: 400_000,
kernelConfig: defaultConfig(400_000),
kernelConfig: { ...defaultConfig(400_000), minFoldBenefitTokens: 0 } as ResolvedKernelConfig,
compress: { injectTool: true, injectNudge: true },
promptCache: { routing: "auto" },
sessionHeader: "x-acp-session",
Expand Down Expand Up @@ -255,7 +256,7 @@ const RAW = "Historical source for the inline-key dial test. ".repeat(120);

function context(externalSummary: unknown): RewriteCtx {
const core = createCore();
const config = defaultConfig(400_000) as Config & { externalSummary?: unknown };
const config = { ...defaultConfig(400_000), minFoldBenefitTokens: 0 } as Config & { externalSummary?: unknown };
config.externalSummary = externalSummary;
config.preserveRecentMessages = 0;
config.preserveRecentTokens = 0;
Expand Down
2 changes: 1 addition & 1 deletion tests/anthropic-round2-thinking-hold.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const UNSIGNED_BODY = { model: "claude", messages: [], stream: true, max_tokens:
function makeCtx(id: string, messages: CoreMessage[] = []): LoopCtx {
return {
core: createCore(),
config: defaultConfig(200000) as Config,
config: { ...defaultConfig(200000), minFoldBenefitTokens: 0 } as Config,
messages,
session: {
id,
Expand Down
2 changes: 1 addition & 1 deletion tests/cache-friendly-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ async function driveWire(wire: Wire, steps: Step[], opts?: DriveOpts): Promise<C
port: 0, host: "127.0.0.1", upstream: "http://127.0.0.1",
routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { [adapter.model]: { context: 400_000 } } } },
modelContextLimit: 400_000,
kernelConfig: defaultConfig(400_000),
kernelConfig: { ...defaultConfig(400_000), minFoldBenefitTokens: 0 },
compress: { injectTool: true, injectNudge: true, nudgeGrowthTokens: 500 },
promptCache: { routing: "auto" },
sessionHeader: "x-acp-session",
Expand Down
3 changes: 2 additions & 1 deletion tests/cache-friendly-proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { rmrf } from "./tmp-rm.ts";
import { buildAbsorbSystemPrompt, ABSORB_TOOL_NAME } from "../src/compress-tool.ts";
import { GENERATION_BOUNDARY_NOTE } from "../src/generation-boundary.ts";
import { RESPONSES_SYSTEM_SECTION_SEPARATOR } from "../src/responses-system-sections.ts";
import type { ResolvedKernelConfig } from "../src/compress-settings.ts";

type Item = Record<string, unknown>;
type Wire = "responses" | "chat" | "anthropic" | "google";
Expand Down Expand Up @@ -333,7 +334,7 @@ function proxyOptions(upstreamPort: number, model: string, ctx: number): ProxyOp
upstream: "http://127.0.0.1",
routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { [model]: { context: ctx } } } },
modelContextLimit: ctx,
kernelConfig: defaultConfig(ctx),
kernelConfig: { ...defaultConfig(ctx), minFoldBenefitTokens: 0 } as ResolvedKernelConfig,
compress: { injectTool: true, injectNudge: true },
promptCache: { routing: "auto" },
sessionHeader: "x-acp-session",
Expand Down
8 changes: 4 additions & 4 deletions tests/ccr-v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
type CoreMessage,
type MessageContentStore,
} from "acp-kernel";
import { applyCompressSettings } from "../src/compress-settings.ts";
import { applyCompressSettings, type ResolvedKernelConfig } from "../src/compress-settings.ts";
import { adoptContentStore, contentStoreOf, drainPendingRetrievals, storeEffectiveCcr } from "../src/store.ts";
import { getSession } from "../src/session.ts";
import { SessionStore, _setStoreForTest } from "../src/persist.ts";
Expand Down Expand Up @@ -62,7 +62,7 @@ type FoldResult = {
function fold(opts: { ccr: boolean; preStore?: MessageContentStore }): FoldResult {
const core = createCore();
const config = applyCompressSettings(
defaultConfig(200_000),
{ ...defaultConfig(200_000), minFoldBenefitTokens: 0 } as ResolvedKernelConfig,
200_000,
{ absorb: { enabled: true, minToolTokens: 50 }, ccr: opts.ccr ? { enabled: true, minToolTokens: 50 } : { enabled: false, minToolTokens: 50 } },
) as Config;
Expand Down Expand Up @@ -317,7 +317,7 @@ async function startV2Proxy(captured: string[], noCcr?: boolean): Promise<{ prox
[`http://127.0.0.1:${upstreamPort}`]: { models: { "claude-test": { context: 400_000 } } },
},
modelContextLimit: 400_000,
kernelConfig: defaultConfig(400_000),
kernelConfig: { ...defaultConfig(400_000), minFoldBenefitTokens: 0 } as ResolvedKernelConfig,
compress: noCcr ? { injectTool: true, injectNudge: false, minCompressRangeChars: 1000 } : { injectTool: true, injectNudge: false, minCompressRangeChars: 1000, ccr: { enabled: true } },
compat: { roles: {} },
streamErrorShape: "protocol",
Expand Down Expand Up @@ -455,7 +455,7 @@ test("e2e CCR v2 non-stream: fold persists across turns; range restore delivered
[`http://127.0.0.1:${upstreamPort}`]: { models: { "claude-test": { context: 400_000 } } },
},
modelContextLimit: 400_000,
kernelConfig: defaultConfig(400_000),
kernelConfig: { ...defaultConfig(400_000), minFoldBenefitTokens: 0 } as ResolvedKernelConfig,
compress: { injectTool: true, injectNudge: false, minCompressRangeChars: 1000, ccr: { enabled: true } },
compat: { roles: {} },
streamErrorShape: "protocol",
Expand Down
2 changes: 1 addition & 1 deletion tests/claude-bridge-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ async function startRig(): Promise<Rig> {
upstream: "http://127.0.0.1",
routes: { [`http://127.0.0.1:${upstreamPort}`]: {} },
modelContextLimit: 200_000,
kernelConfig: defaultConfig(200_000),
kernelConfig: { ...defaultConfig(200_000), minFoldBenefitTokens: 0 },
compress: { injectTool: true, injectNudge: true },
promptCache: { routing: "auto" },
sessionHeader: "x-acp-session",
Expand Down
5 changes: 3 additions & 2 deletions tests/claude-fork-lineage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { claudeForkParentFromArgv, hookMainWithDeps } from "../src/claude-native
import { PassThrough } from "node:stream";
import type { ProxyOptions } from "../src/config.ts";
import { _setForTest as setRegistryForTest } from "../src/registry.ts";
import type { ResolvedKernelConfig } from "../src/compress-settings.ts";

/**
* #2408 claude --fork-session lineage declaration. `claude --resume <parent>
Expand Down Expand Up @@ -220,11 +221,11 @@ function proxyOpts(relayUrl: string): ProxyOptions {
upstream: "http://127.0.0.1",
routes: {} as ProxyOptions["routes"],
modelContextLimit: 1_000_000,
kernelConfig: defaultConfig(1_000_000, {
kernelConfig: { ...defaultConfig(1_000_000, {
preserveRecentMessages: 2,
preserveRecentTokens: 400,
compress: { minCompressRange: 200, minSummaryLength: 20, maxSummaryLength: 5000 },
}),
}), minFoldBenefitTokens: 0 } as ResolvedKernelConfig,
compress: { injectTool: true, injectNudge: false },
promptCache: { routing: "auto" },
forkAdoption: true,
Expand Down
2 changes: 1 addition & 1 deletion tests/codex-compact-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ async function withHarness(opts: { mode?: string; firstTurnTokens: number; stric
upstream: "http://127.0.0.1",
routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "gpt-resp": { context: 15_000 } } } },
modelContextLimit: 15_000,
kernelConfig: defaultConfig(15_000),
kernelConfig: { ...defaultConfig(15_000), minFoldBenefitTokens: 0 },
compress: { injectTool: true, injectNudge: true },
promptCache: { routing: "auto" },
sessionHeader: "x-acp-session",
Expand Down
3 changes: 2 additions & 1 deletion tests/compress-fingerprint-refold.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import { parseCompressInput } from "../src/compress-tool.ts";
import { storeUnpromptedGate } from "../src/unprompted-gate.ts";
import { resolveDecompress } from "../src/decompress-shared.ts";
import { SessionStore, _setStoreForTest } from "../src/persist.ts";
import type { ResolvedKernelConfig } from "../src/compress-settings.ts";

type Ctx = Omit<RewriteCtx, "log"> & { log: (m: string) => void; logs: string[] };

function makeCtx(): Ctx {
const logs: string[] = [];
const ctx: Ctx = {
core: createCore(),
config: defaultConfig(200000),
config: { ...defaultConfig(200000), minFoldBenefitTokens: 0 } as ResolvedKernelConfig,
messages: [] as CoreMessage[],
session: {
id: `fpr-${Math.random().toString(36).slice(2)}`,
Expand Down
Loading
Loading