Skip to content

Commit 87b36c7

Browse files
saphidmaria-rcks
andauthored
fix(codex): preserve partial answer messages and thread histories (#23388)
Co-authored-by: SAPHID <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 8f6c9b7 commit 87b36c7

5 files changed

Lines changed: 219 additions & 24 deletions

File tree

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3201,7 +3201,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
32013201
answers: ReadonlyArray<{
32023202
readonly id: string;
32033203
readonly text: string;
3204-
readonly phase?: "commentary" | "final_answer" | null;
3204+
readonly phase?: "commentary" | "partial_answer" | "final_answer" | null;
32053205
readonly omitPhase?: boolean;
32063206
readonly streamed?: boolean;
32073207
readonly completionDelayMs?: number;
@@ -3296,6 +3296,39 @@ describe("CodexAdapterV2 post-settle continuation", () => {
32963296
});
32973297
};
32983298

3299+
it.effect.each([false, true])(
3300+
"preserves a final answer that repeats a preceding partial answer (streamed: %s)",
3301+
(streamed) =>
3302+
Effect.scoped(
3303+
Effect.gen(function* () {
3304+
const transcript = finalAnswerTranscript("codex-partial-then-final", [
3305+
{ id: "partial", text: "CODEX_RECOVERY_OK", phase: "partial_answer", streamed },
3306+
{ id: "final", text: "CODEX_RECOVERY_OK", phase: "final_answer", streamed },
3307+
]);
3308+
const harness = yield* makeCodexReplayHarness(transcript);
3309+
const now = yield* DateTime.now;
3310+
yield* harness.runtime.startTurn(
3311+
makeCodexTestTurnInput({
3312+
threadId: harness.threadId,
3313+
providerThread: harness.providerThread,
3314+
now,
3315+
attemptId: RunAttemptId.make("attempt-codex-partial-then-final"),
3316+
text: "Reply with the requested recovery marker.",
3317+
}),
3318+
);
3319+
yield* harness.awaitTerminal(1);
3320+
assert.deepEqual(
3321+
assistantMessages(harness.events).map((event) => event.message.text),
3322+
["CODEX_RECOVERY_OK", "CODEX_RECOVERY_OK"],
3323+
);
3324+
}).pipe(
3325+
Effect.provide(
3326+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
3327+
),
3328+
),
3329+
),
3330+
);
3331+
32993332
it.effect.each([
33003333
{
33013334
name: "a text reply gains each saved image once",

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4769,7 +4769,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
47694769
}
47704770

47714771
if (payload.item.type === "agentMessage") {
4772-
if (payload.item.phase !== "commentary") {
4772+
if (payload.item.phase == null || payload.item.phase === "final_answer") {
47734773
yield* Ref.update(finalAnswerItemIdsByTurn, (current) => {
47744774
const updated = new Map(current);
47754775
const itemIds = new Set(updated.get(payload.turnId) ?? []);
@@ -5132,7 +5132,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
51325132
return;
51335133
}
51345134

5135-
const finalAnswer = payload.item.phase !== "commentary";
5135+
const finalAnswer = payload.item.phase == null || payload.item.phase === "final_answer";
51365136
if (finalAnswer) {
51375137
yield* Ref.update(finalAnswerItemIdsByTurn, (current) => {
51385138
const updated = new Map(current);

‎packages/effect-codex-app-server/scripts/generate.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,29 @@ const ManualSchemas: Record<string, Schema.Json> = {
145145
// `account/read` decode and take the provider down with it.
146146
const DefinitionOverrides: Record<string, Schema.Json> = {
147147
PlanType: { type: "string" },
148+
// Newer custom Codex binaries emit partial answers before additional tools or text.
149+
MessagePhase: {
150+
description:
151+
'Classifies an assistant message as interim commentary or final answer text.\n\nProviders do not emit this consistently, so callers must treat `None` as "phase unknown" and keep compatibility behavior for legacy models.',
152+
oneOf: [
153+
{
154+
type: "string",
155+
enum: ["commentary"],
156+
description:
157+
"Mid-turn assistant text (for example preamble/progress narration).\n\nAdditional tool calls or assistant output may follow before turn completion.",
158+
},
159+
{
160+
type: "string",
161+
enum: ["partial_answer"],
162+
description: "Stable answer text that may be followed by more assistant output or tools.",
163+
},
164+
{
165+
type: "string",
166+
enum: ["final_answer"],
167+
description: "The assistant's terminal answer text for the current turn.",
168+
},
169+
],
170+
},
148171
};
149172

150173
const getGeneratedPaths = Effect.fn("getGeneratedPaths")(function* () {

0 commit comments

Comments
 (0)