@@ -209,6 +209,75 @@ describe("presentAssistantMessage - Custom Tool Recording", () => {
209209 } )
210210
211211 describe ( "Presentation lock ownership" , ( ) => {
212+ it ( "drains an update queued during the helper-to-wrapper handoff" , async ( ) => {
213+ mockTask . assistantMessageContent = [ { type : "text" , content : "initial" , partial : true } ]
214+ let queuedCall : Promise < void > | undefined
215+ let lockedDuringHandoff = false
216+ mockTask . say . mockImplementationOnce ( async ( ) => {
217+ // The first microtask precedes the helper's await continuation.
218+ // The second follows that continuation but precedes the wrapper's.
219+ queueMicrotask ( ( ) => {
220+ queueMicrotask ( ( ) => {
221+ lockedDuringHandoff = mockTask . presentAssistantMessageLocked
222+ mockTask . assistantMessageContent [ 0 ] = { type : "text" , content : "final" , partial : false }
223+ mockTask . didCompleteReadingStream = true
224+ queuedCall = presentAssistantMessage ( mockTask )
225+ } )
226+ } )
227+ } )
228+
229+ await presentAssistantMessage ( mockTask )
230+ await queuedCall
231+
232+ expect ( lockedDuringHandoff ) . toBe ( true )
233+ expect ( mockTask . say . mock . calls ) . toEqual ( [
234+ [ "text" , "initial" , undefined , true ] ,
235+ [ "text" , "final" , undefined , false ] ,
236+ ] )
237+ expect ( mockTask . currentStreamingContentIndex ) . toBe ( 1 )
238+ expect ( mockTask . userMessageContentReady ) . toBe ( true )
239+ expect ( mockTask . presentAssistantMessageHasPendingUpdates ) . toBe ( false )
240+ expect ( mockTask . presentAssistantMessageLocked ) . toBe ( false )
241+ } )
242+
243+ it ( "stops draining an update queued during the handoff once the task aborts" , async ( ) => {
244+ mockTask . assistantMessageContent = [ { type : "text" , content : "initial" , partial : true } ]
245+ let queuedCall : Promise < void > | undefined
246+ let pendingDuringHandoff = false
247+ let aborted = false
248+ let abortChecksAfterAbort = 0
249+ Object . defineProperty ( mockTask , "abort" , {
250+ configurable : true ,
251+ get : ( ) => {
252+ // Fail fast instead of hanging if a drain keeps retrying an
253+ // aborted pass whose pending update can never be consumed.
254+ if ( aborted && ++ abortChecksAfterAbort > 10 ) {
255+ throw new Error ( "presenter kept draining after abort" )
256+ }
257+ return aborted
258+ } ,
259+ } )
260+ mockTask . say . mockImplementationOnce ( async ( ) => {
261+ queueMicrotask ( ( ) => {
262+ queueMicrotask ( ( ) => {
263+ mockTask . assistantMessageContent [ 0 ] = { type : "text" , content : "final" , partial : false }
264+ mockTask . didCompleteReadingStream = true
265+ queuedCall = presentAssistantMessage ( mockTask )
266+ pendingDuringHandoff = mockTask . presentAssistantMessageHasPendingUpdates
267+ aborted = true
268+ } )
269+ } )
270+ } )
271+
272+ await presentAssistantMessage ( mockTask )
273+ await queuedCall
274+
275+ expect ( pendingDuringHandoff ) . toBe ( true )
276+ expect ( mockTask . say ) . toHaveBeenCalledExactlyOnceWith ( "text" , "initial" , undefined , true )
277+ expect ( mockTask . currentStreamingContentIndex ) . toBe ( 0 )
278+ expect ( mockTask . presentAssistantMessageLocked ) . toBe ( false )
279+ } )
280+
212281 it ( "holds one lock across consecutive blocks and queues overlapping calls" , async ( ) => {
213282 mockTask . assistantMessageContent = [
214283 { type : "text" , content : "first" , partial : false } ,
0 commit comments