@@ -9,12 +9,15 @@ import {
99import * as Context from "effect/Context" ;
1010import * as DateTime from "effect/DateTime" ;
1111import * as Deferred from "effect/Deferred" ;
12+ import * as Duration from "effect/Duration" ;
1213import * as Effect from "effect/Effect" ;
1314import * as Fiber from "effect/Fiber" ;
1415import * as Layer from "effect/Layer" ;
16+ import * as Queue from "effect/Queue" ;
1517import * as Ref from "effect/Ref" ;
1618import * as Schema from "effect/Schema" ;
1719import { Tool , Toolkit } from "effect/ai" ;
20+ import * as TestClock from "effect/testing/TestClock" ;
1821import { HttpClient , HttpClientResponse } from "effect/http" ;
1922
2023import * as McpHttpServer from "../mcp/McpHttpServer.ts" ;
@@ -44,6 +47,49 @@ const toolkit = Toolkit.make(
4447 } ) ,
4548) ;
4649
50+ /** One `probe` call to a linked peer at a remote address, answered by `http`. */
51+ const probeThrough = Effect . fn ( function * ( http : HttpClient . HttpClient ) {
52+ const box = descriptorOf ( "environment-box" , "Box" ) ;
53+ const context = yield * Layer . build (
54+ PeerMcpClient . layer . pipe (
55+ Layer . provide (
56+ Layer . mergeAll (
57+ Layer . succeed ( HttpClient . HttpClient , http ) ,
58+ Layer . mock ( PeerLinks . PeerLinks ) ( {
59+ resolve : ( ) =>
60+ Effect . succeed ( {
61+ link : {
62+ environmentId : box . environmentId ,
63+ label : box . label ,
64+ urls : [ "https://peer.example" ] ,
65+ access : "auto" ,
66+ linkedAt : DateTime . makeUnsafe ( 0 ) ,
67+ expiresAt : DateTime . makeUnsafe ( 1 ) ,
68+ lastReachedAt : null ,
69+ lastError : null ,
70+ } ,
71+ url : "https://peer.example" ,
72+ token : "fixture" ,
73+ } ) ,
74+ recordOutcome : ( ) => Effect . void ,
75+ } ) ,
76+ ) ,
77+ ) ,
78+ ) ,
79+ ) ;
80+ return Context . get ( context , PeerMcpClient . PeerMcpClient ) . call ( {
81+ environmentId : box . environmentId ,
82+ tool : "probe" ,
83+ arguments : { } ,
84+ limits : { runtimeMode : "approval-required" , interactionMode : "plan" } ,
85+ success : Result ,
86+ } ) ;
87+ } ) ;
88+
89+ const probeResult = JSON . stringify ( {
90+ result : { content : [ { type : "text" , text : '{"modes":"approval-required/plan"}' } ] } ,
91+ } ) ;
92+
4793const refusal = new OrchestratorMcpFailure ( {
4894 code : "thread_not_found" ,
4995 message : "Thread thread-gone was not found." ,
@@ -295,3 +341,45 @@ it.effect("renews a missing peer session once and reuses its replacement", () =>
295341 ] ) ;
296342 } ) . pipe ( Effect . scoped ) ,
297343) ;
344+
345+ it . effect ( "bounds the session handshake, not a tool call that waits longer" , ( ) =>
346+ Effect . gen ( function * ( ) {
347+ const arrived = yield * Queue . unbounded < number > ( ) ;
348+ const answer = yield * Deferred . make < void > ( ) ;
349+ let requests = 0 ;
350+ const http = HttpClient . make ( ( request ) =>
351+ Effect . gen ( function * ( ) {
352+ const index = requests ++ ;
353+ yield * Queue . offer ( arrived , index ) ;
354+ // The first initialize, then the next initialized notification, never answer;
355+ // the third session's tool call answers late.
356+ if ( index === 0 || index === 2 ) return yield * Effect . never ;
357+ if ( index === 5 ) yield * Deferred . await ( answer ) ;
358+ return HttpClientResponse . fromWeb (
359+ request ,
360+ new Response ( index === 5 ? probeResult : "" , {
361+ status : index === 4 ? 202 : 200 ,
362+ headers : { "mcp-session-id" : "session" } ,
363+ } ) ,
364+ ) ;
365+ } ) ,
366+ ) ;
367+ const probe = yield * probeThrough ( http ) ;
368+
369+ for ( const requestsBeforeStall of [ 1 , 2 ] ) {
370+ const stalled = yield * probe . pipe ( Effect . flip , Effect . forkScoped ) ;
371+ yield * Queue . takeN ( arrived , requestsBeforeStall ) ;
372+ yield * TestClock . adjust ( Duration . seconds ( 10 ) ) ;
373+ expect ( yield * Fiber . join ( stalled ) ) . toMatchObject ( {
374+ code : "orchestration_error" ,
375+ message : "The linked environment stopped answering." ,
376+ } ) ;
377+ }
378+
379+ const waiting = yield * Effect . forkScoped ( probe ) ;
380+ yield * Queue . takeN ( arrived , 3 ) ;
381+ yield * TestClock . adjust ( Duration . minutes ( 1 ) ) ;
382+ yield * Deferred . succeed ( answer , undefined ) ;
383+ expect ( yield * Fiber . join ( waiting ) ) . toEqual ( { modes : "approval-required/plan" } ) ;
384+ } ) . pipe ( Effect . scoped ) ,
385+ ) ;
0 commit comments