Skip to content

Commit 2e806ef

Browse files
authored
fix(codex): keep the real answer when a trailing coda follows (#123993)
* fix(codex): keep prior finals when a coda follows without tool work Live persist last-won a single assistant slot, so a trailing final_answer replaced the real summary. Keep explicit finals after the last native tool; phase-less coordination text stays last-wins. * fix(codex): record the native-work barrier from item notifications turn/completed.items is a last_agent_message Summary, so result-time tool scans cannot see intervening work. Invalidate persistable finals when native tools arrive, drop trailing silent tokens, and let a later unphased message replace earlier finals. * fix(codex): make the persistable-text barrier once-per-handoff Delayed native completions must not move the barrier past an answer that arrived while that tool was already active. Dynamic tool calls are the same handoff. If no audible text exists after the barrier, recover the last persistable answer instead of dropping it. * fix(codex): keep a post-handoff silent final authoritative A tool after an audible final starts a new answer segment. If that segment is NO_REPLY, do not revive the pre-tool text. Recover a prior answer only when the post-handoff segment is empty. * fix(codex): classify silent finals with the delivery payload predicate Token-only matching missed JSON and reasoning-wrapped NO_REPLY payloads, so a trailing control envelope could join into the visible answer. Use the shared payload-level predicate. * fix(codex): honor unphased replacement and sleep handoffs A later unphased item must invalidate older finals even when a later final skips it, and sleep is a native answer barrier.
1 parent 8b200f3 commit 2e806ef

2 files changed

Lines changed: 579 additions & 9 deletions

File tree

‎extensions/codex/src/app-server/event-projector-assistant.ts‎

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { EmbeddedRunAttemptParamsV2 as EmbeddedRunAttemptParams } from "openclaw/plugin-sdk/agent-harness-runtime";
22
import type { AssistantMessage } from "openclaw/plugin-sdk/llm";
3+
import { isSilentReplyPayloadText } from "openclaw/plugin-sdk/reply-chunking";
34
import { readStringField as readString } from "openclaw/plugin-sdk/string-coerce-runtime";
45
import {
56
createAssistantCommentaryMessage as buildAssistantCommentaryMessage,
@@ -40,6 +41,11 @@ export class CodexAssistantProjection {
4041
private responseModel: string | undefined;
4142
private streamedPartialAssistantItemId: string | undefined;
4243
private streamedPartialAssistantItemReplaceable = false;
44+
// turn/completed.items is a Summary of last_agent_message only. Tool
45+
// invalidation has to be recorded from the first item notification for each
46+
// native or dynamic handoff, or a later coda would revive every pre-tool final.
47+
private persistableAssistantBarrier = 0;
48+
private readonly persistableAssistantBarrierItemIds = new Set<string>();
4349

4450
constructor(
4551
private readonly params: EmbeddedRunAttemptParams,
@@ -155,6 +161,7 @@ export class CodexAssistantProjection {
155161
}
156162

157163
recordItemStarted(item: CodexThreadItem | undefined, itemId: string | undefined): void {
164+
this.noteNativeWorkBarrier(item);
158165
if (
159166
item?.type === "agentMessage" &&
160167
itemId &&
@@ -181,6 +188,7 @@ export class CodexAssistantProjection {
181188
itemId: string | undefined,
182189
activeItemIds: ReadonlySet<string>,
183190
): void {
191+
this.noteNativeWorkBarrier(item);
184192
if (
185193
item?.type === "agentMessage" &&
186194
itemId &&
@@ -305,8 +313,24 @@ export class CodexAssistantProjection {
305313
}
306314

307315
collectAssistantTexts(): string[] {
308-
const finalText = this.resolveFinalAssistantTextItem()?.text;
309-
return finalText ? [finalText] : [];
316+
const afterHandoff = this.collectPersistableAssistantTexts(this.persistableAssistantBarrier);
317+
const audibleAfterHandoff = afterHandoff.filter((text) => !isSilentReplyPayloadText(text));
318+
if (audibleAfterHandoff.length > 0) {
319+
return audibleAfterHandoff;
320+
}
321+
// A post-handoff silent token is the new terminal identity. Recover a
322+
// pre-barrier answer only when that segment has no persistable text.
323+
if (afterHandoff.length > 0) {
324+
return afterHandoff.slice(-1);
325+
}
326+
const recoveredAudible = this.collectPersistableAssistantTexts(0).filter(
327+
(text) => !isSilentReplyPayloadText(text),
328+
);
329+
if (recoveredAudible.length > 0) {
330+
return recoveredAudible.slice(-1);
331+
}
332+
const recovered = this.resolveFinalAssistantTextItem()?.text;
333+
return recovered ? [recovered] : [];
310334
}
311335

312336
collectCommentaryMessages(): Array<{ itemId: string; message: AssistantMessage }> {
@@ -546,6 +570,49 @@ export class CodexAssistantProjection {
546570
return undefined;
547571
}
548572

573+
private collectPersistableAssistantTexts(minIndex: number): string[] {
574+
let texts: string[] = [];
575+
let replaceable = false;
576+
// Walk time order. Unphased text replaces the current segment. Explicit
577+
// finals accumulate unless they follow a replacement. Silent payloads
578+
// never replace; they only ride along for post-handoff identity.
579+
for (let index = minIndex; index < this.assistantItemOrder.length; index += 1) {
580+
const itemId = this.assistantItemOrder[index];
581+
if (!itemId || this.assistantPhaseByItem.get(itemId) === "commentary") {
582+
continue;
583+
}
584+
const text = this.assistantTextByItem.get(itemId)?.trim();
585+
if (!text || this.isToolProgressEchoText(itemId, text)) {
586+
continue;
587+
}
588+
if (isSilentReplyPayloadText(text)) {
589+
texts.push(text);
590+
continue;
591+
}
592+
const isTerminalFinal = this.assistantPhaseByItem.get(itemId) === "final_answer";
593+
if (!isTerminalFinal || replaceable) {
594+
texts = [text];
595+
replaceable = !isTerminalFinal;
596+
continue;
597+
}
598+
texts.push(text);
599+
}
600+
return texts;
601+
}
602+
603+
private noteNativeWorkBarrier(item: CodexThreadItem | undefined): void {
604+
if (!item || !shouldAdvancePersistableAssistantBarrier(item)) {
605+
return;
606+
}
607+
if (item.id && this.persistableAssistantBarrierItemIds.has(item.id)) {
608+
return;
609+
}
610+
if (item.id) {
611+
this.persistableAssistantBarrierItemIds.add(item.id);
612+
}
613+
this.persistableAssistantBarrier = this.assistantItemOrder.length;
614+
}
615+
549616
private rememberAssistantItem(itemId: string): void {
550617
if (!itemId || this.assistantItemOrder.includes(itemId)) {
551618
return;
@@ -558,3 +625,13 @@ export class CodexAssistantProjection {
558625
return this.rawPromotedAssistantItemIds.has(itemId) && this.matchesToolProgressEcho(text);
559626
}
560627
}
628+
629+
function shouldAdvancePersistableAssistantBarrier(item: CodexThreadItem): boolean {
630+
// Sleep is a Codex public Sleep handoff, not mutating presentation work.
631+
// Record it here so a later final cannot join the pre-sleep answer.
632+
return (
633+
shouldClearTerminalPresentationForNativeItem(item) ||
634+
item.type === "dynamicToolCall" ||
635+
item.type === "sleep"
636+
);
637+
}

0 commit comments

Comments
 (0)