@@ -16,7 +16,7 @@ import {
1616 validateWorkspaceFileWriteTarget ,
1717 writeWorkspaceFileByPath ,
1818} from '@/lib/copilot/vfs/resource-writer'
19- import { isRemoteSandboxEnabled } from '@/lib/core/config/env-flags'
19+ import { isMothershipSandboxEnabled , isRemoteSandboxEnabled } from '@/lib/core/config/env-flags'
2020import {
2121 createTimeoutAbortController ,
2222 isTimeoutAbortReason ,
@@ -1687,6 +1687,16 @@ export const POST = withRouteHandler(async (req: NextRequest) => {
16871687 logger . warn ( `[${ requestId } ] Unauthorized function execution attempt` )
16881688 return NextResponse . json ( { error : auth . error || 'Unauthorized' } , { status : 401 } )
16891689 }
1690+ const usesMothershipSandbox = auth . sandboxProfile === 'mothership'
1691+ if ( usesMothershipSandbox && ! isMothershipSandboxEnabled ) {
1692+ return NextResponse . json (
1693+ { success : false , error : 'Mothership code sandbox is not configured' } ,
1694+ { status : 503 }
1695+ )
1696+ }
1697+ const remoteSandboxEnabled = usesMothershipSandbox
1698+ ? isMothershipSandboxEnabled
1699+ : isRemoteSandboxEnabled
16901700
16911701 executionDeadlineAt = parseExecutionDeadlineHeader ( req . headers )
16921702 includePrivateResolvedSecretNames = requestsPrivateToolMetadata (
@@ -1886,7 +1896,7 @@ export const POST = withRouteHandler(async (req: NextRequest) => {
18861896 }
18871897
18881898 if ( lang === CodeLanguage . Shell ) {
1889- if ( ! isRemoteSandboxEnabled ) {
1899+ if ( ! remoteSandboxEnabled ) {
18901900 throw new Error (
18911901 'Shell execution requires a remote code sandbox to be enabled. Please contact your administrator to enable it.'
18921902 )
@@ -1901,7 +1911,7 @@ export const POST = withRouteHandler(async (req: NextRequest) => {
19011911 }
19021912
19031913 logger . info ( `[${ requestId } ] E2B shell execution` , {
1904- enabled : isRemoteSandboxEnabled ,
1914+ enabled : remoteSandboxEnabled ,
19051915 hasApiKey : Boolean ( process . env . E2B_API_KEY ) ,
19061916 envVarCount : Object . keys ( shellEnvs ) . length ,
19071917 } )
@@ -1924,6 +1934,7 @@ export const POST = withRouteHandler(async (req: NextRequest) => {
19241934 outputSandboxPaths,
19251935 workspaceId,
19261936 sandboxId : selectedSandboxId ,
1937+ ...( usesMothershipSandbox ? { sandboxKind : 'mothership' as const } : { } ) ,
19271938 signal : executionSignal ,
19281939 } )
19291940 const executionTime = Date . now ( ) - execStart
@@ -1971,22 +1982,23 @@ export const POST = withRouteHandler(async (req: NextRequest) => {
19711982 )
19721983 }
19731984
1974- if ( lang === CodeLanguage . Python && ! isRemoteSandboxEnabled ) {
1985+ if ( lang === CodeLanguage . Python && ! remoteSandboxEnabled ) {
19751986 throw new Error (
19761987 'Python execution requires a remote code sandbox to be enabled. Please contact your administrator to enable it, or use JavaScript instead.'
19771988 )
19781989 }
19791990
1980- if ( lang === CodeLanguage . JavaScript && hasImports && ! isRemoteSandboxEnabled ) {
1991+ if ( lang === CodeLanguage . JavaScript && hasImports && ! remoteSandboxEnabled ) {
19811992 throw new Error (
19821993 'JavaScript code with import statements requires a remote code sandbox to be enabled. Please remove the import statements, or contact your administrator to enable it.'
19831994 )
19841995 }
19851996
19861997 const useRemoteSandbox =
1987- isRemoteSandboxEnabled &&
1988- ! isCustomTool &&
1989- ( lang === CodeLanguage . Python || ( lang === CodeLanguage . JavaScript && hasImports ) )
1998+ usesMothershipSandbox ||
1999+ ( remoteSandboxEnabled &&
2000+ ! isCustomTool &&
2001+ ( lang === CodeLanguage . Python || ( lang === CodeLanguage . JavaScript && hasImports ) ) )
19902002
19912003 if ( useRemoteSandbox && containsLargeValueRef ( contextVariables ) ) {
19922004 throw new Error (
@@ -2004,7 +2016,7 @@ export const POST = withRouteHandler(async (req: NextRequest) => {
20042016 ! useRemoteSandbox &&
20052017 ( outputSandboxPaths . length > 0 || outputSandboxPath || _sandboxFiles ?. length )
20062018 ) {
2007- const remediation = ! isRemoteSandboxEnabled
2019+ const remediation = ! remoteSandboxEnabled
20082020 ? "No remote code sandbox is enabled on this deployment, so there is no sandbox filesystem for any language. Pass input data via params and return output as the code's return value with outputs.files[].path (no sandboxPath)."
20092021 : isCustomTool
20102022 ? "custom tools always run in the isolated JavaScript VM, which has no sandbox filesystem. Pass input data via params and return output as the code's return value."
@@ -2022,7 +2034,7 @@ export const POST = withRouteHandler(async (req: NextRequest) => {
20222034
20232035 if ( useRemoteSandbox ) {
20242036 logger . info ( `[${ requestId } ] E2B status` , {
2025- enabled : isRemoteSandboxEnabled ,
2037+ enabled : remoteSandboxEnabled ,
20262038 hasApiKey : Boolean ( process . env . E2B_API_KEY ) ,
20272039 language : lang ,
20282040 } )
@@ -2086,6 +2098,7 @@ export const POST = withRouteHandler(async (req: NextRequest) => {
20862098 outputSandboxPaths,
20872099 workspaceId,
20882100 sandboxId : selectedSandboxId ,
2101+ ...( usesMothershipSandbox ? { sandboxKind : 'mothership' as const } : { } ) ,
20892102 signal : executionSignal ,
20902103 } )
20912104 const executionTime = Date . now ( ) - execStart
@@ -2172,6 +2185,7 @@ export const POST = withRouteHandler(async (req: NextRequest) => {
21722185 outputSandboxPaths,
21732186 workspaceId,
21742187 sandboxId : selectedSandboxId ,
2188+ ...( usesMothershipSandbox ? { sandboxKind : 'mothership' as const } : { } ) ,
21752189 signal : executionSignal ,
21762190 } )
21772191 const executionTime = Date . now ( ) - execStart
0 commit comments