GeezerVison is showing github.com — fix DeepSeek Responses UUID item ids for Codex by mouzhi · Pull Request #940 · lidge-jun/opencodex
fix DeepSeek Responses UUID item ids for Codex
#940
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter
Filter by extension
.ts
(5)
All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
address review feedback for deepseek responses repair
Make response ids unique, mint missing item ids under rewriteNonCanonicalIds, share rewrite/trailer state through one factory, pass trailers through the Windows eager path, and add focused regression tests. Also link the PR to #938 .
Loading branch information
Uh oh!
There was an error while loading. Please reload this page.
commit 2739386382ddd5460e1e12ca5e7d578558d1b507
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -58,6 +58,8 @@ export type EagerRelayHooks = {
};
export type EagerRelayOptions = {
/** Optional trailer emitted after rewrite flush (e.g. data: [DONE]). */
trailer?: string | (() => string | undefined);
/** Bounded client queue in bytes; producer pauses above it. Default 8 MiB. */
maxQueueBytes?: number;
/** Transient-budget owner for the inline-rewrite frame buffer. */
Expand Down
Expand Up
@@ -232,6 +234,12 @@ export function relaySseEagerBounded(
queuedBytes += tail.byteLength;
try { controllerRef?.enqueue(tail); } catch { /* client already gone */ }
}
const trailer = typeof opts?.trailer === "function" ? opts.trailer() : opts?.trailer;
if (trailer && !cancelled) {
const trailerBytes = new TextEncoder().encode(trailer);
queuedBytes += trailerBytes.byteLength;
try { controllerRef?.enqueue(trailerBytes); } catch { /* client already gone */ }
}
}
if (!hooks.sawTerminal() && !cancelled && !upstream.signal.aborted) {
syntheticKind = "incomplete";
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -86,7 +86,9 @@ function mapRawId(
}
if (!rawId) {
if (!state.repairMissingTerminalIds) return null;
// When non-canonical rewrite is active, mint a request-local id even if the upstream
// omitted the terminal id, so Codex lifecycle correlation stays stable.
if (!state.repairMissingTerminalIds && !state.rewriteNonCanonicalIds) return null;
const minted = mintCanonicalId(type, state.scope, outputIndex);
state.budget?.chargeRetained(new TextEncoder().encode(JSON.stringify([outputIndex, minted])).byteLength, { kind: "item_ids" });
state.outputIds[type].set(outputIndex, minted);
Expand Down
Expand Up
@@ -151,7 +153,8 @@ function mapResponseId(state: ResponsesItemIdRepairState, rawId: string | undefi
if (rawId.startsWith("resp_")) return rawId;
const existing = state.responseIdMap.get(rawId);
if (existing) return existing;
const minted = `resp_ocx_${state.scope}`;
// Keep each distinct upstream response id unique within the request-local scope.
const minted = `resp_ocx_${state.scope}_${state.responseIdMap.size}`;
state.responseIdMap.set(rawId, minted);
Comment thread
Comment on lines
+169
to
+177
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more .
Alias rewritten response IDs in continuation state
When Codex chains a turn with previous_response_id, it sends the synthetic resp_ocx_* value minted here, but the passthrough inspection branch records the untouched upstream response under its raw UUID. The local continuation cache therefore cannot find the synthetic ID; a stateless DeepSeek route loses the earlier input expansion, while a non-stateless custom route may forward an ID the upstream has never issued. Preserve the client-visible prefix while registering an alias from the synthetic response ID to the raw completed snapshot, or otherwise keep continuation lookup synchronized with this rewrite.
Useful? React with 👍 / 👎.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
return minted;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
Expand Down
Expand Up
@@ -220,7 +223,7 @@ function rewriteOutputItem(
if (!mapped) return { item, changed: false };
const currentId = typeof item.id === "string" ? item.id : undefined;
if (currentId === mapped) return { item, changed: false };
if (currentId === undefined && !state.repairMissingTerminalIds) return { item, changed: false };
if (currentId === undefined && !state.repairMissingTerminalIds && !state.rewriteNonCanonicalIds ) return { item, changed: false };
return { item: { ...item, id: mapped }, changed: true };
}
Expand Down
Expand Up
@@ -254,7 +257,7 @@ function rewriteItemIdField(
}
if (!mapped) return { event, changed: false };
if (currentId === mapped) return { event, changed: false };
if (currentId === undefined && !state.repairMissingTerminalIds) return { event, changed: false };
if (currentId === undefined && !state.repairMissingTerminalIds && !state.rewriteNonCanonicalIds ) return { event, changed: false };
const next: Record<string, unknown> = { ...event, item_id: mapped };
if (state.rewriteNonCanonicalIds && "logprobs" in next) delete next.logprobs;
return { event: next, changed: true };
Expand Down
Expand Up
@@ -403,36 +406,45 @@ function repairEventPayload(
* rewriteNonCanonicalIds rewrites DeepSeek-style UUID item/response ids, converts
* reasoning_text streams into Codex-friendly encrypted_content reasoning items, and
* ensures a terminal [DONE] trailer so CLI/TUI turns finish.
*
* The rewrite and trailer share one request-local state object via closure so
* composition cannot lose the trailer state.
*/
export function createResponsesItemIdPayloadRewrite(
export interface ResponsesItemIdRepairHandlers {
rewrite: SsePayloadRewrite;
trailer: () => string | undefined;
}
export function createResponsesItemIdRepairHandlers(
config: ResponsesItemIdRepairConfig,
budget?: TranslatorBudget,
): SsePayloadRewrite {
): ResponsesItemIdRepairHandlers {
const state = createRepairState(config, budget);
const rewrite: SsePayloadRewrite = (payload) => repairEventPayload(payload, state);
(rewrite as SsePayloadRewrite & { __state?: ResponsesItemIdRepairState }).__state = state;
return rewrite;
return {
rewrite: (payload) => repairEventPayload(payload, state),
trailer: () => {
if (state.sawCompleted && !state.sawDoneTrailer) return "data: [DONE]\n\n";
return undefined;
},
};
}
export function createResponsesItemIdDoneTrailer(
rewrite: SsePayloadRewrite,
): () => string | undefined {
return () => {
const state = (rewrite as SsePayloadRewrite & { __state?: ResponsesItemIdRepairState }).__state;
if (!state) return undefined;
if (state.sawCompleted && !state.sawDoneTrailer) return "data: [DONE]\n\n";
return undefined;
};
/** Backward-compatible helper used by existing tests and callers. */
export function createResponsesItemIdPayloadRewrite(
config: ResponsesItemIdRepairConfig,
budget?: TranslatorBudget,
): SsePayloadRewrite {
return createResponsesItemIdRepairHandlers(config, budget).rewrite;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
export function relaySseWithResponsesItemIdRepair(
body: ReadableStream<Uint8Array>,
config: ResponsesItemIdRepairConfig,
budget: TranslatorBudget,
): ReadableStream<Uint8Array> {
const rewrite = createResponsesItemIdPayloadRewrite (config, budget);
return relaySseWithPayloadRewrite(body, rewrite, budget, {
trailer: createResponsesItemIdDoneTrailer(rewrite) ,
const handlers = createResponsesItemIdRepairHandlers (config, budget);
return relaySseWithPayloadRewrite(body, handlers. rewrite, budget, {
trailer: handlers.trailer ,
});
}
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -147,7 +147,7 @@ import { relaySseEagerBounded } from "../relay-eager";
import { isWin32EagerRewrite, selectEagerPath } from "../../lib/bun-stream-caps";
import { cancelBodyOnAbort } from "../../lib/abort";
import {
createResponsesItemIdPayloadRewrite ,
createResponsesItemIdRepairHandlers ,
hasResponsesItemIdRepair,
} from "../responses-item-id-repair";
import {
Expand Down
Expand Up
@@ -1851,12 +1851,13 @@ async function handleResponsesInner(
if (isEventStream && upstreamResponse.body) {
const repairConfig = route.provider.responsesItemIdRepair;
const needsClientRewrite = imageGenCallAliases.size > 0 || hasResponsesItemIdRepair(repairConfig);
const itemIdRepair = hasResponsesItemIdRepair(repairConfig)
? createResponsesItemIdRepairHandlers(repairConfig!, translatorBudget)
: undefined;
// Compose opt-in payload rewrites into one parse/stringify pass (image-gen restore first).
const payloadRewrites = [
createImageGenCallRestoreRewrite(imageGenCallAliases),
hasResponsesItemIdRepair(repairConfig)
? createResponsesItemIdPayloadRewrite(repairConfig!, translatorBudget)
: undefined,
itemIdRepair?.rewrite,
].filter((rewrite): rewrite is NonNullable<typeof rewrite> => rewrite !== undefined);
// #864: win32 rewrite traffic must never enter the tee()+JS-pull chain
// (Bun#32111 JS-sink segfault — text frames pass, the terminal block is
Expand Down
Expand Up
@@ -1920,7 +1921,12 @@ async function handleResponsesInner(
},
onClientCancel: () => options.onNativePassthroughCancel?.(),
onDone: () => unregisterTurn(turnAc),
}, win32EagerRewrite ? { rewriteBudget: translatorBudget } : undefined);
}, win32EagerRewrite
? {
rewriteBudget: translatorBudget,
...(itemIdRepair ? { trailer: itemIdRepair.trailer } : {}),
}
: undefined);
// selectEagerPath admits only no-rewrite traffic on both eligible platforms;
// win32 rewrite traffic reaches this relay too, but with the payload rewrite
// applied inline — never via an image/item-id JS pull wrapper (#32111, #864).
Expand Down
Expand Up
@@ -1994,7 +2000,12 @@ async function handleResponsesInner(
// relay is established practice (relayWithAbort, relaySseWithHeartbeat) and lets a
// mid-stream reset end with a clean response.failed terminal instead of a raw socket error.
const rewrittenBody = payloadRewrites.length > 0
? relaySseWithPayloadRewrite(nativeBody, composeSsePayloadRewrites(...payloadRewrites), translatorBudget)
? relaySseWithPayloadRewrite(
nativeBody,
composeSsePayloadRewrites(...payloadRewrites),
translatorBudget,
itemIdRepair ? { trailer: itemIdRepair.trailer } : undefined,
)
: nativeBody;
const clientBody = process.platform === "win32" && !needsClientRewrite
? nativeBody
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -141,7 +141,7 @@ export function relaySseWithPayloadRewrite(
while ((next = nextSseBlock(buffer))) {
replaceBuffer(next.rest);
const payload = sseDataPayload(next.block);
if (payload) {
if (payload !== null ) {
const rewrittenPayload = rewrite(payload);
if (rewrittenPayload === null) continue;
const block = rewrittenPayload !== payload
Expand All
@@ -156,7 +156,7 @@ export function relaySseWithPayloadRewrite(
}
if (flushFinal && buffer.length > 0) {
const payload = sseDataPayload(buffer);
if (payload) {
if (payload !== null ) {
const rewrittenPayload = rewrite(payload);
if (rewrittenPayload !== null) {
const block = rewrittenPayload !== payload
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -83,12 +83,53 @@ describe("DeepSeek Responses item-id repair", () => {
expect(reasoningAdded.id).toMatch(/^rs_ocx_[0-9a-f]+_0$/);
expect(messageAdded.id).toMatch(/^msg_ocx_[0-9a-f]+_1$/);
expect(textDelta.item_id).toBe(messageAdded.id);
expect(completed.id).toMatch(/^resp_ocx_[0-9a-f]+$/);
expect(completed.id).toMatch(/^resp_ocx_[0-9a-f]+_\d+ $/);
expect(completed.output[0].id).toBe(reasoningAdded.id);
expect(String(completed.output[0].encrypted_content)).toMatch(/^ocxr1:/);
expect(completed.output[1].id).toBe(messageAdded.id);
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
test("mints unique response ids for distinct upstream response ids", async () => {
const upstream = [
`event: response.completed\ndata: {"type":"response.completed","response":{"id":"11111111-1111-1111-1111-111111111111","status":"completed","output":[]}}\n\n`,
`event: response.completed\ndata: {"type":"response.completed","response":{"id":"22222222-2222-2222-2222-222222222222","status":"completed","output":[]}}\n\n`,
].join("");
const events = await parseSse(await readAll(relaySseWithResponsesItemIdRepair(streamFromText(upstream), {
rewriteNonCanonicalIds: true,
})));
const first = (events[0].response as { id: string }).id;
const second = (events[1].response as { id: string }).id;
expect(first).toMatch(/^resp_ocx_[0-9a-f]+_0$/);
expect(second).toMatch(/^resp_ocx_[0-9a-f]+_1$/);
expect(first).not.toBe(second);
});
test("mints reasoning ids when rewriteNonCanonicalIds is enabled without repairMissingTerminalIds", async () => {
const upstream = [
`event: response.output_item.added\ndata: {"type":"response.output_item.added","output_index":0,"item":{"type":"reasoning","summary":[]}}\n\n`,
`event: response.output_item.done\ndata: {"type":"response.output_item.done","output_index":0,"item":{"type":"reasoning","summary":[]}}\n\n`,
].join("");
const events = await parseSse(await readAll(relaySseWithResponsesItemIdRepair(streamFromText(upstream), {
rewriteNonCanonicalIds: true,
})));
const added = events[0].item as Record<string, unknown>;
const done = events[1].item as Record<string, unknown>;
expect(added.id).toMatch(/^rs_ocx_[0-9a-f]+_0$/);
expect(done.id).toBe(added.id);
});
test("does not duplicate an upstream [DONE] trailer", async () => {
const upstream = [
`event: response.completed\ndata: {"type":"response.completed","response":{"id":"58786d76-18be-43d9-b6f5-8922796fbe28","status":"completed","output":[]}}\n\n`,
`data: [DONE]\n\n`,
].join("");
const repaired = await readAll(relaySseWithResponsesItemIdRepair(streamFromText(upstream), {
rewriteNonCanonicalIds: true,
}));
expect(repaired.match(/data: \[DONE\]/g)).toHaveLength(1);
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
test("opt-in flag is recognized", () => {
expect(hasResponsesItemIdRepair({ rewriteNonCanonicalIds: true })).toBe(true);
});
Expand Down
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.