Skip to content

Commit ff523ac

Browse files
vincentkocsteipete
andcommitted
fix(codex): preserve authoritative turn completion
Retain terminal and assistant-completion facts when pre-start notifications exceed the bounded buffer. Exclude commentary from replies, accept legacy unphased final messages, and let completed terminal items own the final answer while rejecting interrupted, failed, or non-terminal outcomes. Co-authored-by: Peter Steinberger <[email protected]>
1 parent 78486e2 commit ff523ac

3 files changed

Lines changed: 394 additions & 46 deletions

File tree

‎extensions/codex/src/conversation-binding.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,6 +2141,64 @@ describe("codex conversation binding", () => {
21412141
await expect(readTestConversationBinding(sessionFile)).resolves.toBeUndefined();
21422142
});
21432143

2144+
it("retains a pre-start final after a saturated commentary stream", async () => {
2145+
const sessionFile = path.join(tempDir, "session.jsonl");
2146+
await writeTestConversationBinding(sessionFile, {
2147+
threadId: "thread-1",
2148+
cwd: tempDir,
2149+
});
2150+
let notificationHandler: ((notification: unknown) => void) | undefined;
2151+
sharedClientMocks.getSharedCodexAppServerClient.mockResolvedValue({
2152+
request: vi.fn(async (method: string) => {
2153+
if (method !== "turn/start") {
2154+
throw new Error(`unexpected method: ${method}`);
2155+
}
2156+
for (let index = 0; index < 100; index += 1) {
2157+
notificationHandler?.({
2158+
method: "item/completed",
2159+
params: {
2160+
threadId: "thread-1",
2161+
turnId: "turn-1",
2162+
item: {
2163+
type: "agentMessage",
2164+
id: `commentary-${index}`,
2165+
text: `progress ${index}`,
2166+
phase: "commentary",
2167+
},
2168+
},
2169+
});
2170+
}
2171+
notificationHandler?.({
2172+
method: "item/completed",
2173+
params: {
2174+
threadId: "thread-1",
2175+
turnId: "turn-1",
2176+
item: { type: "agentMessage", id: "answer", text: "authoritative answer" },
2177+
},
2178+
});
2179+
notificationHandler?.({
2180+
method: "turn/completed",
2181+
params: {
2182+
threadId: "thread-1",
2183+
turn: { id: "turn-1", status: "completed", error: null, items: [] },
2184+
},
2185+
});
2186+
return { turn: { id: "turn-1" } };
2187+
}),
2188+
addNotificationHandler: vi.fn((handler: (notification: unknown) => void) => {
2189+
notificationHandler = handler;
2190+
return () => undefined;
2191+
}),
2192+
addRequestHandler: vi.fn(() => () => undefined),
2193+
});
2194+
const { event, ctx } = boundConversationClaim(sessionFile);
2195+
2196+
await expect(handleCodexConversationInboundClaim(event, ctx)).resolves.toEqual({
2197+
handled: true,
2198+
reply: { text: "authoritative answer" },
2199+
});
2200+
});
2201+
21442202
it("reports an interrupted bound turn as cancellation instead of a partial reply", async () => {
21452203
const sessionFile = path.join(tempDir, "session.jsonl");
21462204
await writeTestConversationBinding(sessionFile, {

‎extensions/codex/src/conversation-turn-collector.test.ts‎

Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,269 @@ describe("codex conversation turn collector", () => {
7070
});
7171
});
7272

73+
it("retains the terminal outcome after a full pre-start assistant stream", async () => {
74+
const collector = createCodexConversationTurnCollector("thread-1");
75+
for (let index = 0; index < 101; index += 1) {
76+
collector.handleNotification({
77+
method: "item/agentMessage/delta",
78+
params: { threadId: "thread-1", turnId: "turn-1", itemId: "answer", delta: "." },
79+
});
80+
}
81+
collector.handleNotification({
82+
method: "turn/completed",
83+
params: {
84+
threadId: "thread-1",
85+
turn: {
86+
id: "turn-1",
87+
status: "completed",
88+
items: [{ type: "agentMessage", id: "answer", text: "authoritative answer" }],
89+
},
90+
},
91+
});
92+
collector.setTurnId("turn-1");
93+
94+
await expect(collector.wait({ timeoutMs: 100 })).resolves.toEqual({
95+
replyText: "authoritative answer",
96+
});
97+
});
98+
99+
it("does not let completed commentary replace or impersonate a final answer", async () => {
100+
const collector = createCodexConversationTurnCollector("thread-1");
101+
collector.setTurnId("turn-1");
102+
collector.handleNotification({
103+
method: "item/completed",
104+
params: {
105+
threadId: "thread-1",
106+
turnId: "turn-1",
107+
item: { type: "agentMessage", id: "answer", text: "real answer", phase: "final_answer" },
108+
},
109+
});
110+
collector.handleNotification({
111+
method: "item/agentMessage/delta",
112+
params: { threadId: "thread-1", turnId: "turn-1", itemId: "progress", delta: "progress" },
113+
});
114+
collector.handleNotification({
115+
method: "item/completed",
116+
params: {
117+
threadId: "thread-1",
118+
turnId: "turn-1",
119+
item: {
120+
type: "agentMessage",
121+
id: "completed-progress",
122+
text: "completed progress",
123+
phase: "commentary",
124+
},
125+
},
126+
});
127+
collector.handleNotification({
128+
method: "turn/completed",
129+
params: {
130+
threadId: "thread-1",
131+
turn: {
132+
id: "turn-1",
133+
status: "completed",
134+
items: [
135+
{ type: "agentMessage", id: "progress", text: "late progress", phase: "commentary" },
136+
],
137+
},
138+
},
139+
});
140+
141+
await expect(collector.wait({ timeoutMs: 100 })).resolves.toEqual({
142+
replyText: "real answer",
143+
});
144+
});
145+
146+
it("retains commentary completion after the pre-start notification buffer fills", async () => {
147+
const collector = createCodexConversationTurnCollector("thread-1");
148+
for (let index = 0; index < 100; index += 1) {
149+
collector.handleNotification({
150+
method: "item/agentMessage/delta",
151+
params: { threadId: "thread-1", turnId: "turn-1", itemId: "progress", delta: "." },
152+
});
153+
}
154+
collector.handleNotification({
155+
method: "item/completed",
156+
params: {
157+
threadId: "thread-1",
158+
turnId: "turn-1",
159+
item: { type: "agentMessage", id: "progress", text: "progress", phase: "commentary" },
160+
},
161+
});
162+
collector.handleNotification({
163+
method: "turn/completed",
164+
params: { threadId: "thread-1", turn: { id: "turn-1", status: "completed", items: [] } },
165+
});
166+
collector.setTurnId("turn-1");
167+
168+
await expect(collector.wait({ timeoutMs: 100 })).resolves.toEqual({ replyText: "" });
169+
});
170+
171+
it("retains a final completion over a full buffer of commentary completions", async () => {
172+
const collector = createCodexConversationTurnCollector("thread-1");
173+
for (let index = 0; index < 100; index += 1) {
174+
collector.handleNotification({
175+
method: "item/completed",
176+
params: {
177+
threadId: "thread-1",
178+
turnId: "turn-1",
179+
item: {
180+
type: "agentMessage",
181+
id: `commentary-${index}`,
182+
text: `progress ${index}`,
183+
phase: "commentary",
184+
},
185+
},
186+
});
187+
}
188+
collector.handleNotification({
189+
method: "item/completed",
190+
params: {
191+
threadId: "thread-1",
192+
turnId: "turn-1",
193+
item: { type: "agentMessage", id: "answer", text: "authoritative answer" },
194+
},
195+
});
196+
collector.handleNotification({
197+
method: "turn/completed",
198+
params: { threadId: "thread-1", turn: { id: "turn-1", status: "completed", items: [] } },
199+
});
200+
collector.setTurnId("turn-1");
201+
202+
await expect(collector.wait({ timeoutMs: 100 })).resolves.toEqual({
203+
replyText: "authoritative answer",
204+
});
205+
});
206+
207+
it("retains the newest final completion when the pre-start buffer is full", async () => {
208+
const collector = createCodexConversationTurnCollector("thread-1");
209+
for (let index = 0; index < 100; index += 1) {
210+
collector.handleNotification({
211+
method: "item/completed",
212+
params: {
213+
threadId: "thread-1",
214+
turnId: "turn-1",
215+
item: { type: "agentMessage", id: `answer-${index}`, text: `answer ${index}` },
216+
},
217+
});
218+
}
219+
collector.handleNotification({
220+
method: "item/completed",
221+
params: {
222+
threadId: "thread-1",
223+
turnId: "turn-1",
224+
item: { type: "agentMessage", id: "newest", text: "newest answer" },
225+
},
226+
});
227+
collector.handleNotification({
228+
method: "turn/completed",
229+
params: { threadId: "thread-1", turn: { id: "turn-1", status: "completed", items: [] } },
230+
});
231+
collector.setTurnId("turn-1");
232+
233+
await expect(collector.wait({ timeoutMs: 100 })).resolves.toEqual({
234+
replyText: "newest answer",
235+
});
236+
});
237+
238+
it("does not let unrelated tool completion evict the final assistant message", async () => {
239+
const collector = createCodexConversationTurnCollector("thread-1");
240+
for (let index = 0; index < 100; index += 1) {
241+
collector.handleNotification({
242+
method: "item/agentMessage/delta",
243+
params: { threadId: "thread-1", turnId: "turn-1", itemId: "answer", delta: "." },
244+
});
245+
collector.handleNotification({
246+
method: "item/completed",
247+
params: {
248+
threadId: "thread-1",
249+
turnId: "turn-1",
250+
item: { type: "commandExecution", id: `command-${index}` },
251+
},
252+
});
253+
}
254+
collector.handleNotification({
255+
method: "item/completed",
256+
params: {
257+
threadId: "thread-1",
258+
turnId: "turn-1",
259+
item: { type: "agentMessage", id: "answer", text: "authoritative answer" },
260+
},
261+
});
262+
collector.handleNotification({
263+
method: "turn/completed",
264+
params: { threadId: "thread-1", turn: { id: "turn-1", status: "completed", items: [] } },
265+
});
266+
collector.setTurnId("turn-1");
267+
268+
await expect(collector.wait({ timeoutMs: 100 })).resolves.toEqual({
269+
replyText: "authoritative answer",
270+
});
271+
});
272+
273+
it("returns the last completed final answer when the terminal summary is empty", async () => {
274+
const collector = createCodexConversationTurnCollector("thread-1");
275+
collector.setTurnId("turn-1");
276+
const completedAnswers: Array<{ id: string; text: string }> = [
277+
{ id: "first", text: "first answer" },
278+
{ id: "second", text: "second answer" },
279+
];
280+
for (const { id, text } of completedAnswers) {
281+
collector.handleNotification({
282+
method: "item/completed",
283+
params: {
284+
threadId: "thread-1",
285+
turnId: "turn-1",
286+
item: { type: "agentMessage", id, text, phase: "final_answer" },
287+
},
288+
});
289+
}
290+
collector.handleNotification({
291+
method: "turn/completed",
292+
params: { threadId: "thread-1", turn: { id: "turn-1", status: "completed", items: [] } },
293+
});
294+
295+
await expect(collector.wait({ timeoutMs: 100 })).resolves.toEqual({
296+
replyText: "second answer",
297+
});
298+
});
299+
300+
it("lets the terminal final answer supersede later streamed completions", async () => {
301+
const collector = createCodexConversationTurnCollector("thread-1");
302+
collector.setTurnId("turn-1");
303+
collector.handleNotification({
304+
method: "item/completed",
305+
params: {
306+
threadId: "thread-1",
307+
turnId: "turn-1",
308+
item: { type: "agentMessage", id: "answer", text: "earlier answer" },
309+
},
310+
});
311+
collector.handleNotification({
312+
method: "item/completed",
313+
params: {
314+
threadId: "thread-1",
315+
turnId: "turn-1",
316+
item: { type: "agentMessage", id: "later", text: "later answer" },
317+
},
318+
});
319+
collector.handleNotification({
320+
method: "turn/completed",
321+
params: {
322+
threadId: "thread-1",
323+
turn: {
324+
id: "turn-1",
325+
status: "completed",
326+
items: [{ type: "agentMessage", id: "answer", text: "terminal answer" }],
327+
},
328+
},
329+
});
330+
331+
await expect(collector.wait({ timeoutMs: 100 })).resolves.toEqual({
332+
replyText: "terminal answer",
333+
});
334+
});
335+
73336
it("uses completed agent message items when deltas are absent", async () => {
74337
const collector = createCodexConversationTurnCollector("thread-1");
75338
collector.setTurnId("turn-1");
@@ -233,6 +496,33 @@ describe("codex conversation turn collector", () => {
233496
await expect(completion).rejects.toThrow("codex app-server turn interrupted");
234497
});
235498

499+
it("rejects a non-terminal status instead of returning streamed partial text", async () => {
500+
const collector = createCodexConversationTurnCollector("thread-1");
501+
collector.setTurnId("turn-1");
502+
const completion = collector.wait({ timeoutMs: 1_000 });
503+
504+
collector.handleNotification({
505+
method: "item/agentMessage/delta",
506+
params: {
507+
threadId: "thread-1",
508+
turnId: "turn-1",
509+
itemId: "item-1",
510+
delta: "unfinished answer",
511+
},
512+
});
513+
collector.handleNotification({
514+
method: "turn/completed",
515+
params: {
516+
threadId: "thread-1",
517+
turn: { id: "turn-1", status: "inProgress", error: null, items: [] },
518+
},
519+
});
520+
521+
await expect(completion).rejects.toThrow(
522+
"codex app-server turn completed without a valid terminal status",
523+
);
524+
});
525+
236526
it("confirms a terminal notification that arrives after the bound turn times out", async () => {
237527
vi.useFakeTimers();
238528
const collector = createCodexConversationTurnCollector("thread-1");

0 commit comments

Comments
 (0)