@@ -276,60 +276,12 @@ async function createOpenShellSandboxBackend(params: {
276276 remoteWorkspaceDir : params . pluginConfig . remoteWorkspaceDir ,
277277 remoteAgentWorkspaceDir : params . pluginConfig . remoteAgentWorkspaceDir ,
278278 } ) ;
279-
280- return {
281- id : "openshell" ,
282- runtimeId : sandboxName ,
283- runtimeLabel : sandboxName ,
284- workdir : params . pluginConfig . remoteWorkspaceDir ,
285- env : params . createParams . cfg . docker . env ,
286- mode : params . pluginConfig . mode ,
287- configLabel : params . pluginConfig . from ,
288- configLabelKind : "Source" ,
289- workdirValidation : "backend" ,
290- validateWorkdir : async ( workdir ) => await impl . validateWorkdir ( workdir ) ,
291- discardPreparedWorkdir : ( workdir ) => impl . discardPreparedWorkdir ( workdir ) ,
292- workdirRoots : [
293- params . pluginConfig . remoteWorkspaceDir ,
294- params . pluginConfig . remoteAgentWorkspaceDir ,
295- ] ,
296- buildExecSpec : async ( { command, workdir, env, usePty } ) => {
297- const pending = await impl . prepareExec ( { command, workdir, env, usePty } ) ;
298- return {
299- argv : pending . argv ,
300- env : buildOpenShellSshExecEnv ( ) ,
301- stdinMode : "pipe-open" ,
302- finalizeToken : pending . token ,
303- } ;
304- } ,
305- finalizeExec : async ( { token } ) => {
306- await impl . finalizeExec ( token as PendingExec | undefined ) ;
307- } ,
308- runShellCommand : async ( command ) => await impl . runRemoteShellScript ( command ) ,
309- createFsBridge : ( { sandbox } ) =>
310- params . pluginConfig . mode === "remote"
311- ? createRemoteShellSandboxFsBridge ( {
312- sandbox,
313- runtime : impl . asHandle ( ) ,
314- } )
315- : createOpenShellFsBridge ( {
316- sandbox,
317- backend : impl . asHandle ( ) ,
318- } ) ,
319- remoteWorkspaceDir : params . pluginConfig . remoteWorkspaceDir ,
320- remoteAgentWorkspaceDir : params . pluginConfig . remoteAgentWorkspaceDir ,
321- runRemoteShellScript : async ( command ) => await impl . runRemoteShellScript ( command ) ,
322- mkdirpRemotePath : async ( remotePath , signal ) => await impl . mkdirpRemotePath ( remotePath , signal ) ,
323- removeRemotePath : async ( remotePath , removeParams ) =>
324- await impl . removeRemotePath ( remotePath , removeParams ) ,
325- renameRemotePath : async ( fromRemotePath , toRemotePath , signal ) =>
326- await impl . renameRemotePath ( fromRemotePath , toRemotePath , signal ) ,
327- syncLocalPathToRemote : async ( localPath , remotePath ) =>
328- await impl . syncLocalPathToRemote ( localPath , remotePath ) ,
329- } ;
279+ return impl . asHandle ( ) ;
330280}
331281
332282class OpenShellSandboxBackendImpl {
283+ // Filesystem bridges must retain the same lifecycle owner returned by the factory.
284+ private handle : OpenShellSandboxBackend | null = null ;
333285 private ensurePromise : Promise < void > | null = null ;
334286 private preparedRemoteWorkspaceForNextExec : {
335287 workdir : string ;
@@ -348,7 +300,10 @@ class OpenShellSandboxBackendImpl {
348300 ) { }
349301
350302 asHandle ( ) : OpenShellSandboxBackend {
351- return {
303+ if ( this . handle ) {
304+ return this . handle ;
305+ }
306+ const handle : OpenShellSandboxBackend = {
352307 id : "openshell" ,
353308 runtimeId : this . params . execContext . sandboxName ,
354309 runtimeLabel : this . params . execContext . sandboxName ,
@@ -380,11 +335,11 @@ class OpenShellSandboxBackendImpl {
380335 this . params . execContext . config . mode === "remote"
381336 ? createRemoteShellSandboxFsBridge ( {
382337 sandbox,
383- runtime : this . asHandle ( ) ,
338+ runtime : handle ,
384339 } )
385340 : createOpenShellFsBridge ( {
386341 sandbox,
387- backend : this . asHandle ( ) ,
342+ backend : handle ,
388343 } ) ,
389344 runRemoteShellScript : async ( command ) => await this . runRemoteShellScript ( command ) ,
390345 mkdirpRemotePath : async ( remotePath , signal ) =>
@@ -396,6 +351,8 @@ class OpenShellSandboxBackendImpl {
396351 syncLocalPathToRemote : async ( localPath , remotePath ) =>
397352 await this . syncLocalPathToRemote ( localPath , remotePath ) ,
398353 } ;
354+ this . handle = handle ;
355+ return handle ;
399356 }
400357
401358 async prepareExec ( params : {
0 commit comments