@@ -99,6 +99,10 @@ const serveB = (initial: ExecutionEnvironmentDescriptor) =>
9999 const redirects = yield * Ref . make < ReadonlyMap < string , string > > ( new Map ( ) ) ;
100100 const redirectedBodies = yield * Ref . make < ReadonlyArray < string > > ( [ ] ) ;
101101 const toolResult = yield * Ref . make < McpSchema . CallToolResult | null > ( null ) ;
102+ const sse = yield * Ref . make ( false ) ;
103+ const replies = yield * Ref . make < ReadonlyMap < string , { status : number ; body : unknown } > > (
104+ new Map ( ) ,
105+ ) ;
102106 const layerEnvironment = Layer . succeed ( ServerEnvironment . ServerEnvironment , {
103107 getEnvironmentId : Ref . get ( descriptor ) . pipe ( Effect . map ( ( current ) => current . environmentId ) ) ,
104108 getDescriptor : Ref . get ( descriptor ) ,
@@ -136,12 +140,30 @@ const serveB = (initial: ExecutionEnvironmentDescriptor) =>
136140 if ( authorization !== undefined ) {
137141 yield * Ref . update ( bearers , ( seen ) => [ ...seen , authorization ] ) ;
138142 }
143+ const reply = ( yield * Ref . get ( replies ) ) . get ( request . url ) ;
144+ if ( reply !== undefined )
145+ return HttpServerResponse . jsonUnsafe ( reply . body , { status : reply . status } ) ;
139146 const response = yield * httpEffect ;
140147 // Keep real authentication and dispatch, but exercise richer peer wire results.
141148 const result = yield * Ref . get ( toolResult ) ;
142149 if ( result !== null && request . url === "/mcp" && response . status === 200 ) {
143150 const rpc = yield * decodeMcpRequest ( yield * request . json ) ;
144151 if ( rpc . method === "tools/call" ) {
152+ if ( yield * Ref . get ( sse ) ) {
153+ return HttpServerResponse . text (
154+ [
155+ 'data: {"jsonrpc":"2.0","method":"notifications/progress","params":{"result":"notification"}}' ,
156+ "" ,
157+ 'data: {"jsonrpc":"2.0","id":999,"error":{"code":-1,"message":"other request"}}' ,
158+ "" ,
159+ 'data: {"jsonrpc":"2.0",' ,
160+ 'data: "id":' + rpc . id + ',"result":' + JSON . stringify ( result ) + "}" ,
161+ "" ,
162+ "" ,
163+ ] . join ( "\r\n" ) ,
164+ { contentType : "text/event-stream" } ,
165+ ) ;
166+ }
145167 return HttpServerResponse . jsonUnsafe ( { jsonrpc : "2.0" , id : rpc . id , result } ) ;
146168 }
147169 }
@@ -204,6 +226,8 @@ const serveB = (initial: ExecutionEnvironmentDescriptor) =>
204226 alternateDescriptor,
205227 bearers,
206228 toolResult,
229+ sse,
230+ replies,
207231 redirects,
208232 redirectedBodies,
209233 /** How often its descriptor was asked for, as a probe of whether it answers. */
@@ -215,6 +239,7 @@ const serveB = (initial: ExecutionEnvironmentDescriptor) =>
215239const makeA = (
216240 descriptor : ExecutionEnvironmentDescriptor ,
217241 afterSecretWrite : Effect . Effect < void > = Effect . void ,
242+ beforeSecretRemove : Effect . Effect < void , ServerSecretStore . SecretStoreError > = Effect . void ,
218243) =>
219244 Effect . gen ( function * ( ) {
220245 const secretReads = yield * Ref . make ( 0 ) ;
@@ -225,6 +250,7 @@ const makeA = (
225250 return ServerSecretStore . ServerSecretStore . of ( {
226251 ...secrets ,
227252 set : ( name , value ) => secrets . set ( name , value ) . pipe ( Effect . andThen ( afterSecretWrite ) ) ,
253+ remove : ( name ) => beforeSecretRemove . pipe ( Effect . andThen ( secrets . remove ( name ) ) ) ,
228254 get : ( name ) =>
229255 Ref . update ( secretReads , ( count ) => count + 1 ) . pipe ( Effect . andThen ( secrets . get ( name ) ) ) ,
230256 } ) ;
@@ -289,7 +315,29 @@ it.effect("links, reads and forgets a peer while preserving access limits and re
289315 Effect . scoped (
290316 Effect . gen ( function * ( ) {
291317 const b = yield * serveB ( box ) ;
292- const a = yield * makeA ( laptop ) ;
318+ const removal = yield * Ref . make < "fail" | "pause" | null > ( null ) ;
319+ const removalEntered = yield * Deferred . make < void > ( ) ;
320+ const allowRemoval = yield * Deferred . make < void > ( ) ;
321+ const a = yield * makeA (
322+ laptop ,
323+ Effect . void ,
324+ Ref . get ( removal ) . pipe (
325+ Effect . flatMap ( ( action ) =>
326+ action === "fail"
327+ ? Effect . fail (
328+ new ServerSecretStore . SecretStoreRemoveError ( {
329+ resource : "test credential" ,
330+ cause : new Error ( "removal failed" ) ,
331+ } ) ,
332+ )
333+ : action === "pause"
334+ ? Deferred . succeed ( removalEntered , undefined ) . pipe (
335+ Effect . andThen ( Deferred . await ( allowRemoval ) ) ,
336+ )
337+ : Effect . void ,
338+ ) ,
339+ ) ,
340+ ) ;
293341
294342 const refused = yield * a . links
295343 . link ( { url : b . url , pairingCode : "not-a-code" , access : "auto" } )
@@ -332,6 +380,31 @@ it.effect("links, reads and forgets a peer while preserving access limits and re
332380 const notFound = yield * probe ( a , undefined , "refuse" ) . pipe ( Effect . flip ) ;
333381 expect ( notFound ) . toMatchObject ( { code : "thread_not_found" , message : "No such thread." } ) ;
334382
383+ const names = yield * a . secretNames ;
384+ const beforeUnlink = yield * a . links . get ( box . environmentId ) ;
385+ yield * Ref . set ( removal , "fail" ) ;
386+ expect ( ( yield * a . links . unlink ( box . environmentId ) . pipe ( Effect . flip ) ) . reason ) . toBe ( "storage" ) ;
387+ expect ( yield * a . links . get ( box . environmentId ) ) . toEqual ( beforeUnlink ) ;
388+ expect ( yield * a . secretNames ) . toEqual ( names ) ;
389+ expect ( yield * probe ( a ) ) . toEqual ( { caller : "T3 Code · Laptop" , modes : "auto/default" } ) ;
390+
391+ yield * Ref . set ( removal , "pause" ) ;
392+ const unlinking = yield * a . links . unlink ( box . environmentId ) . pipe ( Effect . forkScoped ) ;
393+ yield * Deferred . await ( removalEntered ) ;
394+ yield * Ref . set ( removal , null ) ;
395+ const newPairing = yield * b . auth . issuePairingCredential ( ) ;
396+ const relinked = yield * a . links . link ( {
397+ url : b . url ,
398+ pairingCode : newPairing . credential ,
399+ access : "approval-required" ,
400+ } ) ;
401+ yield * Deferred . succeed ( allowRemoval , undefined ) ;
402+ expect ( yield * Fiber . join ( unlinking ) ) . toBe ( false ) ;
403+ expect ( yield * a . links . get ( box . environmentId ) ) . toEqual ( Option . some ( relinked ) ) ;
404+ expect ( yield * probe ( a ) ) . toEqual ( {
405+ caller : "T3 Code · Laptop" ,
406+ modes : "approval-required/default" ,
407+ } ) ;
335408 expect ( yield * a . links . unlink ( box . environmentId ) ) . toBe ( true ) ;
336409 expect ( yield * a . links . unlink ( box . environmentId ) ) . toBe ( false ) ;
337410 expect ( yield * a . links . get ( box . environmentId ) ) . toEqual ( Option . none ( ) ) ;
@@ -340,7 +413,7 @@ it.effect("links, reads and forgets a peer while preserving access limits and re
340413 yield * Ref . set ( b . bearers , [ ] ) ;
341414 expect ( ( yield * probe ( a ) . pipe ( Effect . flip ) ) . code ) . toBe ( "invalid_request" ) ;
342415 expect ( yield * Ref . get ( b . bearers ) ) . toHaveLength ( 0 ) ;
343- expect ( yield * linkedSessions ( b ) ) . toHaveLength ( 1 ) ;
416+ expect ( yield * linkedSessions ( b ) ) . toHaveLength ( 2 ) ;
344417 } ) ,
345418 ) . pipe ( Effect . provide ( Layer . merge ( NodeHttpServer . layerTest , NodeServices . layer ) ) ) ,
346419) ;
@@ -506,6 +579,51 @@ it.effect(
506579 . pipe ( Effect . flip ) ;
507580 expect ( lanAlternate . message ) . toContain ( "plain http" ) ;
508581
582+ for ( const url of [
583+ "file://username:[email protected] /path?token=private" , 584+ "http://username:[email protected] :3773/path?token=private" , 585+ ] ) {
586+ const invalid = yield * a . links
587+ . link ( { url, pairingCode : "unused" , access : "auto" } )
588+ . pipe ( Effect . flip ) ;
589+ expect ( invalid . message ) . not . toMatch ( / u s e r n a m e | p a s s w o r d | p r i v a t e | e x a m p l e \. t e s t / ) ;
590+ }
591+ yield * Ref . set ( b . descriptor , box ) ;
592+ const peerText = "PRIVATE-PEER-RESPONSE\n" . repeat ( 200 ) ;
593+ for ( const [ path , body , message ] of [
594+ [
595+ "/oauth/mcp/register" ,
596+ { error : "invalid_client_metadata" , error_description : peerText } ,
597+ "The peer refused to register this environment. Check its connection settings." ,
598+ ] ,
599+ [
600+ "/oauth/mcp/decision" ,
601+ { _tag : "AuthMcpApprovalError" , message : peerText } ,
602+ "The peer refused link approval. Check the pairing code and try again." ,
603+ ] ,
604+ [
605+ "/oauth/mcp/token" ,
606+ { error : "invalid_grant" , error_description : peerText } ,
607+ "The peer refused the sign-in token. Create a new pairing code and try again." ,
608+ ] ,
609+ [
610+ "/oauth/mcp/decision" ,
611+ { redirectTo : "http://localhost/?error_description=" + encodeURIComponent ( peerText ) } ,
612+ "The peer did not approve the link. Check the pairing code and try again." ,
613+ ] ,
614+ ] as const ) {
615+ yield * Ref . set (
616+ b . replies ,
617+ new Map ( [ [ path , { status : "redirectTo" in body ? 200 : 400 , body } ] ] ) ,
618+ ) ;
619+ const code = yield * b . auth . issuePairingCredential ( ) ;
620+ const failed = yield * a . links
621+ . link ( { url : b . url , pairingCode : code . credential , access : "auto" } )
622+ . pipe ( Effect . flip ) ;
623+ expect ( failed . reason ) . toBe ( "pairing_rejected" ) ;
624+ expect ( failed . message ) . toBe ( message ) ;
625+ }
626+ yield * Ref . set ( b . replies , new Map ( ) ) ;
509627 yield * Ref . set ( b . descriptor , laptop ) ;
510628 const self = yield * a . links
511629 . link ( { url : b . url , pairingCode : pairing . credential , access : "auto" } )
@@ -545,6 +663,8 @@ it.effect(
545663 } ) ,
546664 ) ;
547665 expect ( yield * probe ( a ) ) . toEqual ( result ) ;
666+ yield * Ref . set ( b . sse , true ) ;
667+ expect ( yield * probe ( a ) ) . toEqual ( result ) ;
548668
549669 yield * Ref . set (
550670 b . toolResult ,
0 commit comments