Skip to content

Commit 486bcf2

Browse files
committed
feat(responses): guard empty completions with one identical-turn retry
Port codex-router PR #145: a 200 that completes with no output text and no tool call is a failure the client cannot see (it silently records the turn as done). Hold pre-content adapter events, suppress the terminal of an empty turn, retry the IDENTICAL request once, and surface empty_completion_retry_failed when the retry is also empty or fails upstream. Usage is merged across both attempts so the request log meters the whole turn. Kill switch: OCX_EMPTY_COMPLETION_RETRY=0 restores the previous behavior. Compaction turns and combo attempts are excluded.
1 parent 8b1c620 commit 486bcf2

3 files changed

Lines changed: 646 additions & 10 deletions

File tree

‎src/server/responses/core.ts‎

Lines changed: 78 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ import {
7777
import { buildWebSearchTool, planWebSearch, runWithWebSearch, shouldResolveOpenAiWebSearchSidecar } from "../../web-search";
7878
import { buildImageTool, buildVideoTool, planImageBridge, planVideoBridge, runWithImageBridge, clampImageMaxRounds, IMAGE_GEN_TOOL_NAME, VIDEO_GEN_TOOL_NAME } from "../../images";
7979
import { describeImagesInPlace, planVisionSidecar, resolveOpenAiVisionModel, shouldResolveOpenAiVisionSidecar, stripImagesInPlace } from "../../vision";
80-
import { createAdapterEventQueue, preflightAdapterEvents } from "../../adapters/run-turn-queue";
80+
import { createAdapterEventQueue, preflightAdapterEvents, type AdapterEventQueue } from "../../adapters/run-turn-queue";
8181
import {
8282
applyCodexAuthContextToProvider,
8383
CodexAccountCooldownError,
@@ -233,6 +233,10 @@ import { createRoutedCustomToolRestoreBlockRewrite } from "../responses-custom-t
233233
import { createGithubCopilotResponsesBlockRewrite } from "../github-copilot-responses-repair";
234234
import { responsesJsonToSseStream } from "../responses-json-events";
235235
import { guardTerminalEventStream } from "./terminal-guard";
236+
import {
237+
emptyCompletionRetryEnabled,
238+
guardEmptyCompletionEventStream,
239+
} from "./empty-completion-guard";
236240

237241
/**
238242
* Adapters whose continuation state must survive Codex's store:false requests.
@@ -3055,22 +3059,37 @@ async function handleResponsesInner(
30553059
return wsResponse;
30563060
}
30573061

3062+
// Empty-completion guard (codex-router PR #145 port): a 200 that completes with no output
3063+
// text and no tool call is a failure the client cannot see — it silently records the turn as
3064+
// done. The guard holds pre-content adapter events, suppresses the terminal of an empty
3065+
// turn, retries the IDENTICAL request once, and surfaces a stated error when the retry is
3066+
// empty or fails. Kill switch: OCX_EMPTY_COMPLETION_RETRY=0. Compaction turns and combo
3067+
// attempts keep their own machinery (the combo preflight already handles empty streams).
3068+
const emptyCompletionGuardEnabled =
3069+
emptyCompletionRetryEnabled()
3070+
&& !options.comboAttempt
3071+
&& !routedCompaction;
3072+
30583073
if (adapter.runTurn) {
30593074
const runTurnAbort = new AbortController();
30603075
linkAbortSignal(runTurnAbort, options.abortSignal);
30613076
const queue = createAdapterEventQueue({
30623077
onBacklogExceeded: () => runTurnAbort.abort(),
30633078
});
3064-
const runTurn = async (): Promise<void> => {
3079+
// One attempt of the runTurn transport, against an explicit queue. The
3080+
// empty-completion guard re-invokes the IDENTICAL turn (same parsed request,
3081+
// same forwarded headers, same abort signal) through a fresh queue, so the
3082+
// attempt body must not capture the first queue.
3083+
const runTurnAttempt = async (targetQueue: AdapterEventQueue): Promise<void> => {
30653084
try {
30663085
noteAttemptSend(logCtx.activeAttempt, logCtx.usageLogInputTokens);
30673086
await adapter.runTurn?.(
30683087
parsed,
30693088
{ headers: selectedForwardHeaders, abortSignal: runTurnAbort.signal, translatorBudget },
3070-
queue.push,
3089+
targetQueue.push,
30713090
);
30723091
} catch (err) {
3073-
queue.push({
3092+
targetQueue.push({
30743093
type: "error",
30753094
message: err instanceof Error ? err.message : String(err),
30763095
});
@@ -3080,9 +3099,20 @@ async function handleResponsesInner(
30803099
if (!logCtx.conversationId && parsed._cursorConversationId) {
30813100
logCtx.conversationId = normalizeLogConversationId(parsed._cursorConversationId);
30823101
}
3083-
queue.close();
3102+
targetQueue.close();
30843103
}
30853104
};
3105+
const runTurn = async (): Promise<void> => runTurnAttempt(queue);
3106+
// The empty-completion retry re-runs the turn against a fresh queue: the
3107+
// first queue is closed once its attempt settles, and pushing into it after
3108+
// close is a silent no-op.
3109+
const runTurnRetrySource = (): AsyncIterable<AdapterEvent> => {
3110+
const retryQueue = createAdapterEventQueue({
3111+
onBacklogExceeded: () => runTurnAbort.abort(),
3112+
});
3113+
void runTurnAttempt(retryQueue);
3114+
return retryQueue.stream();
3115+
};
30863116

30873117
const { toolNsMap, declaredToolNames, toolParameterSchemas, freeformToolNames, toolSearchToolNames } = toolBridgeMaps;
30883118
if (parsed.stream) {
@@ -3098,8 +3128,16 @@ async function handleResponsesInner(
30983128
}
30993129
eventSource = preflight.stream;
31003130
}
3131+
const guardedSource = emptyCompletionGuardEnabled
3132+
? guardEmptyCompletionEventStream({
3133+
firstEvents: eventSource,
3134+
// Identical-turn retry: same parsed request, same headers, same
3135+
// signal — run the adapter transport again against a fresh queue.
3136+
continuation: runTurnRetrySource,
3137+
})
3138+
: eventSource;
31013139
const sseStream = bridgeToResponsesSSE(
3102-
eventSource, parsed._responseModelId ?? parsed.modelId, toolNsMap, freeformToolNames, toolSearchToolNames,
3140+
guardedSource, parsed._responseModelId ?? parsed.modelId, toolNsMap, freeformToolNames, toolSearchToolNames,
31033141
() => {
31043142
runTurnAbort.abort();
31053143
queue.close();
@@ -3142,7 +3180,17 @@ async function handleResponsesInner(
31423180
}
31433181

31443182
await runTurn();
3145-
const events = await queue.collect();
3183+
const firstAttemptEvents = await queue.collect();
3184+
let events: AdapterEvent[];
3185+
if (emptyCompletionGuardEnabled) {
3186+
events = [];
3187+
for await (const event of guardEmptyCompletionEventStream({
3188+
firstEvents: (async function* () { yield* firstAttemptEvents; })(),
3189+
continuation: runTurnRetrySource,
3190+
})) events.push(event);
3191+
} else {
3192+
events = firstAttemptEvents;
3193+
}
31463194
if (options.comboAttempt) {
31473195
const firstMeaningful = events.find(event => event.type !== "heartbeat");
31483196
if (!firstMeaningful || firstMeaningful.type === "error") {
@@ -3848,9 +3896,19 @@ async function handleResponsesInner(
38483896
continuation: fetchTerminalGuardContinuation,
38493897
})
38503898
: initialEventStream;
3899+
// The empty-completion guard sits OUTSIDE the terminal guard: a completed
3900+
// turn with no text and no tool call is retried with the IDENTICAL request
3901+
// (fetchTerminalGuardContinuation(parsed) replays the cached byte-identical
3902+
// request — same body, same headers, same signal).
3903+
const guardedEventStream = emptyCompletionGuardEnabled
3904+
? guardEmptyCompletionEventStream({
3905+
firstEvents: eventStream,
3906+
continuation: () => fetchTerminalGuardContinuation(parsed),
3907+
})
3908+
: eventStream;
38513909
const { toolNsMap, declaredToolNames, toolParameterSchemas, freeformToolNames, toolSearchToolNames } = toolBridgeMaps;
38523910
const sseStream = bridgeToResponsesSSE(
3853-
eventStream, parsed._responseModelId ?? parsed.modelId, toolNsMap, freeformToolNames, toolSearchToolNames,
3911+
guardedEventStream, parsed._responseModelId ?? parsed.modelId, toolNsMap, freeformToolNames, toolSearchToolNames,
38543912
() => upstream.abort(), 2_000,
38553913
{
38563914
translatorBudget,
@@ -3895,17 +3953,27 @@ async function handleResponsesInner(
38953953
let events: AdapterEvent[];
38963954
try {
38973955
const initialEvents = await activeAdapter.parseResponse(upstreamResponse, translatorBudget);
3956+
let guardedEvents: AdapterEvent[];
38983957
if (terminalGuardEnabled) {
3899-
events = [];
3958+
guardedEvents = [];
39003959
for await (const event of guardTerminalEventStream({
39013960
parsed,
39023961
firstEvents: (async function* () { yield* initialEvents; })(),
39033962
adapterName: activeAdapter.name,
39043963
maxAutoContinuations: 1,
39053964
continuation: fetchTerminalGuardContinuation,
3965+
})) guardedEvents.push(event);
3966+
} else {
3967+
guardedEvents = initialEvents;
3968+
}
3969+
if (emptyCompletionGuardEnabled) {
3970+
events = [];
3971+
for await (const event of guardEmptyCompletionEventStream({
3972+
firstEvents: (async function* () { yield* guardedEvents; })(),
3973+
continuation: () => fetchTerminalGuardContinuation(parsed),
39063974
})) events.push(event);
39073975
} else {
3908-
events = initialEvents;
3976+
events = guardedEvents;
39093977
}
39103978
} finally {
39113979
cleanupUpstreamAbort();

0 commit comments

Comments
 (0)