Skip to content
27 changes: 19 additions & 8 deletions src/compress-loop-anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { isProxyToolFor } from "./absorb.js";
import { withSessionLock } from "./session.js";
import { drainPendingRetrievals } from "./store.js";
import { fetchWithRetry, UpstreamHttpError } from "./fetch-util.js";
import { noteUpstreamThrottle429, noteUpstreamThrottleResolved } from "./upstream-throttle.js";
import { proxyDispatcher } from "./upstream-proxy.js";
import { COMPRESS_TOOL_NAME } from "./compress-tool.js";
import { applyConfiguredCompression } from "./external-summary-compress.js";
Expand Down Expand Up @@ -147,15 +148,25 @@ export async function compressLoopAnthropicJson(
if (requestOptions.egress) roundBody = await requestOptions.egress(roundBody);
requestOptions.resign?.(requestOptions.headers, roundBody);
log(`[acp-proxy: non-stream anthropic proxy-tool round ${round}; re-requesting so the model sees the result]`);
const result = await fetchWithRetry(
requestOptions.url,
{ method: "POST", headers: requestOptions.headers, body: roundBody, ...(options.proxyUrl ? { dispatcher: proxyDispatcher(options.proxyUrl) } : {}) },
undefined,
options.signal,
).catch((e) => {
if (e instanceof UpstreamHttpError) log(`[acp-proxy: non-stream anthropic loop upstream error ${e.status}: ${e.body.slice(0, 200)}]`);
let result: Awaited<ReturnType<typeof fetchWithRetry>>;
try {
result = await fetchWithRetry(
requestOptions.url,
{ method: "POST", headers: requestOptions.headers, body: roundBody, ...(options.proxyUrl ? { dispatcher: proxyDispatcher(options.proxyUrl) } : {}) },
undefined,
options.signal,
);
// #4743: the non-stream Anthropic-wire compress loop feeds the 429 governor — a settled ok response clears any hold.
noteUpstreamThrottleResolved(requestOptions.url, requestOptions.headers, result.response.status);
} catch (e) {
if (e instanceof UpstreamHttpError) {
// #4743: a terminal 429 exhausted the retry budget — arm the governor; any other settled status clears it (relay parity, #4204).
if (e.status === 429) noteUpstreamThrottle429(requestOptions.url, requestOptions.headers, null);
else noteUpstreamThrottleResolved(requestOptions.url, requestOptions.headers, e.status);
log(`[acp-proxy: non-stream anthropic loop upstream error ${e.status}: ${e.body.slice(0, 200)}]`);
}
throw e;
});
}
try {
current = (await result.response.json()) as Record<string, unknown>;
} finally {
Expand Down
35 changes: 22 additions & 13 deletions src/compress-loop-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { type ResponseInputItem } from "acp-kernel/wire";
import { MAX_LOOP_ROUNDS } from "./loop/index.js";
import { stripResponsesText } from "./loop/tag-echo-filter.js";
import { fetchWithRetry, UpstreamHttpError } from "./fetch-util.js";
import { noteUpstreamThrottle429, noteUpstreamThrottleResolved } from "./upstream-throttle.js";
import { proxyDispatcher } from "./upstream-proxy.js";
import { safePrefix, safeSuffix } from "./text-safe.js";

Expand Down Expand Up @@ -222,24 +223,32 @@ export async function compressLoopResponsesJson(
// signature covers the shipped bytes.
if (requestOptions.egress) roundBody = await requestOptions.egress(roundBody);
requestOptions.resign?.(requestOptions.headers, roundBody);
const result = await fetchWithRetry(requestOptions.url, {
method: "POST",
headers: requestOptions.headers,
body: roundBody,
...(ctx.proxyUrl ? { dispatcher: proxyDispatcher(ctx.proxyUrl) } : {}),
}, undefined, undefined, (info) => {
// #189: correlate the rejection with the rewrite that preceded it.
const lc = lastCompressSuffix(ctx.session.lastCompress);
ctx.log(`[acp-proxy: responses upstream rejected replay (HTTP ${info.status}: ${info.detail.slice(0, 120)}); likely provider risk-control — retrying in ${info.delayMs}ms (attempt ${info.attempt}/${info.maxAttempts})${lc}]`);
loggerLog("warn", `[acp-compress-responses] upstream rejected replay (HTTP ${info.status}); retrying in ${info.delayMs}ms (attempt ${info.attempt}/${info.maxAttempts})${lc}`);
}).catch((e) => {
let result: Awaited<ReturnType<typeof fetchWithRetry>>;
try {
result = await fetchWithRetry(requestOptions.url, {
method: "POST",
headers: requestOptions.headers,
body: roundBody,
...(ctx.proxyUrl ? { dispatcher: proxyDispatcher(ctx.proxyUrl) } : {}),
}, undefined, undefined, (info) => {
// #189: correlate the rejection with the rewrite that preceded it.
const lc = lastCompressSuffix(ctx.session.lastCompress);
ctx.log(`[acp-proxy: responses upstream rejected replay (HTTP ${info.status}: ${info.detail.slice(0, 120)}); likely provider risk-control — retrying in ${info.delayMs}ms (attempt ${info.attempt}/${info.maxAttempts})${lc}]`);
loggerLog("warn", `[acp-compress-responses] upstream rejected replay (HTTP ${info.status}); retrying in ${info.delayMs}ms (attempt ${info.attempt}/${info.maxAttempts})${lc}`);
});
// #4743: the Responses-wire compress loop feeds the 429 governor — a settled ok response clears any hold.
noteUpstreamThrottleResolved(requestOptions.url, requestOptions.headers, result.response.status);
} catch (e) {
if (e instanceof UpstreamHttpError) {
// #4743: a terminal 429 exhausted the retry budget — arm the governor for the next client turn; any other settled status proves the endpoint is processing again — clear it (relay parity, #4204).
if (e.status === 429) noteUpstreamThrottle429(requestOptions.url, requestOptions.headers, null);
else noteUpstreamThrottleResolved(requestOptions.url, requestOptions.headers, e.status);
const suffix = e.attempts > 1 ? ` after ${e.attempts} attempt(s)` : "";
ctx.log(`[acp-proxy: responses compress loop upstream error ${e.status}${suffix}: ${e.body.slice(0, 200)}]`);
loggerLog("error", `[acp-compress-responses] upstream error ${e.status}${suffix}: ${e.body.slice(0, 200)}`);
loggerLog("error", `[acp-compress-responses] upstream error ${e.status}${suffix}: ${e.body.slice(0, 200)}]`);
}
throw e;
});
}
try {
current = await result.response.json() as Record<string, unknown>;
} finally {
Expand Down
3 changes: 3 additions & 0 deletions src/preflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { applyAbsorbView } from "./absorb.js";
import { adoptContentStore, ccrLoopConfig, contentStoreOf } from "./store.js";
import { applyRanges, normalizeRangeOrder, type RewriteCtx } from "./stream.js";
import { fetchWithTimeout, isTransientUpstreamError, replayMaxAttempts, replayBackoffMs, sleep, UpstreamHttpError } from "./fetch-util.js";
import { noteUpstreamThrottleResolved } from "./upstream-throttle.js";
import { dumpSummaryRejection } from "./error-dump.js";
import { proxyDispatcher } from "./upstream-proxy.js";
import { isTlsTrustCode, tlsTrustRemediation, TLS_TRUST_CODES } from "./upstream-fail.js";
Expand Down Expand Up @@ -1485,6 +1486,8 @@ async function requestSummaryBody(deps: PreflightDeps, body: string, stream: boo
deps.log("info", `[preflight] summary attempt ${attempt} headers received (${result.response.status}) after ${Date.now() - sentAt}ms`);
const text = await result.response.text();
deps.signal?.throwIfAborted();
// #4743 review fix (#4772): a settled non-429 summary response proves the endpoint processed again — clear any hold. A 429 must NOT arm here: this request's own forward runs AFTER preflight and passes the relay gate, so arming now would block our own forward of a fitting payload (#301 contract: fits + preflight 429 → still forward). The arm lives at the caller's withhold verdict (server.ts) instead.
if (result.response.status !== 429) noteUpstreamThrottleResolved(deps.url, outHeaders, result.response.status);
if (!result.response.ok) throw new UpstreamHttpError(result.response.status, text, attempt);
return text;
} catch (err) {
Expand Down
4 changes: 4 additions & 0 deletions src/reasoning-guard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { fetchWithTimeout } from "./fetch-util.js";
import { noteUpstreamThrottle429, noteUpstreamThrottleResolved } from "./upstream-throttle.js";
import { scrubLoneSurrogatesOnWire } from "./text-safe.js";
import { normalizeSseLineEndings, finalizeSseLineEndings } from "./sse-util.js";
import { awaitDrain } from "./server/stream-io.js";
Expand Down Expand Up @@ -570,6 +571,9 @@ export async function runReasoningGuard(p: GuardParams): Promise<void> {
finish(buildIncomplete(st, "upstream_error"));
return;
}
// #4743: reasoning-guard continuation rounds feed the 429 governor — a 429 arms the hold for the next client turn.
if (roundRes.response.status === 429) noteUpstreamThrottle429(p.upstreamUrl, p.reqHeaders, roundRes.response.headers.get("retry-after"));
else noteUpstreamThrottleResolved(p.upstreamUrl, p.reqHeaders, roundRes.response.status);
const contBody = roundRes.response.body;
roundRes.stopIdleTimer();
result = contBody ? await consumeRound(contBody, st, p.res) : { error: "no_body" };
Expand Down
3 changes: 3 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { codexAlignedWindow } from "./codex-models.js";
import { fetchWithTimeout, upstreamTimeoutMs } from "./fetch-util.js";
import { getUpstreamConnectionStatus, resolveProxy, resolveProxyDecision, proxyDispatcher, type UpstreamProxyDecision } from "./upstream-proxy.js";
import { buildProxyFailureResponse, isExpectedFailFast, type SkippedProxyInfo } from "./upstream-fail.js";
import { noteUpstreamThrottle429 } from "./upstream-throttle.js";
import { hostIdForLog, maskHostPortForLog, setMaskHostsEnabled, maskUrlForLog, maskUrlsInText } from "./log-mask.js";
// Protocol codecs + the historical-image strip primitive live in the kernel now
// (single source of truth shared with the omp/pi adapters): import from
Expand Down Expand Up @@ -4681,6 +4682,8 @@ export async function preflightCompressIfNeeded(
ffDetail += ` ${result.compressedRanges} range(s) folded this turn persist in the session — the next attempt starts from a smaller baseline and continues the fold${shortfall !== undefined ? ` (~${shortfall.toLocaleString("en-US")} tokens short of the target)` : ""}.`;
}
const ff = failFast(status, ffDetail, retryable, result.compressedRanges > 0 ? foldedCalibratedSize : undefined, result.rangesRemaining);
// #4743 review fix (#4772): the withhold verdict carries the 429 evidence — arm the egress hold HERE, not per-response inside the walk: the fits-case forward runs after preflight and must not be blocked by its own summary probe's 429 (#301). Nothing is forwarded on this request, so the armed hold only answers the NEXT turn's relay forward locally. No retry-after header is available at this layer — base-delay backoff applies.
if (f?.kind === "upstream" && f.status === 429) noteUpstreamThrottle429(upstreamUrl, headers, null);
// (#4079) the #726 dead-end cooldown arming was removed with the whole
// mechanism — see the block comment where the walk starts. The retry
// contract lives entirely in this answer's retryable:false/status.
Expand Down
Loading
Loading