22import * as NodeStream from "node:stream" ;
33
44import { describe , expect , it } from "@effect/vitest" ;
5+ import * as Deferred from "effect/Deferred" ;
56import * as Effect from "effect/Effect" ;
67import * as Fiber from "effect/Fiber" ;
78
8- import { runAcpMcpStdioBridge } from "./AcpMcpStdioBridge.ts" ;
9+ import { mcpToolSession , runAcpMcpStdioBridge } from "./AcpMcpStdioBridge.ts" ;
910
1011function makeHarness ( responder : ( request : Request ) => Promise < Response > | Response ) {
1112 const input = new NodeStream . PassThrough ( ) ;
@@ -29,6 +30,13 @@ function makeHarness(responder: (request: Request) => Promise<Response> | Respon
2930 return { input, written, requests, bridge } ;
3031}
3132
33+ function pendingFetch ( started : Deferred . Deferred < AbortSignal | undefined > ) {
34+ return ( _url : string , init ?: RequestInit ) : Promise < Response > => {
35+ Deferred . doneUnsafe ( started , Effect . succeed ( init ?. signal ?? undefined ) ) ;
36+ return new Promise ( ( ) => { } ) ;
37+ } ;
38+ }
39+
3240describe ( "AcpMcpStdioBridge" , ( ) => {
3341 it . effect ( "forwards requests and replays the session id and protocol version" , ( ) =>
3442 Effect . gen ( function * ( ) {
@@ -282,4 +290,43 @@ describe("AcpMcpStdioBridge", () => {
282290 expect ( JSON . parse ( written [ 0 ] ! ) ) . toMatchObject ( { id : 7 , result : { } } ) ;
283291 } ) ,
284292 ) ;
293+
294+ it . effect ( "aborts the pending fetch when a tool call is interrupted" , ( ) =>
295+ Effect . gen ( function * ( ) {
296+ const started = yield * Deferred . make < AbortSignal | undefined > ( ) ;
297+ const fiber = yield * mcpToolSession ( {
298+ endpoint : "http://127.0.0.1:1/mcp" ,
299+ authorization : "Bearer bridge-test" ,
300+ clientName : "t3-code-acp-cli" ,
301+ fetchImplementation : pendingFetch ( started ) ,
302+ } ) . pipe (
303+ Effect . flatMap ( ( session ) => session . call ( "orchestrator_capabilities" , { } ) ) ,
304+ Effect . scoped ,
305+ Effect . forkChild ,
306+ ) ;
307+
308+ const signal = yield * Deferred . await ( started ) ;
309+ yield * Fiber . interrupt ( fiber ) ;
310+ expect ( signal ?. aborted ) . toBe ( true ) ;
311+ } ) ,
312+ ) ;
313+
314+ it . effect ( "aborts the pending fetch when the stdio bridge is interrupted" , ( ) =>
315+ Effect . gen ( function * ( ) {
316+ const started = yield * Deferred . make < AbortSignal | undefined > ( ) ;
317+ const input = new NodeStream . PassThrough ( ) ;
318+ const fiber = yield * runAcpMcpStdioBridge ( {
319+ endpoint : "http://127.0.0.1:1/mcp" ,
320+ authorization : "Bearer bridge-test" ,
321+ input,
322+ output : { write : ( ) => undefined } ,
323+ fetchImplementation : pendingFetch ( started ) ,
324+ } ) . pipe ( Effect . forkChild ) ;
325+
326+ input . write ( `${ JSON . stringify ( { jsonrpc : "2.0" , id : 1 , method : "tools/list" } ) } \n` ) ;
327+ const signal = yield * Deferred . await ( started ) ;
328+ yield * Fiber . interrupt ( fiber ) ;
329+ expect ( signal ?. aborted ) . toBe ( true ) ;
330+ } ) ,
331+ ) ;
285332} ) ;
0 commit comments