Skip to content

Commit f39d38b

Browse files
fix(server): Codex question answers keep their native question id (#35886)
1 parent be03b93 commit f39d38b

2 files changed

Lines changed: 104 additions & 11 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2356,6 +2356,95 @@ describe("CodexAdapterV2 post-settle continuation", () => {
23562356
),
23572357
);
23582358

2359+
it.effect("returns a Codex question's answer under its native question id", () =>
2360+
Effect.scoped(
2361+
Effect.gen(function* () {
2362+
const nativeThreadId = "question-id-thread";
2363+
const nativeTurnId = "question-id-turn";
2364+
const transcript = makeCodexReplayTranscript({
2365+
scenario: "codex-question-native-id",
2366+
entries: [
2367+
...codexReplayPreamble({ nativeThreadId, nativeTurnId, prompt: "Pick one." }),
2368+
{
2369+
type: "emit_inbound",
2370+
label: "question",
2371+
frame: {
2372+
id: 100,
2373+
method: "item/tool/requestUserInput",
2374+
params: {
2375+
threadId: nativeThreadId,
2376+
turnId: nativeTurnId,
2377+
itemId: "question-id-item",
2378+
isBlocking: true,
2379+
questions: [
2380+
{
2381+
id: " choice ",
2382+
header: "Choice",
2383+
question: "Which option?",
2384+
options: [{ label: "A", description: "Option A" }],
2385+
},
2386+
],
2387+
},
2388+
},
2389+
},
2390+
{
2391+
type: "expect_outbound",
2392+
label: "answer",
2393+
frame: { id: 100, result: { answers: { " choice ": { answers: ["A"] } } } },
2394+
},
2395+
{
2396+
type: "emit_inbound",
2397+
label: "complete",
2398+
frame: {
2399+
method: "turn/completed",
2400+
params: {
2401+
threadId: nativeThreadId,
2402+
turn: makeCodexReplayTurn({ id: nativeTurnId, status: "completed" }),
2403+
},
2404+
},
2405+
},
2406+
],
2407+
});
2408+
const harness = yield* makeCodexReplayHarness(transcript);
2409+
yield* harness.runtime.startTurn(
2410+
makeCodexTestTurnInput({
2411+
threadId: harness.threadId,
2412+
providerThread: harness.providerThread,
2413+
now: yield* DateTime.now,
2414+
attemptId: RunAttemptId.make("question-id-attempt"),
2415+
text: "Pick one.",
2416+
}),
2417+
);
2418+
const questionItem = () =>
2419+
harness.events.flatMap((event) =>
2420+
event.type === "turn_item.updated" && event.turnItem.type === "user_input_request"
2421+
? [event.turnItem]
2422+
: [],
2423+
)[0];
2424+
const request = () =>
2425+
harness.events.flatMap((event) =>
2426+
event.type === "runtime_request.updated" ? [event.runtimeRequest] : [],
2427+
)[0];
2428+
yield* harness.until(() => questionItem() !== undefined && request() !== undefined);
2429+
const publishedId = questionItem()!.questions[0]!.id;
2430+
yield* harness.runtime.respondToRuntimeRequest({
2431+
requestId: request()!.id,
2432+
answers: { [publishedId]: ["A"] },
2433+
});
2434+
yield* harness.firstTerminal;
2435+
}).pipe(
2436+
Effect.provide(
2437+
Layer.mergeAll(
2438+
IdAllocator.layer,
2439+
McpProviderSessions.layer,
2440+
NodeServices.layer,
2441+
NodeCrypto.layer,
2442+
),
2443+
),
2444+
),
2445+
),
2446+
);
2447+
23592448
it.effect("records the model Codex reports on the root provider turn", () =>
23602449
Effect.scoped(
23612450
Effect.gen(function* () {

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -668,14 +668,15 @@ function answerValueToStrings(value: unknown): ReadonlyArray<string> {
668668

669669
function toCodexUserInputAnswers(
670670
answers: ProviderUserInputAnswers,
671-
allowedQuestionIds: ReadonlySet<string>,
671+
nativeQuestionIds: ReadonlyMap<string, string>,
672672
): CodexSchema.ToolRequestUserInputResponse["answers"] {
673673
return Object.fromEntries(
674-
Object.entries(answers).flatMap(([questionId, value]) =>
675-
allowedQuestionIds.has(questionId)
676-
? [[questionId, { answers: [...answerValueToStrings(value)] }]]
677-
: [],
678-
),
674+
Object.entries(answers).flatMap(([questionId, value]) => {
675+
const nativeId = nativeQuestionIds.get(questionId);
676+
return nativeId === undefined
677+
? []
678+
: [[nativeId, { answers: [...answerValueToStrings(value)] }]];
679+
}),
679680
);
680681
}
681682

@@ -4556,6 +4557,12 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
45564557
`Provider thread ${input.context.providerThread.id} is missing a provider session id.`,
45574558
);
45584559
}
4560+
const nativeQuestionIds = new Map(
4561+
input.questions.map((question, index) => [
4562+
nonEmptyText(question.id, `question-${index + 1}`),
4563+
question.id,
4564+
]),
4565+
);
45594566
const questions = input.questions.map((question, index) => ({
45604567
id: nonEmptyText(question.id, `question-${index + 1}`),
45614568
header: nonEmptyText(question.header, "Question"),
@@ -4630,7 +4637,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
46304637
questions,
46314638
...(input.responseMode === undefined ? {} : { responseMode: input.responseMode }),
46324639
};
4633-
return { node, request, turnItem };
4640+
return { node, request, turnItem, nativeQuestionIds };
46344641
});
46354642

46364643
yield* client.handleServerNotification("item/agentMessage/delta", (payload) =>
@@ -6103,10 +6110,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
61036110
),
61046111
);
61056112
return {
6106-
answers: toCodexUserInputAnswers(
6107-
resolved,
6108-
new Set(payload.questions.map((question) => question.id)),
6109-
),
6113+
answers: toCodexUserInputAnswers(resolved, artifacts.nativeQuestionIds),
61106114
} satisfies CodexSchema.ToolRequestUserInputResponse;
61116115
}).pipe(Effect.orDie),
61126116
);

0 commit comments

Comments
 (0)