@@ -225,7 +225,7 @@ it.effect("effect-acp agent handles core agent requests and outbound client requ
225225 } ) ,
226226) ;
227227
228- it . effect ( "effect-acp agent uses distinct ids for RPC calls and extension requests " , ( ) =>
228+ it . effect ( "effect-acp agent keeps RPC ids within int32 and apart from extension ids " , ( ) =>
229229 Effect . gen ( function * ( ) {
230230 const { stdio, input, output } = yield * makeInMemoryStdio ( ) ;
231231 const scope = yield * Scope . make ( ) ;
@@ -274,6 +274,11 @@ it.effect("effect-acp agent uses distinct ids for RPC calls and extension reques
274274 : yield * decodedExt ( firstOutbound ) ;
275275
276276 assert . notEqual ( permissionRequest . id , extRequest . id ) ;
277+ // Keep locally generated ids compatible with SDKs that decode signed int32.
278+ assert . typeOf ( permissionRequest . id , "number" ) ;
279+ assert . equal ( permissionRequest . id , 2 ** 30 ) ;
280+ assert . isAtMost ( Number ( permissionRequest . id ) , 2 ** 31 - 1 ) ;
281+ assert . equal ( extRequest . id , 1 ) ;
277282
278283 yield * Queue . offer (
279284 input ,
@@ -300,10 +305,95 @@ it.effect("effect-acp agent uses distinct ids for RPC calls and extension reques
300305 const permission = yield * Fiber . join ( permissionFiber ) ;
301306 assert . equal ( permission . outcome . outcome , "selected" ) ;
302307 assert . deepEqual ( yield * Fiber . join ( extFiber ) , { ok : true } ) ;
308+
309+ const nextPermissionFiber = yield * agent . client
310+ . requestPermission ( permissionRequest . params )
311+ . pipe ( Effect . forkScoped ) ;
312+ const nextPermissionRequest = yield * decodedPermission ( yield * Queue . take ( output ) ) ;
313+ assert . equal ( nextPermissionRequest . id , 2 ** 30 + 1 ) ;
314+ yield * Queue . offer (
315+ input ,
316+ yield * encodeJsonl ( RequestPermissionResponse , {
317+ jsonrpc : "2.0" ,
318+ id : nextPermissionRequest . id ,
319+ result : { outcome : { outcome : "cancelled" } } ,
320+ } ) ,
321+ ) ;
322+ assert . equal ( ( yield * Fiber . join ( nextPermissionFiber ) ) . outcome . outcome , "cancelled" ) ;
303323 } ) . pipe ( Effect . provide ( context ) , Effect . ensuring ( Scope . close ( scope , Exit . void ) ) ) ;
304324 } ) ,
305325) ;
306326
327+ it . effect . each ( [ 0 , 2 ** 30 , 2 ** 32 ] ) (
328+ "effect-acp agent answers incoming id %s independently of outbound ids" ,
329+ ( requestId ) =>
330+ Effect . gen ( function * ( ) {
331+ const { stdio, input, output } = yield * makeInMemoryStdio ( ) ;
332+ const agent = yield * AcpAgent . make ( stdio ) ;
333+ const contexts = yield * Ref . make < Array < AcpProtocol . AcpRequestContext > > ( [ ] ) ;
334+ yield * agent . handleInitialize ( ( _request , context ) =>
335+ Ref . update ( contexts , ( current ) => [ ...current , context ] ) . pipe (
336+ Effect . as ( {
337+ protocolVersion : 2 ,
338+ capabilities : { } ,
339+ info : { name : "mock-agent" , version : "0.0.0" } ,
340+ } ) ,
341+ ) ,
342+ ) ;
343+
344+ // Opposite directions may reuse an id even while a request is pending.
345+ const outboundFiber = yield * agent . client
346+ . requestPermission ( {
347+ sessionId : "session-1" ,
348+ title : "Allow mock action" ,
349+ subject : {
350+ type : "tool_call" ,
351+ toolCall : { toolCallId : "tool-1" , title : "Allow mock action" } ,
352+ } ,
353+ options : [ { optionId : "allow" , name : "Allow" , kind : "allow_once" } ] ,
354+ } )
355+ . pipe ( Effect . forkScoped ) ;
356+ const outbound = yield * decodeRequestPermissionRequest ( yield * Queue . take ( output ) ) ;
357+
358+ yield * Queue . offer (
359+ input ,
360+ yield * encodeJsonl ( InitializeRequest , {
361+ jsonrpc : "2.0" ,
362+ id : requestId ,
363+ method : "initialize" ,
364+ params : {
365+ protocolVersion : 2 ,
366+ capabilities : { } ,
367+ info : { name : "test-client" , version : "0.0.0" } ,
368+ } ,
369+ headers : [ ] ,
370+ } ) ,
371+ ) ;
372+ assert . deepEqual ( yield * decodeInitializeResponse ( yield * Queue . take ( output ) ) , {
373+ jsonrpc : "2.0" ,
374+ id : requestId ,
375+ result : {
376+ protocolVersion : 2 ,
377+ capabilities : { } ,
378+ info : { name : "mock-agent" , version : "0.0.0" } ,
379+ } ,
380+ } ) ;
381+ assert . deepEqual ( yield * Ref . get ( contexts ) , [
382+ { requestId : `$t3:jsonrpc:number:${ requestId } ` , method : "initialize" } ,
383+ ] ) ;
384+
385+ yield * Queue . offer (
386+ input ,
387+ yield * encodeJsonl ( RequestPermissionResponse , {
388+ jsonrpc : "2.0" ,
389+ id : outbound . id ,
390+ result : { outcome : { outcome : "cancelled" } } ,
391+ } ) ,
392+ ) ;
393+ assert . equal ( ( yield * Fiber . join ( outboundFiber ) ) . outcome . outcome , "cancelled" ) ;
394+ } ) ,
395+ ) ;
396+
307397it . effect ( "effect-acp agent answers a request whose handler dies with an error for it" , ( ) =>
308398 Effect . gen ( function * ( ) {
309399 const { stdio, input, output } = yield * makeInMemoryStdio ( ) ;
0 commit comments