@@ -61,6 +61,7 @@ import * as CommandReceiptStore from "../orchestration-v2/CommandReceiptStore.ts
6161import * as SourceControlRepositoryService from "../sourceControl/SourceControlRepositoryService.ts" ;
6262import * as GitVcsDriver from "../vcs/GitVcsDriver.ts" ;
6363import * as PeerForwarding from "./PeerForwarding.ts" ;
64+ import * as PeerMcpClient from "./PeerMcpClient.ts" ;
6465import * as PeerLinks from "./PeerLinks.ts" ;
6566import { descriptorOf , layerLinkingEnvironment , linkTo , servePeer } from "./PeerLinks.testkit.ts" ;
6667import * as RemoteDelegation from "./RemoteDelegation.ts" ;
@@ -121,6 +122,7 @@ interface BoxThread {
121122 readonly stops : Ref . Ref < ReadonlyArray < "interrupt" | "thread.stop" | "delegated-tasks" > > ;
122123 /** The launch starts no run, as one recorded before its run was kept. */
123124 readonly runless : Ref . Ref < boolean > ;
125+ readonly launchError : Ref . Ref < ThreadLaunch . ThreadLaunchError | null > ;
124126 /** Completed on the first interrupt there. */
125127 readonly interruptedOnce : Deferred . Deferred < void > ;
126128 readonly finished : Deferred . Deferred < void > ;
@@ -164,6 +166,7 @@ const makeBoxThread = Effect.gen(function* () {
164166 interrupted : yield * Ref . make ( 0 ) ,
165167 stops : yield * Ref . make < ReadonlyArray < "interrupt" | "thread.stop" | "delegated-tasks" > > ( [ ] ) ,
166168 runless : yield * Ref . make ( false ) ,
169+ launchError : yield * Ref . make < ThreadLaunch . ThreadLaunchError | null > ( null ) ,
167170 interruptedOnce : yield * Deferred . make < void > ( ) ,
168171 finished : yield * Deferred . make < void > ( ) ,
169172 holdRead : yield * Ref . make ( false ) ,
@@ -440,7 +443,9 @@ const boxToolkitLayer = (thread: BoxThread) => {
440443 } ) ;
441444 const layerBoxLaunches = Layer . mock ( ThreadLaunch . ThreadLaunchService ) ( {
442445 launch : ( input ) =>
443- Ref . get ( thread . runless ) . pipe (
446+ Ref . get ( thread . launchError ) . pipe (
447+ Effect . flatMap ( ( error ) => ( error === null ? Effect . void : Effect . fail ( error ) ) ) ,
448+ Effect . andThen ( Ref . get ( thread . runless ) ) ,
444449 Effect . flatMap ( ( runless ) =>
445450 Ref . set ( thread . launched , input . threadId ! ) . pipe (
446451 Effect . andThen ( Ref . set ( thread . linkOrigin , input . linkOrigin ?? null ) ) ,
@@ -821,10 +826,19 @@ const taskResult = (laptopEnv: Effect.Success<ReturnType<typeof makeLaptop>>, ta
821826 ) ,
822827 ) ;
823828
824- it . effect ( "returns the recorded task when an accepted launch loses its response " , ( ) =>
829+ it . effect ( "returns a definite launch refusal instead of an indefinitely launching task " , ( ) =>
825830 Effect . scoped (
826831 Effect . gen ( function * ( ) {
827832 const thread = yield * makeBoxThread ;
833+ yield * Ref . set (
834+ thread . launchError ,
835+ new ThreadLaunch . ThreadLaunchError ( {
836+ operation : "create-thread" ,
837+ commandId : CommandId . make ( "command:refused-launch" ) ,
838+ projectId : boxProject ,
839+ cause : new Error ( "The destination cannot create the thread." ) ,
840+ } ) ,
841+ ) ;
828842 const b = yield * serveBox ( thread ) ;
829843 const offers = yield * SubscriptionRef . make <
830844 ReadonlyArray < ProviderContinuationRequests . ProviderContinuationRequest >
@@ -833,12 +847,67 @@ it.effect("returns the recorded task when an accepted launch loses its response"
833847 const a = yield * makeLaptop ( database , offers ) ;
834848 yield * linkTo ( a . links , b , "full-access" ) ;
835849 yield * seedParent ( a ) ;
836- yield * Ref . set (
837- thread . afterLaunch ,
838- Ref . set ( thread . afterLaunch , Effect . void ) . pipe (
839- Effect . andThen ( Effect . die ( "The accepted launch response was lost." ) ) ,
850+ const refused = yield * a . call ( "delegate_task" , {
851+ task : "Run the checks there." ,
852+ target : {
853+ environmentId : box . environmentId ,
854+ projectId : boxProject ,
855+ providerInstanceId : instanceId ,
856+ model : "gpt-5.4" ,
857+ } ,
858+ mode : "async" ,
859+ clientRequestId : "refused-launch" ,
860+ } ) ;
861+ expect ( refused . isError ) . toBe ( true ) ;
862+ expect ( JSON . stringify ( refused . content ) ) . toContain ( "The operation could not be completed." ) ;
863+ const task = ( yield * a . orchestrator . getThreadProjection ( parentThreadId ) ) . subagents [ 0 ] ! ;
864+ expect ( task . status ) . toBe ( "failed" ) ;
865+ expect ( task . result ) . toBe ( "The operation could not be completed." ) ;
866+ expect ( task . completionDelivery ?. state ) . toBe ( "disposed" ) ;
867+ expect ( task . remoteChild ?. stoppedThere ) . toBe ( true ) ;
868+ expect ( task . remoteChild ?. launchError ) . toBeUndefined ( ) ;
869+ expect ( yield * Ref . get ( thread . launchIds ) ) . toEqual ( [ ] ) ;
870+ } ) ,
871+ ) . pipe ( Effect . provide ( NodeHttpServer . layerTest ) ) ,
872+ ) ;
873+
874+ it . effect ( "returns the recorded task when an accepted launch loses its response" , ( ) =>
875+ Effect . scoped (
876+ Effect . gen ( function * ( ) {
877+ const thread = yield * makeBoxThread ;
878+ const b = yield * serveBox ( thread ) ;
879+ const offers = yield * SubscriptionRef . make <
880+ ReadonlyArray < ProviderContinuationRequests . ProviderContinuationRequest >
881+ > ( [ ] ) ;
882+ const database = yield * SqlitePersistence . layerMemory . pipe ( Layer . build ) ;
883+ const linking = yield * layerLinkingEnvironment ( laptop ) . pipe ( Layer . build ) ;
884+ const client = Context . get ( linking , PeerMcpClient . PeerMcpClient ) ;
885+ let lost = false ;
886+ const a = yield * makeLaptop (
887+ database ,
888+ offers ,
889+ Context . add (
890+ linking ,
891+ PeerMcpClient . PeerMcpClient ,
892+ PeerMcpClient . PeerMcpClient . of ( {
893+ call : ( input ) =>
894+ client . call ( input ) . pipe (
895+ Effect . flatMap ( ( result ) => {
896+ if ( input . tool !== "t3_thread_launch" || lost ) return Effect . succeed ( result ) ;
897+ lost = true ;
898+ return Effect . fail (
899+ new PeerMcpClient . UncertainCall ( {
900+ code : "orchestration_error" ,
901+ message : "The linked environment stopped answering." ,
902+ } ) ,
903+ ) ;
904+ } ) ,
905+ ) ,
906+ } ) ,
840907 ) ,
841908 ) ;
909+ yield * linkTo ( a . links , b , "full-access" ) ;
910+ yield * seedParent ( a ) ;
842911 const delegated = yield * delegateToBox ( a ) ;
843912 expect ( delegated . taskId ) . toBeDefined ( ) ;
844913 const recorded = yield * a . orchestrator . getThreadProjection ( parentThreadId ) ;
0 commit comments