Skip to content
Closed
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
7 changes: 5 additions & 2 deletions src/server/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2151,12 +2151,15 @@ async function handleRequest(
// (nothing foldable) returns null and forward() passes the
// original 400 through verbatim.
const overflowRefold = pendingForward.prepared
? async (realWindow: number | undefined): Promise<string | Buffer | null> => {
? async (realWindow: number | undefined): Promise<{ body: string | Buffer; prepared: Prepared | null } | null> => {
const next = await withSessionLock(session, () => runPreparedPipeline(false, realWindow));
if (!next || String(next.body) === String(pendingForward.body)) return null;
pendingForward.body = next.body;
pendingForward.prepared = next.prepared;
return next.body;
// #2777: hand back the refold pipeline's Prepared as well —
// forward() must adopt it locally or its internal loop keeps
// the pre-refold message view for tool continuations.
return { body: next.body, prepared: next.prepared };
}
: undefined;
await forward(req, res, opts, pendingForward.body, pendingForward.prepared, core, reqConfig, log, route, instanceId, affinity, overflowRefold);
Expand Down
9 changes: 7 additions & 2 deletions src/server/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export async function forward(
route: ReturnType<typeof resolveUpstream>,
instanceId: string,
affinity?: string,
overflowRefold?: (realWindow: number | undefined) => Promise<string | Buffer | null>,
overflowRefold?: (realWindow: number | undefined) => Promise<{ body: string | Buffer; prepared: Prepared | null } | null>,
): Promise<void> {
const forwardStartedAt = Date.now();
// E2: a codex native-compaction request intercepted in prepare() carries a
Expand Down Expand Up @@ -745,12 +745,17 @@ export async function forward(
// #816 family: the refold rebuilds the body from state
// summaries — scrub string bodies again so a lone
// surrogate can't poison the retry (see the #1884 seam).
const wireRefolded = typeof refolded === "string" ? scrubLoneSurrogatesOnWire(refolded) : refolded;
const wireRefolded = typeof refolded.body === "string" ? scrubLoneSurrogatesOnWire(refolded.body) : refolded.body;
applyResign(headers, wireRefolded);
const retried = await fetchWithTimeout(upstreamUrl, { ...init, body: wireRefolded }, undefined, clientAbort.signal);
if (retried.response.ok) {
upstreamResult.clearTimer();
wireBody = wireRefolded; // #1900: track the accepted re-send as the wire base
// #2777: adopt the refold's Prepared too — this local binding
// (not pendingForward) seeds the internal loop + refreshFolded;
// the stale pre-refold view would rebuild the oversized message
// set on any post-acceptance tool continuation.
if (refolded.prepared) prepared = refolded.prepared;
upstreamResult = retried;
log("info", `[${prepared.session.id}] context overflow — refolded and re-sent within the same request, upstream accepted (#1195)`);
} else {
Expand Down
62 changes: 62 additions & 0 deletions tests/issue1195-overflow-refold.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,68 @@ test("e2e #1195 A: stated-window overflow is rescued within the SAME request —
assert.equal(s!.stats.lastInputTokensSource, "usage");
});

// #2777: the model answers the accepted folded retry with a compress call whose
// refs do not exist (the stale pre-refold view's numbering). The proxy executes
// it (FAILED), skips the post-compress fold refresh, and must rebuild the
// internal continuation from the ACCEPTED folded view — not from the stale
// Prepared captured before the refold.
function compressInvalidRefsSse(): string {
const args = JSON.stringify({ startId: "m00042", endId: "m00099", summary: "stale-view compress attempt" });
return (
`event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "m2", role: "assistant", usage: { input_tokens: 5000 } } })}\n\n` +
`event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "tool_use", id: "toolu_refold_d1", name: "compress", input: {} } })}\n\n` +
`event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "input_json_delta", partial_json: args } })}\n\n` +
`event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n` +
`event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "tool_use", stop_sequence: null }, usage: { output_tokens: 3 } })}\n\n` +
`event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`
);
}

test("e2e #2777: an internal tool continuation after an ACCEPTED overflow refold rebuilds from the accepted folded view, not the stale pre-refold Prepared", async (t) => {
const upstream = await recordedUpstream((call, res) => {
if (call === 0) {
res.writeHead(400, { "content-type": "application/json" });
res.end(STATED_OVERFLOW_BODY_128K);
} else if (call === 1) {
res.writeHead(200, { "content-type": "text/event-stream" });
res.end(compressInvalidRefsSse());
} else {
res.writeHead(200, { "content-type": "text/event-stream" });
res.end(okSse());
}
});

_setStoreForTest(new SessionStore({ enabled: false }));
setRegistryForTest({});
const proxy = await startServer(proxyBaseOptions(upstream.port, 400_000));
await once(proxy, "listening");
const proxyPort = (proxy.address() as { port: number }).port;
t.after(async () => {
proxy.close();
await once(proxy, "close");
upstream.server.close();
await once(upstream.server, "close");
});

const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstream.port}/v1/messages`;
const body = JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: true, messages: bigConversation() });
const headers = { "content-type": "application/json", "x-acp-session": "refold-d" };

const r1 = await fetch(url, { method: "POST", headers, body });
assert.equal(r1.status, 200, "the rescued turn completes despite the failed compress");
const r1text = await r1.text();
assert.ok(r1text.includes("text_delta"), "the continuation's completion reaches the client");

// bodies: [0] original 400'd forward, [1] summarization call, [2] accepted
// folded retry, [3] the internal continuation after the failed compress.
assert.equal(upstream.bodies.length, 4, `one overflow + one summary + one retry + one continuation: ${upstream.bodies.length}`);
assert.ok(upstream.bodies[2].includes(SUMMARY_TEXT), "the accepted retry carried the fold");
const cont = upstream.bodies[3]!;
assert.ok(cont.includes(SUMMARY_TEXT), "#2777: the continuation retains the accepted folded summary");
assert.ok(cont.includes("MARKER_11_"), "the continuation keeps the recent tail");
assert.ok(!cont.includes("MARKER_1_"), "#2777: the continuation must not resurrect the folded early range from the stale Prepared");
});

test("e2e #1195 B: an unfoldable payload passes the ORIGINAL overflow 400 through verbatim — the refold is not a client-visible change", async (t) => {
const upstream = await recordedUpstream((_call, res) => {
res.writeHead(400, { "content-type": "application/json" });
Expand Down
Loading