@@ -210,20 +210,45 @@ describe("GatewayProtocolClient requests", () => {
210210 await expect ( aborted ) . rejects . toThrow ( "gateway request aborted for aborted" ) ;
211211
212212 const replacement = client . request ( "replacement" , { } , { timeoutMs : null } ) ;
213- expect ( latestFrame ( connection ) ) . toMatchObject ( { id : "same-id:1 " , method : "replacement" } ) ;
214- respond ( connection , "same-id" , { stale : true } ) ;
213+ expect ( latestFrame ( connection ) ) . toMatchObject ( { id : "2: same-id" , method : "replacement" } ) ;
214+ respond ( connection , "1: same-id" , { stale : true } ) ;
215215 expect ( client . hasPendingRequests ) . toBe ( true ) ;
216- respond ( connection , "same-id:1 " , { current : true } ) ;
216+ respond ( connection , "2: same-id" , { current : true } ) ;
217217 await expect ( replacement ) . resolves . toEqual ( { current : true } ) ;
218218
219219 await expect ( client . request ( "send.failure" , { } , { timeoutMs : null } ) ) . rejects . toThrow (
220220 "synthetic send failure" ,
221221 ) ;
222- expect ( latestFrame ( connection ) ) . toMatchObject ( { id : "same-id:2 " , method : "send.failure" } ) ;
222+ expect ( latestFrame ( connection ) ) . toMatchObject ( { id : "3: same-id" , method : "send.failure" } ) ;
223223 expect ( client . hasPendingRequests ) . toBe ( false ) ;
224224 client . stop ( ) ;
225225 } ) ;
226226
227+ it ( "keeps concurrent requests distinct when generated IDs contain sequence suffixes" , async ( ) => {
228+ const generatedIds = [ "same-id:1" , "same-id" ] ;
229+ const { client, connections } = createRequestHarness ( {
230+ createRequestId : ( ) => generatedIds . shift ( ) ?? "same-id" ,
231+ } ) ;
232+ const connection = connections [ 0 ] ;
233+ if ( ! connection ) {
234+ throw new Error ( "expected request connection" ) ;
235+ }
236+
237+ const first = client . request ( "first" , { } , { timeoutMs : null } ) ;
238+ const second = client . request ( "second" , { } , { timeoutMs : null } ) ;
239+ const [ firstFrame , secondFrame ] = connection . frames ;
240+ if ( ! firstFrame || ! secondFrame ) {
241+ throw new Error ( "expected concurrent request frames" ) ;
242+ }
243+ expect ( firstFrame . id ) . not . toBe ( secondFrame . id ) ;
244+
245+ respond ( connection , firstFrame . id , { request : "first" } ) ;
246+ respond ( connection , secondFrame . id , { request : "second" } ) ;
247+ await expect ( first ) . resolves . toEqual ( { request : "first" } ) ;
248+ await expect ( second ) . resolves . toEqual ( { request : "second" } ) ;
249+ client . stop ( ) ;
250+ } ) ;
251+
227252 it ( "ignores late accepted and final replies after a timeout collision" , async ( ) => {
228253 vi . useFakeTimers ( ) ;
229254 const onAccepted = vi . fn ( ) ;
@@ -242,14 +267,14 @@ describe("GatewayProtocolClient requests", () => {
242267 { } ,
243268 { timeoutMs : null , expectFinal : true , onAccepted } ,
244269 ) ;
245- expect ( latestFrame ( connection ) ) . toMatchObject ( { id : "same-id:1 " , method : "agent" } ) ;
246- respond ( connection , "same-id" , { status : "accepted" , runId : "old" } ) ;
247- respond ( connection , "same-id" , { status : "ok" , runId : "old" } ) ;
270+ expect ( latestFrame ( connection ) ) . toMatchObject ( { id : "2: same-id" , method : "agent" } ) ;
271+ respond ( connection , "1: same-id" , { status : "accepted" , runId : "old" } ) ;
272+ respond ( connection , "1: same-id" , { status : "ok" , runId : "old" } ) ;
248273 expect ( onAccepted ) . not . toHaveBeenCalled ( ) ;
249274 expect ( client . hasPendingRequests ) . toBe ( true ) ;
250275
251- respond ( connection , "same-id:1 " , { status : "accepted" , runId : "new" } ) ;
252- respond ( connection , "same-id:1 " , { status : "ok" , runId : "new" } ) ;
276+ respond ( connection , "2: same-id" , { status : "accepted" , runId : "new" } ) ;
277+ respond ( connection , "2: same-id" , { status : "ok" , runId : "new" } ) ;
253278 await expect ( replacement ) . resolves . toEqual ( { status : "ok" , runId : "new" } ) ;
254279 expect ( onAccepted ) . toHaveBeenCalledExactlyOnceWith ( { status : "accepted" , runId : "new" } ) ;
255280 client . stop ( ) ;
@@ -463,7 +488,7 @@ describe("GatewayProtocolClient requests", () => {
463488 client . stop ( ) ;
464489 } ) ;
465490
466- it ( "clears generation tombstones when the socket flushes" , async ( ) => {
491+ it ( "restarts the request sequence when the socket flushes" , async ( ) => {
467492 vi . useFakeTimers ( ) ;
468493 const { client, connections } = createRequestHarness ( { createRequestId : ( ) => "same-id" } ) ;
469494 const firstConnection = connections [ 0 ] ;
@@ -482,8 +507,8 @@ describe("GatewayProtocolClient requests", () => {
482507 throw new Error ( "expected replacement request connection" ) ;
483508 }
484509 const replacement = client . request ( "second" , { } , { timeoutMs : null } ) ;
485- expect ( latestFrame ( secondConnection ) ) . toMatchObject ( { id : "same-id" , method : "second" } ) ;
486- respond ( secondConnection , "same-id" , { ok : true } ) ;
510+ expect ( latestFrame ( secondConnection ) ) . toMatchObject ( { id : "1: same-id" , method : "second" } ) ;
511+ respond ( secondConnection , "1: same-id" , { ok : true } ) ;
487512 await expect ( replacement ) . resolves . toEqual ( { ok : true } ) ;
488513 client . stop ( ) ;
489514 } ) ;
0 commit comments