Skip to content

Commit 7ff6549

Browse files
committed
fix(proxy): address review P1/P2 — fresh Request per connect retry, unconditional Anthropic upstream on /v1/responses
- handler.ts: rebuild upstreamReq inside the connect-retry loop so every attempt fetches with a fresh body stream (a reused Request is marked bodyUsed after the first fetch, so attempts 2+ throw 'Request body already used' before any I/O — dead retry exactly on start-plan, which hits the plain pass-through path where dispatch does not rebuild the Request). Skip retries once the client signal is aborted. - responses-handler.ts: always translate Responses -> Chat -> Anthropic for both plans. Previously start-plan built an OpenAI Chat body while buildUpstreamURL now returns the live Anthropic messages endpoint, which would post an OpenAI-format body to an Anthropic endpoint. startPlan body transform (system blocks / cache_control) is preserved. Review: TriDefender request-changes on PR TriDefender#34 (P1, P2). Nits P3/P4 (tests for the new resilience behaviors, README routing sync) follow in a separate commit.
1 parent b05f82d commit 7ff6549

2 files changed

Lines changed: 17 additions & 12 deletions

File tree

‎src/proxy/handler.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,15 @@ export async function proxyRequest(
230230
// connect") happen a few times a day against the gateway. Retry the
231231
// CONNECT twice with a short backoff before surfacing a 502 — the
232232
// request never reached upstream, so resending is side-effect-free.
233+
// Guard rails (review P1/P2 + nits): skip retry when the client already
234+
// aborted, and re-dispatch a FRESH Request each attempt — a reused
235+
// Request has its body stream marked used after the first fetch, so
236+
// attempt 2+ would throw "Request body already used" before any I/O
237+
// (start-plan hits the plain pass-through path where dispatch does NOT
238+
// rebuild the Request).
233239
const maxConnectAttempts = 3;
234240
for (let attempt = 1; ; attempt++) {
241+
if (clientReq.signal.aborted) throw new Error("client aborted before upstream connect");
235242
try {
236243
upstreamResp = await dispatch(upstreamReq, upstreamHeaderPairs);
237244
break;
@@ -241,6 +248,10 @@ export async function proxyRequest(
241248
if (debug) debugError(reqId, "upstream_connect_retry", `attempt ${attempt}/${maxConnectAttempts - 1} failed (${(err as Error).message}), retrying in ${backoffMs}ms`);
242249
console.log(`${reqId} upstream connect failed (${(err as Error).message}), retry ${attempt + 1}/${maxConnectAttempts} in ${backoffMs}ms`);
243250
await new Promise((r) => setTimeout(r, backoffMs));
251+
// Rebuild the Request so the body stream is fresh for the next
252+
// dispatch (mirrors the captcha retry, which already constructs a
253+
// new Request per attempt).
254+
upstreamReq = buildUpstreamRequest(clientReq, upstreamFormat, provider, cred, transformedBody, config.identity, config.plan, captchaHeaders, clientSession);
244255
}
245256
}
246257
} catch (err) {

‎src/proxy/responses-handler.ts‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ export async function handleResponses(
144144
const providerDef = resolveProviderDef(opts.config);
145145

146146
// ── 5. body transform (start-plan system / anthropic cache_control + user_id) ──
147+
// Both plans post Anthropic upstream (mirrors handler.ts): the start-plan
148+
// OpenAI gateway was retired server-side (404 as of 2026-08-28), so the
149+
// Responses → Chat → Anthropic translator chain runs unconditionally.
147150
const startPlan = opts.config.plan === "start-plan";
148-
const upstreamFormat: "openai" | "anthropic" = startPlan ? "openai" : "anthropic";
151+
const upstreamFormat: "openai" | "anthropic" = "anthropic";
149152
let upstreamRequestBody: string;
150-
if (upstreamFormat === "anthropic") {
151-
// v2.3: coding-plan upstream is Anthropic (mirrors the real client).
152-
// Chain the existing translators: Responses → Chat → Anthropic request;
153-
// Anthropic → Chat → Responses on the way back.
153+
{
154154
let anthropicReq: AnthropicMessagesRequest;
155155
try {
156156
anthropicReq = translateRequestOpenAIToAnthropic(chatRequest);
@@ -160,14 +160,8 @@ export async function handleResponses(
160160
upstreamRequestBody = transformRequestBody(JSON.stringify(anthropicReq), {
161161
format: "anthropic",
162162
userId: cred.userId,
163-
startPlan: false,
164-
}) ?? JSON.stringify(anthropicReq);
165-
} else {
166-
upstreamRequestBody = transformRequestBody(JSON.stringify(chatRequest), {
167-
format: "openai",
168-
userId: undefined,
169163
startPlan,
170-
}) ?? JSON.stringify(chatRequest);
164+
}) ?? JSON.stringify(anthropicReq);
171165
}
172166
const transformedBody = upstreamRequestBody;
173167

0 commit comments

Comments
 (0)