@@ -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