Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/server/budget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { readOutputBudget, writeOutputBudget, type OutputBudgetField } from "./s
const OUTPUT_CLAMP_MARGIN_PCT = 0.05;
const OUTPUT_CLAMP_MIN_MARGIN = 2048;
const OUTPUT_CLAMP_FLOOR = 1024;
// #3071/#2490: capability floor for the clamped budget — see clampOutputBudget.
const OUTPUT_CLAMP_MIN_OUTPUT = 4096;
// #453 mitigation: host-side escalation line. The kernel already force-injects
// every turn once usage >= nudge.maxContextLimitPct (its pressure branch has no
// cadence gate), so the only cadence-silent zone is BELOW that line. Force the
Expand Down Expand Up @@ -240,7 +242,14 @@ export function estimateWireOverhead(protocol: "anthropic" | "openai" | "respons
* checkpoints never land), so no output budget recovers the session; a
* sub-floor clamp would only starve a terminal turn. That surrender is
* deliberate, not a gap (pinned in
* tests/issue2490-ledger-floor-doomed-probe.test.ts). */
* tests/issue2490-ledger-floor-doomed-probe.test.ts).
* #3071: where a clamp DOES fire (cap >= OUTPUT_CLAMP_FLOOR), the 5% margin drags
* cap into the low thousands near the window (observed 1479/2623), starving the
* reply to an empty finish=length turn even though raw headroom still fits a normal
* reply. Floor the reduction target at min(OUTPUT_CLAMP_MIN_OUTPUT, window - input)
* — always <= window - input, so input+output can never exceed the window
* (fail-closed preserved). The sub-floor surrender above (cap < OUTPUT_CLAMP_FLOOR)
* stays byte-identical. */
export function clampOutputBudget(requested: number, inputEstimate: number, nativeWindow: number): number | undefined {
const margin = Math.max(OUTPUT_CLAMP_MIN_MARGIN, Math.ceil(inputEstimate * OUTPUT_CLAMP_MARGIN_PCT));
// #2011: max_tokens / max_completion_tokens / max_output_tokens are integer-typed on every
Expand All @@ -253,7 +262,12 @@ export function clampOutputBudget(requested: number, inputEstimate: number, nati
// headroom, so input+output <= window still holds).
const cap = Math.floor(nativeWindow - inputEstimate - margin);
if (cap < OUTPUT_CLAMP_FLOOR || cap >= requested) return undefined;
return cap;
// #3071: cap >= OUTPUT_CLAMP_FLOOR here. Never reduce below the capability
// floor, bounded by raw headroom so input+output <= window still holds.
const floorOut = Math.min(OUTPUT_CLAMP_MIN_OUTPUT, Math.floor(nativeWindow - inputEstimate));
const clamped = Math.max(cap, floorOut);
if (clamped >= requested) return undefined;
return clamped;
}

/** #2490: translate the refused dsh compaction replay size (recorded on the
Expand Down
13 changes: 8 additions & 5 deletions tests/issue2122-clamp-calibration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ process.env.NODE_ENV = "test";
// Fix: scale the LOCAL-EST side by the per-route estimator calibration k̂ (#1933 F1,
// learned from measured/local pairs on the same route), exactly as the preflight
// gate already does — the usage-grade baseline stays unscaled. These tests pin
// both the incident arithmetic (raw path still clamps 32768 -> 3357) and the fix
// (calibrated path leaves 32768 untouched).
// both the incident arithmetic (raw path still clamps 32768 -> 4096, the #3071
// capability floor of the naive 3357 cap) and the fix (calibrated path leaves
// 32768 untouched).

import type { CoreMessage } from "acp-kernel";
import { clampOutgoingOutput, estimateInputTokens } from "../src/server/budget.ts";
Expand All @@ -34,15 +35,17 @@ test("#2122 fixture: raw local estimate reproduces the incident's input~137237",
assert.equal(rawLocalEst(), 137_237);
});

test("#2122 repro: without calibration the raw est overrides the usage baseline and clamps 32768 -> 3357", () => {
test("#2122 repro: without calibration the raw est overrides the usage baseline and clamps 32768 -> 4096 (#3071 floor)", () => {
const body: Record<string, unknown> = { max_tokens: 32_768 };
clampOutgoingOutput(body, "max_tokens", {
systemText: "", tools: [], processedMessages: MSGS,
lastInputTokens: 81_678, lastInputTokensSource: "usage",
nativeWindow: 147_456, imageTokens: 0,
}, "t2122", () => {});
// margin = max(2048, ceil(137237*0.05)=6862); cap = floor(147456-137237-6862) = 3357
assert.equal(body.max_tokens, 3_357);
// margin = max(2048, ceil(137237*0.05)=6862); naive cap = floor(147456-137237-6862) = 3357.
// #3071 floors the emitted budget at min(4096, headroom 10219) = 4096 — the old
// 3357 pin was exactly the sub-floor starvation the capability floor removes.
assert.equal(body.max_tokens, 4_096);
});

test("#2122 fix: calibrated local side restores provider-consistent headroom — no clamp", () => {
Expand Down
7 changes: 5 additions & 2 deletions tests/issue2558-clamp-image.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ test("#2558: the pre-fix formula DID clamp the same inputs (regression teeth)",
const oldInputEstimate = Math.max(usage, 0) + imageTokens; // 226_651, matches the report's input~226655
const capped = clampOutputBudget(requested, oldInputEstimate, window);
assert.ok(capped !== undefined && capped < requested, `old formula clamps (${capped} < ${requested})`);
// margin = ceil(226651*0.05)=11333; cap = floor(240000-226651-11333)=2016
assert.equal(capped, 2_016);
// margin = ceil(226651*0.05)=11333; naive cap = floor(240000-226651-11333)=2016.
// #3071 floors the emitted budget at min(4096, headroom 13349) = 4096 — the tooth
// still bites (the double-counted estimate still starves the clamp), just no
// longer below the 4096 capability minimum.
assert.equal(capped, 4_096);
});
74 changes: 74 additions & 0 deletions tests/issue3071-planning-floor-min-output.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// #3071 path 3: the #2490 planning-floor clamp drove the outgoing output budget into
// the low thousands (observed 1479/2623) whenever input sat near the window — the 5%
// margin ate most of the raw headroom, starving the reply to an empty finish=length
// turn. Owner-signed constraint (#3071): floor the clamped budget at min(4096, window
// - input) so a normal reply always has room, WITHOUT relaxing fail-closed — the
// sub-floor surrender (cap < OUTPUT_CLAMP_FLOOR, i.e. input+margin already past the
// window) stays byte-identical, and min(4096, window-input) is always <= window-input
// so input+output can never exceed the window. The #2490 STICKY pin must stay green.
import assert from "node:assert/strict";
import test from "node:test";

process.env.NODE_ENV = "test";

import { clampOutputBudget, clampOutgoingOutput } from "../src/server/budget.ts";

const WINDOW = 262_144; // the reported DSH / ark-code-latest window

test("#3071: planning-floor clamp is floored to 4096, not the low thousands (131072 -> 2623 becomes 4096)", () => {
// Refused dsh compaction replay floors planning input at 247162 tokens; the naive
// cap is floor(262144 - 247162 - ceil(247162*0.05)) = 2623. Raw headroom is 14982,
// which comfortably fits the 4096 capability floor.
assert.equal(clampOutputBudget(131_072, 247_162, WINDOW), 4_096);
});

test("#3071: the 1479 case is floored too when the incoming budget exceeds the floor", () => {
// planning floor 248252 -> naive cap 1479. An incoming 8192 (a restored starved
// budget) is reduced to 4096, not 1479.
assert.equal(clampOutputBudget(8_192, 248_252, WINDOW), 4_096);
});

test("#3071: the clamp never RAISES — a client budget already under the floor is honored", () => {
// The exact log line "clamped 2512 -> 1479": incoming 2512 < 4096, so we do not
// reduce it to 1479 (that would lower a value the client itself chose); it passes
// through untouched. Raising belongs to the restore lane, not the clamp.
assert.equal(clampOutputBudget(2_512, 248_252, WINDOW), undefined);
});

test("#3071: sub-floor surrender is untouched (fail-closed preserved when input+margin >= window)", () => {
// Mirrors the #2490 unit s4 shape: input+margin already past the window -> no clamp.
assert.equal(clampOutputBudget(384_000, 990_000, 1_000_000), undefined);
// And the #2490 incident clamp (well above the floor) is unchanged.
assert.equal(clampOutputBudget(384_000, 900_000, 1_000_000), 55_000);
});

test("#3071: when raw headroom < 4096 the floor is bounded by headroom, not forced to 4096", () => {
// window-input = 3500 (< 4096): naive cap 1452, floor bounded down to 3500 = full
// headroom. input+output = 8500 + 3500 = 12000 = window exactly (never exceeded).
assert.equal(clampOutputBudget(8_000, 8_500, 12_000), 3_500);
});

test("#3071: a healthy cap well above the floor is unchanged", () => {
// cap 136144 >> 4096 -> the capability floor does not interfere.
assert.equal(clampOutputBudget(200_000, 120_000, WINDOW), 136_144);
});

test("#3071 (integration): clampOutgoingOutput plans against the refused ledger and floors to 4096", () => {
const lines: string[] = [];
const log = (level: string, msg: string): void => { if (level === "info" || level === "warn") lines.push(msg); };
const rebuilt: Record<string, unknown> = { max_tokens: 131_072 };
const ctx = {
systemText: "",
tools: undefined,
processedMessages: [{ role: "user", text: "hello" }],
lastInputTokens: 1_000,
lastInputTokensSource: "usage",
nativeWindow: WINDOW,
imageTokens: 0,
ledgerFloorTokens: 247_162,
} as Parameters<typeof clampOutgoingOutput>[2];
clampOutgoingOutput(rebuilt, "max_tokens", ctx, "s-3071", log);
assert.equal(rebuilt.max_tokens, 4_096, "planned against the refused raw ledger, floored to the capability minimum");
assert.match(lines[0] ?? "", /planning floor 247162 from the refused dsh compaction replay \(#2490\)/);
assert.match(lines[0] ?? "", /-> 4096/);
});
Loading