@@ -1323,15 +1323,31 @@ describe("TUI PTY real backends", () => {
13231323 } ) ;
13241324 try {
13251325 let queueClientConnected = false ;
1326+ const admittedRunIds = new Set < string > ( ) ;
13261327 queueClient . onConnected = ( ) => {
13271328 queueClientConnected = true ;
13281329 } ;
1330+ // Retain admission events that arrive before both chat.send ACKs settle.
1331+ queueClient . onEvent = ( { event, payload } ) => {
1332+ if ( event !== "chat" || ! payload || typeof payload !== "object" ) {
1333+ return ;
1334+ }
1335+ const chatEvent = payload as { runId ?: unknown ; sessionKey ?: unknown ; state ?: unknown } ;
1336+ if (
1337+ chatEvent . state === "final" &&
1338+ chatEvent . sessionKey === fixture . sessionKey &&
1339+ typeof chatEvent . runId === "string"
1340+ ) {
1341+ admittedRunIds . add ( chatEvent . runId ) ;
1342+ }
1343+ } ;
13291344 queueClient . start ( ) ;
13301345 await waitFor ( {
13311346 timeoutMs : LOCAL_STARTUP_TIMEOUT_MS ,
13321347 read : ( ) => ( queueClientConnected ? true : null ) ,
13331348 onTimeout : ( ) => new Error ( "TUI Gateway client did not connect" ) ,
13341349 } ) ;
1350+ await queueClient . subscribeSessionEvents ( ) ;
13351351 await fixture . run . write ( "/queue collect debounce:250ms\r" , { delay : false } ) ;
13361352 await fixture . waitForOutput ( "Queue mode set to collect." ) ;
13371353 await fixture . run . write ( "slow collect parent\r" ) ;
@@ -1345,16 +1361,23 @@ describe("TUI PTY real backends", () => {
13451361 sessionKey : fixture . sessionKey ,
13461362 message : "collect prompt alpha" ,
13471363 } ) ;
1348- await sleep ( 50 ) ;
13491364 const betaSend = queueClient . sendChat ( {
13501365 sessionKey : fixture . sessionKey ,
13511366 message : "collect prompt beta" ,
13521367 } ) ;
13531368 const sendResults = await Promise . all ( [ alphaSend , betaSend ] ) ;
13541369 expect ( sendResults . map ( ( result ) => result . status ) ) . toEqual ( [ "started" , "started" ] ) ;
1355- // Let both Gateway submissions reach the active-turn queue before the
1356- // parent response opens the collect debounce window.
1357- await sleep ( 1_000 ) ;
1370+ const expectedRunIds = sendResults . map ( ( { runId } ) => runId ) ;
1371+ await waitFor ( {
1372+ timeoutMs : LOCAL_OUTPUT_TIMEOUT_MS ,
1373+ read : ( ) => ( expectedRunIds . every ( ( runId ) => admittedRunIds . has ( runId ) ) ? true : null ) ,
1374+ onTimeout : ( ) =>
1375+ new Error (
1376+ `queued prompts were not admitted: expected ${ expectedRunIds . join ( ", " ) } ; ` +
1377+ `observed ${ [ ...admittedRunIds ] . join ( ", " ) } \n${ fixture . gateway . logs ( ) } \n` +
1378+ fixture . run . output ( ) ,
1379+ ) ,
1380+ } ) ;
13581381 fixture . mockModel . releaseFirstResponse ( ) ;
13591382 await waitFor ( {
13601383 timeoutMs : LOCAL_OUTPUT_TIMEOUT_MS ,
0 commit comments