Skip to content

Commit b6d3250

Browse files
lidge-junWZBbiao
andcommitted
fix(responses): relay Darwin rewrites eagerly
Reimplement the explicit Darwin config-eager predicate from PR lidge-jun#947 for issue lidge-jun#1127, and apply client rewrites with their budget inside the selected single-reader relay. This was rebuilt instead of cherry-picked because PR lidge-jun#947 conflicts with the current stacked transport code and its fixed settle waits did not deterministically prove relay cleanup. Co-authored-by: biao <[email protected]>
1 parent 58500d2 commit b6d3250

5 files changed

Lines changed: 106 additions & 48 deletions

File tree

‎src/lib/bun-stream-caps.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export function decideEagerRelay(
9494
* Windows preserves the decision for no-rewrite traffic. Darwin permits only
9595
* explicit config opt-in; `auto` remains tee even on a future fixed runtime.
9696
* Returns the normalized effective decision, or null when platform policy,
97-
* rewrite needs, or a Darwin non-config-eager mode selects tee.
97+
* Windows rewrite needs, or a Darwin non-config-eager mode selects tee.
9898
*/
9999
export function selectEagerPath(
100100
platform: NodeJS.Platform,
@@ -103,12 +103,12 @@ export function selectEagerPath(
103103
version: string = Bun.version,
104104
minFixed: string | null = MIN_FIXED_BUN_VERSION,
105105
): EagerRelayDecision | null {
106-
if (needsClientRewrite || (platform !== "win32" && platform !== "darwin")) {
106+
if (platform !== "win32" && platform !== "darwin") {
107107
return null;
108108
}
109109

110110
const decision = decideEagerRelay(mode, version, minFixed);
111-
if (platform === "win32") return decision;
111+
if (platform === "win32") return needsClientRewrite ? null : decision;
112112
return decision.reason === "config-eager" ? decision : null;
113113
}
114114

‎src/server/responses/core.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,6 +2135,8 @@ async function handleResponsesInner(
21352135
needsClientRewrite,
21362136
config.streamMode ?? "auto",
21372137
);
2138+
const inlineEagerRewrite = needsClientRewrite
2139+
&& (win32EagerRewrite || eagerPath?.useEagerRelay === true);
21382140
if (eagerPath?.useEagerRelay || win32EagerRewrite) {
21392141
const turnAc = new AbortController();
21402142
linkAbortSignal(upstream, turnAc.signal);
@@ -2191,11 +2193,11 @@ async function handleResponsesInner(
21912193
},
21922194
onClientCancel: () => options.onNativePassthroughCancel?.(),
21932195
onDone: () => unregisterTurn(turnAc),
2194-
}, win32EagerRewrite ? { rewriteBudget: translatorBudget } : undefined);
2196+
}, inlineEagerRewrite ? { rewriteBudget: translatorBudget } : undefined);
21952197
// When selected, this relay closes response.completed even if upstream
2196-
// keeps the connection alive. Windows rewrite traffic applies its
2197-
// payload transform inline — never via the Bun#32111-unsafe
2198-
// tee()+JS-pull chain (#864).
2198+
// keeps the connection alive. Windows forced-rewrite traffic and Darwin
2199+
// explicit eager traffic apply client rewrites inline rather than via
2200+
// the tee()+JS-pull chain.
21992201
if (!headers.has("content-type")) headers.set("content-type", "text/event-stream");
22002202
return markEagerRelaySseResponse(
22012203
markNativePassthroughSseResponse(new Response(eagerBody, {

‎tests/bun-stream-caps.test.ts‎

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -98,44 +98,40 @@ describe("decideEagerRelay (activation scenarios)", () => {
9898
});
9999

100100
describe("selectEagerPath (platform policy matrix)", () => {
101-
test("win32 + no rewrite + config-eager → eager", () => {
102-
expect(selectEagerPath("win32", false, "eager-relay", "1.3.14", null))
103-
.toEqual({ useEagerRelay: true, reason: "config-eager" });
104-
});
105-
106-
test("win32 + no rewrite + auto known-bad → tee", () => {
107-
expect(selectEagerPath("win32", false, "auto", "1.3.14", null))
108-
.toEqual({ useEagerRelay: false, reason: "auto-known-bad" });
109-
});
110-
111-
test("win32 + no rewrite + auto fixed runtime → eager", () => {
112-
expect(selectEagerPath("win32", false, "auto", "1.4.0", "1.4.0"))
113-
.toEqual({ useEagerRelay: true, reason: "auto-fixed-runtime" });
114-
});
115-
116-
test("darwin + no rewrite + config-eager → eager", () => {
117-
expect(selectEagerPath("darwin", false, "eager-relay", "1.3.14", null))
118-
.toEqual({ useEagerRelay: true, reason: "config-eager" });
119-
});
120-
121-
test("darwin + no rewrite + auto fixed runtime → tee with no eager decision", () => {
122-
expect(selectEagerPath("darwin", false, "auto", "1.4.0", "1.4.0")).toBeNull();
123-
});
124-
125-
test("darwin + rewrite + config-eager → tee", () => {
126-
expect(selectEagerPath("darwin", true, "eager-relay", "1.3.14", null)).toBeNull();
127-
});
128-
129-
test("linux + config-eager → tee", () => {
130-
expect(selectEagerPath("linux", false, "eager-relay", "1.3.14", null)).toBeNull();
131-
});
132-
133-
test("legacy-tee is a Windows decision and null on ineligible platforms", () => {
134-
expect(selectEagerPath("win32", false, "legacy-tee", "9.9.9", "1.4.0"))
135-
.toEqual({ useEagerRelay: false, reason: "config-legacy" });
136-
expect(selectEagerPath("darwin", false, "legacy-tee", "9.9.9", "1.4.0")).toBeNull();
137-
expect(selectEagerPath("linux", false, "legacy-tee", "9.9.9", "1.4.0")).toBeNull();
138-
});
101+
const configLegacy = { useEagerRelay: false, reason: "config-legacy" } as const;
102+
const configEager = { useEagerRelay: true, reason: "config-eager" } as const;
103+
const autoFixed = { useEagerRelay: true, reason: "auto-fixed-runtime" } as const;
104+
const cases: Array<{
105+
platform: NodeJS.Platform;
106+
mode: "auto" | "legacy-tee" | "eager-relay";
107+
rewrite: boolean;
108+
expected: typeof configLegacy | typeof configEager | typeof autoFixed | null;
109+
}> = [
110+
{ platform: "win32", mode: "legacy-tee", rewrite: false, expected: configLegacy },
111+
{ platform: "win32", mode: "eager-relay", rewrite: false, expected: configEager },
112+
{ platform: "win32", mode: "auto", rewrite: false, expected: autoFixed },
113+
{ platform: "win32", mode: "legacy-tee", rewrite: true, expected: null },
114+
{ platform: "win32", mode: "eager-relay", rewrite: true, expected: null },
115+
{ platform: "win32", mode: "auto", rewrite: true, expected: null },
116+
{ platform: "darwin", mode: "legacy-tee", rewrite: false, expected: null },
117+
{ platform: "darwin", mode: "eager-relay", rewrite: false, expected: configEager },
118+
{ platform: "darwin", mode: "auto", rewrite: false, expected: null },
119+
{ platform: "darwin", mode: "legacy-tee", rewrite: true, expected: null },
120+
{ platform: "darwin", mode: "eager-relay", rewrite: true, expected: configEager },
121+
{ platform: "darwin", mode: "auto", rewrite: true, expected: null },
122+
{ platform: "linux", mode: "legacy-tee", rewrite: false, expected: null },
123+
{ platform: "linux", mode: "eager-relay", rewrite: false, expected: null },
124+
{ platform: "linux", mode: "auto", rewrite: false, expected: null },
125+
{ platform: "linux", mode: "legacy-tee", rewrite: true, expected: null },
126+
{ platform: "linux", mode: "eager-relay", rewrite: true, expected: null },
127+
{ platform: "linux", mode: "auto", rewrite: true, expected: null },
128+
];
129+
130+
for (const { platform, mode, rewrite, expected } of cases) {
131+
test(`${platform} + ${mode} + rewrite=${rewrite}`, () => {
132+
expect(selectEagerPath(platform, rewrite, mode, "1.4.0", "1.4.0")).toEqual(expected);
133+
});
134+
}
139135
});
140136

141137
describe("isStreamMode", () => {

‎tests/relay-eager.test.ts‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,18 +270,34 @@ describe("relaySseEagerBounded — inline payload rewrite (#864)", () => {
270270
const budget = createTranslatorBudget();
271271
const up = controlledUpstream();
272272
const ac = new AbortController();
273-
const { hooks } = makeHooks();
273+
const { hooks, rec } = makeHooks();
274+
let resolveDone!: () => void;
275+
const done = new Promise<void>(resolve => { resolveDone = resolve; });
276+
const previousOnDone = hooks.onDone;
277+
hooks.onDone = () => {
278+
previousOnDone();
279+
resolveDone();
280+
};
274281
hooks.rewritePayload = (payload: string) => payload;
275282
relaySseEagerBounded(up.stream, ac, hooks, { rewriteBudget: budget });
276283

277284
up.push(enc.encode(`data: {"type":"unterminated"`));
278-
await settle();
279285
// The shared terminal boundary now owns incomplete SSE framing, so the
280286
// downstream rewrite stage never retains an unterminated block.
281287
expect(budget.snapshot().currentBytes).toBe(0);
282288
ac.abort(new Error("test abort"));
283-
await settle();
289+
let timeout: ReturnType<typeof setTimeout> | undefined;
290+
await Promise.race([
291+
done,
292+
new Promise<never>((_, reject) => {
293+
timeout = setTimeout(() => reject(new Error("relay cleanup timed out")), 2_000);
294+
}),
295+
]).finally(() => {
296+
if (timeout) clearTimeout(timeout);
297+
});
284298
expect(budget.snapshot().currentBytes).toBe(0);
299+
expect(rec.dones).toBe(1);
300+
budget.dispose();
285301
});
286302

287303
test("blocks without a data field pass through untouched before the terminal", async () => {

‎tests/responses-snapshot-repair-server.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { tmpdir } from "node:os";
44
import { join } from "node:path";
55
import { saveConfig } from "../src/config";
66
import { startServer } from "../src/server";
7+
import { handleResponses } from "../src/server/responses";
8+
import { isEagerRelaySseResponse } from "../src/server/relay";
79
import type { OcxConfig } from "../src/types";
810
import { installIsolatedCodexHome, type IsolatedCodexHome } from "./helpers/isolated-codex-home";
911

@@ -63,6 +65,48 @@ afterEach(async () => {
6365
});
6466

6567
describe("responsesSnapshotRepair through /v1/responses", () => {
68+
test.skipIf(process.platform !== "darwin")(
69+
"Darwin eager-relay applies snapshot repair inline before bytes reach the client",
70+
async () => {
71+
const gateway = "https://sparse-darwin-eager.example.test";
72+
stubSparseGateway(gateway);
73+
const config = {
74+
port: 0,
75+
streamMode: "eager-relay",
76+
defaultProvider: "sparse",
77+
providers: {
78+
sparse: {
79+
adapter: "openai-responses",
80+
baseUrl: `${gateway}/v1`,
81+
authMode: "key",
82+
apiKey: "test-key",
83+
responsesSnapshotRepair: true,
84+
},
85+
},
86+
} as OcxConfig;
87+
88+
const response = await handleResponses(
89+
new Request("http://localhost/v1/responses", {
90+
method: "POST",
91+
headers: { "content-type": "application/json" },
92+
body: JSON.stringify({ model: "sparse-model", input: "hi", stream: true }),
93+
}),
94+
config,
95+
{ model: "", provider: "" },
96+
);
97+
98+
expect(isEagerRelaySseResponse(response)).toBe(true);
99+
const text = await response.text();
100+
expect(text).toContain("response.content_part.added");
101+
expect(text).toContain("response.output_text.done");
102+
expect(text).toContain("response.output_item.done");
103+
const completedLine = text.split("\n").find(line => line.includes('"response.completed"'));
104+
expect(completedLine).toBeDefined();
105+
const completed = JSON.parse(completedLine!.replace(/^data: /, "")) as { response: { output: { id: string }[] } };
106+
expect(completed.response.output[0]?.id).toBe("msg_sparse");
107+
},
108+
);
109+
66110
test("an opt-in gateway's sparse stream reaches the client as the full canonical lifecycle", async () => {
67111
const gateway = "https://sparse.example.test";
68112
stubSparseGateway(gateway);

0 commit comments

Comments
 (0)