Skip to content

Commit 6637747

Browse files
committed
fix(codex): preserve assistant text on post-turn compaction failure (#94688)
When the Codex app-server marks a turn as 'failed' because post-turn CLI transcript compaction summarization failed (e.g. Connection error), the bounded-turn collector now recovers the already-collected assistant text from item/agentMessage/delta and item/completed notifications instead of discarding it. Root cause: text collection happened AFTER error checks, so a 'failed' status always threw before inspecting the accumulated assistantTextByItem map. Fix: collect text and items first, then only throw when the turn is genuinely empty. Compaction failures are still logged at warn level for observability.
1 parent f7c3775 commit 6637747

1 file changed

Lines changed: 27 additions & 8 deletions

File tree

‎extensions/codex/src/app-server/bounded-turn.ts‎

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from "node:fs/promises";
22
import path from "node:path";
3+
import { embeddedAgentLog } from "openclaw/plugin-sdk/agent-harness-runtime";
34
import type { AuthProfileStore } from "openclaw/plugin-sdk/agent-runtime";
45
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
56
import { resolveTimerTimeoutMs } from "openclaw/plugin-sdk/number-runtime";
@@ -404,14 +405,10 @@ function createCodexBoundedTurnCollector(threadId: string, taskLabel: string) {
404405
taskLabel,
405406
});
406407
}
407-
if (promptError) {
408-
throw new Error(promptError);
409-
}
410-
if (completedTurn?.status === "failed") {
411-
throw new Error(
412-
completedTurn.error?.message ?? `codex app-server ${taskLabel} turn failed`,
413-
);
414-
}
408+
// Collect text from items and deltas FIRST, before error checks.
409+
// A turn may be marked as "failed" due to post-turn CLI compaction
410+
// failure even after the assistant reply has already been received
411+
// via item/agentMessage/delta and item/completed notifications (#94688).
415412
const items = collectCompletedItems(completedTurn?.items, completedItems);
416413
const itemText = collectAssistantTextFromItems(items);
417414
const deltaText = assistantItemOrder
@@ -420,6 +417,28 @@ function createCodexBoundedTurnCollector(threadId: string, taskLabel: string) {
420417
.join("\n\n")
421418
.trim();
422419
const text = (itemText || deltaText).trim();
420+
421+
if (promptError) {
422+
throw new Error(promptError);
423+
}
424+
if (completedTurn?.status === "failed") {
425+
if (text) {
426+
// Post-turn compaction/summarization failed, but the assistant
427+
// reply was already collected successfully. Deliver the recovered
428+
// text instead of discarding the whole turn.
429+
embeddedAgentLog.warn(
430+
`codex app-server ${taskLabel} turn failed but assistant text was already collected`,
431+
{
432+
turnError: completedTurn.error?.message ?? "unknown",
433+
textLength: text.length,
434+
},
435+
);
436+
return { text, items };
437+
}
438+
throw new Error(
439+
completedTurn.error?.message ?? `codex app-server ${taskLabel} turn failed`,
440+
);
441+
}
423442
if (!text) {
424443
throw new Error(`Codex app-server ${taskLabel} turn returned no text.`);
425444
}

0 commit comments

Comments
 (0)