agentscope-runtime-java icon indicating copy to clipboard operation
agentscope-runtime-java copied to clipboard

reactAgent 使用远程沙箱问题,求指导

Open Renwukun opened this issue 5 months ago • 5 comments

reactAgent 使用远程沙箱问题。

  • 使用远程沙箱写执行写文件操作,报错!
  • 我的docker服务没有开启2375端口,生产环境因为安全原因也不会开放这个端口!

第一步,使用runtime-sandbox-server启动沙箱服务,服务正常。

runtime-sandbox-server

INFO:     Started server process [19265]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO /Users/xxx/miniforge3/envs/python3.11/lib/python3.11/site-packages/agentscope_runtime/sandbox/manager/sandbox_manager.py:464 | 2026-06-04 10:54:32 | Watcher started, interval=1s
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     127.0.0.1:60843 - "GET /health HTTP/1.1" 200 OK

第二步,创建 reactAgent并使用沙箱

	@BeforeAll
		static void setUp() {
			
			  。。。。。。
	                 // 不指定远程沙箱是可行的。docker ps也是可以正常看到创建的沙箱容器。
			// ManagerConfig managerConfig = ManagerConfig.builder().build();

			// 指定远程沙箱报错!是什么原因?
			ManagerConfig managerConfig = ManagerConfig.builder().baseUrl("http://127.0.0.1:8000").build();

		
			sandboxService = new SandboxService(managerConfig);
			sandboxService.start();
	}

    @Test
	public void testFsSandboxTools() throws Exception {
		FilesystemSandbox sandbox = new FilesystemSandbox(sandboxService, "testUser", "testSession");

		ReactAgent fsSandboxToolAgent = ReactAgent.builder()
			.name("tool_test_agent")
			.model(chatModel)
			.chatOptions(chatOptions)
			.description("An agent that can use tools to answer questions.")
			.instruction("You are a intelligent agent that can use tools to perform tasks.")
			.tools(List.of(ToolkitInit.WriteFileTool(sandbox)))
			.build();

		     。。。。。。
}

报错信息如下

11:15:25.461 [main] INFO io.agentscope.runtime.sandbox.manager.SandboxService -- Initialized SandboxService in remote mode with base URL: http://127.0.0.1:8080
11:15:25.463 [main] INFO io.agentscope.runtime.sandbox.manager.SandboxService -- Initializing SandboxService with container manager: DOCKER
11:15:25.463 [main] INFO io.agentscope.runtime.sandbox.manager.utils.PortManager -- PortManager initialized with port range: 49152-59152
11:15:25.466 [main] INFO io.agentscope.runtime.sandbox.manager.client.container.docker.DockerClient -- Connecting to Docker at tcp://localhost:2375 without TLS
11:15:25.600 [main] WARN io.agentscope.runtime.sandbox.manager.client.container.docker.DockerClient -- Failed to connect to Docker using configured host (localhost:2375): com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.HttpHostConnectException: Connect to http://localhost:2375 failed: Connection refused
11:15:25.600 [main] INFO io.agentscope.runtime.sandbox.manager.client.container.docker.DockerClient -- Falling back to default Docker configuration
11:15:25.666 [main] INFO io.agentscope.runtime.sandbox.manager.client.container.docker.DockerClientStarter -- Docker client created
11:15:25.666 [main] INFO io.agentscope.runtime.sandbox.manager.SandboxService -- SandboxService started.
11:15:28.759 [main] INFO io.agentscope.runtime.sandbox.manager.SandboxService -- Creating container in remote mode via RemoteHttpClient
11:15:28.763 [main] INFO io.agentscope.runtime.sandbox.manager.SandboxService -- Getting sandbox info in remote mode via RemoteHttpClient
11:15:28.763 [main] ERROR io.agentscope.runtime.sandbox.box.Sandbox -- Failed to initialize sandbox: Sandbox null is not healthy: null (through reference chain: io.agentscope.runtime.sandbox.box.FilesystemSandbox["desktopUrl"])
11:15:28.763 [main] ERROR io.agentscope.runtime.sandbox.tools.fs.WriteFileTool -- Write File Error: Failed to initialize sandbox
11:15:31.391 [main] INFO io.agentscope.runtime.sandbox.manager.SandboxService -- Creating container in remote mode via RemoteHttpClient
11:15:31.392 [main] INFO io.agentscope.runtime.sandbox.manager.SandboxService -- Getting sandbox info in remote mode via RemoteHttpClient
11:15:31.393 [main] ERROR io.agentscope.runtime.sandbox.box.Sandbox -- Failed to initialize sandbox: Sandbox null is not healthy: null (through reference chain: io.agentscope.runtime.sandbox.box.FilesystemSandbox["desktopUrl"])
11:15:31.393 [main] ERROR io.agentscope.runtime.sandbox.tools.fs.WriteFileTool -- Write File Error: Failed to initialize sandbox
11:15:34.306 [main] INFO io.agentscope.runtime.sandbox.manager.SandboxService -- Creating container in remote mode via RemoteHttpClient
11:15:34.307 [main] INFO io.agentscope.runtime.sandbox.manager.SandboxService -- Getting sandbox info in remote mode via RemoteHttpClient
11:15:34.307 [main] ERROR io.agentscope.runtime.sandbox.box.Sandbox -- Failed to initialize sandbox: Sandbox null is not healthy: null (through reference chain: io.agentscope.runtime.sandbox.box.FilesystemSandbox["desktopUrl"])
11:15:34.307 [main] ERROR io.agentscope.runtime.sandbox.tools.fs.WriteFileTool -- Write File Error: Failed to initialize sandbox
11:15:38.245 [main] INFO io.agentscope.runtime.sandbox.manager.SandboxService -- Creating container in remote mode via RemoteHttpClient
11:15:38.246 [main] INFO io.agentscope.runtime.sandbox.manager.SandboxService -- Getting sandbox info in remote mode via RemoteHttpClient
11:15:38.246 [main] ERROR io.agentscope.runtime.sandbox.box.Sandbox -- Failed to initialize sandbox: Sandbox null is not healthy: null (through reference chain: io.agentscope.runtime.sandbox.box.FilesystemSandbox["desktopUrl"])
11:15:38.246 [main] ERROR io.agentscope.runtime.sandbox.tools.fs.WriteFileTool -- Write File Error: Failed to initialize sandbox
=== Write File Sandbox Agent Tool Test ===

Renwukun avatar Jun 04 '26 03:06 Renwukun

你竟然能用runtime-sandbox-server,我安装后提示接口404,后来ai经过一顿研究,发现接口都对不上,让我用java版本的,倒是可以了,也遇到你这个错误,应该也是FilesystemSandbox吧,ai给我重新写了个SandboxService就解决了

noday avatar Jun 30 '26 10:06 noday

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Sandbox null is not healthy: null (through reference chain: io.agentscope.runtime.sandbox.box.FilesystemSandbox["desktopUrl"]) at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:402) at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:361) at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:323) at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:778) at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:184) at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:502) at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:341) at com.fasterxml.jackson.databind.ObjectMapper._writeValueAndClose(ObjectMapper.java:4811) at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:4052) at io.agentscope.runtime.sandbox.manager.SandboxService.createContainer(SandboxService.java:96) at io.agentscope.runtime.sandbox.box.Sandbox.initializeSandbox(Sandbox.java:134) ... 3 more Caused by: java.lang.RuntimeException: Sandbox null is not healthy: null at io.agentscope.runtime.sandbox.box.GuiMixin.getDesktopUrl(GuiMixin.java:49) at io.agentscope.runtime.sandbox.box.FilesystemSandbox.getDesktopUrl(FilesystemSandbox.java:85) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:688) at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:770) 官方来解决一下

noday avatar Jun 30 '26 10:06 noday

你竟然能用runtime-sandbox-server,我安装后提示接口404,后来ai经过一顿研究,发现接口都对不上,让我用java版本的,倒是可以了,也遇到你这个错误,应该也是FilesystemSandbox吧,ai给我重新写了个SandboxService就解决了

我用的是python版本的runtime-sandbox-server启动的沙箱服务,调用方服务是使用spring ai alibaba写的一个智能体服务。 最后没找到问题,就没使用沙箱,我那是个固定的业务,执行一个固定的skills,skills script 脚本需要的依赖也是固定的,就直接在调用方服务镜像里安装了所有需要的依赖,固定了shell执行路径,应该能保证基本安全。看agent scope官网,好像也可以使用其他沙箱环境,没仔细研究。

Renwukun avatar Jun 30 '26 11:06 Renwukun

我也是pip安装的runtime-sandbox-server,启动后,跟agentscope-runtime-sandbox-core1.0.2的接口根本对不上,然后用这个项目里的web启动了个server才对应上接口。但就是报错FilesystemSandbox["desktopUrl"],序列化有问题。继承SandboxService重写里面的方法,序列化时忽略desktopUrl

public class MySandboxService extends SandboxService {

	private static final Logger log = LoggerFactory.getLogger(MySandboxService .class);

	private final ManagerConfig managerConfig;
	private final RemoteHttpClient remoteClient;

	public MySandboxService (ManagerConfig managerConfig) {
		super(managerConfig);
		this.managerConfig = managerConfig;
		String baseUrl = managerConfig.getBaseUrl();
		if (baseUrl != null && !baseUrl.isBlank()) {
			this.remoteClient = new RemoteHttpClient(baseUrl, managerConfig.getBearerToken());
		}
		else {
			this.remoteClient = null;
		}
	}

	@Override
	public ContainerModel createContainer(Sandbox sandbox) throws JsonProcessingException {
		if (remoteClient == null) {
			return super.createContainer(sandbox);
		}
		log.info("Creating container in remote mode via MySandboxService , type={}", sandbox.getSandboxType());
		String sandboxJson = SandboxJsonSupport.toCreateContainerJson(sandbox);/////////这里忽略
		Map<String, Object> request = Map.of("sandbox", sandboxJson);
		Object result = remoteClient.makeRequest(
				RequestMethod.POST,
				"/sandbox/createContainer",
				request,
				"data");
		if (result instanceof Map<?, ?> resultMap) {
			@SuppressWarnings("unchecked")
			ContainerModel containerModel = ContainerModel.fromMap((Map<String, Object>) resultMap);
			managerConfig.getSandboxMap().addSandbox(
					new SandboxKey(sandbox.getUserId(), sandbox.getSessionId(), sandbox.getSandboxType()),
					containerModel);
			return containerModel;
		}
		return null;
	}

}

noday avatar Jul 01 '26 01:07 noday

我也是pip安装的runtime-sandbox-server,启动后,跟agentscope-runtime-sandbox-core1.0.2的接口根本对不上,然后用这个项目里的web启动了个server才对应上接口。但就是报错FilesystemSandbox["desktopUrl"],序列化有问题。继承SandboxService重写里面的方法,序列化时忽略desktopUrl

public class MySandboxService extends SandboxService {

	private static final Logger log = LoggerFactory.getLogger(MySandboxService .class);

	private final ManagerConfig managerConfig;
	private final RemoteHttpClient remoteClient;

	public MySandboxService (ManagerConfig managerConfig) {
		super(managerConfig);
		this.managerConfig = managerConfig;
		String baseUrl = managerConfig.getBaseUrl();
		if (baseUrl != null && !baseUrl.isBlank()) {
			this.remoteClient = new RemoteHttpClient(baseUrl, managerConfig.getBearerToken());
		}
		else {
			this.remoteClient = null;
		}
	}

	@Override
	public ContainerModel createContainer(Sandbox sandbox) throws JsonProcessingException {
		if (remoteClient == null) {
			return super.createContainer(sandbox);
		}
		log.info("Creating container in remote mode via MySandboxService , type={}", sandbox.getSandboxType());
		String sandboxJson = SandboxJsonSupport.toCreateContainerJson(sandbox);/////////这里忽略
		Map<String, Object> request = Map.of("sandbox", sandboxJson);
		Object result = remoteClient.makeRequest(
				RequestMethod.POST,
				"/sandbox/createContainer",
				request,
				"data");
		if (result instanceof Map<?, ?> resultMap) {
			@SuppressWarnings("unchecked")
			ContainerModel containerModel = ContainerModel.fromMap((Map<String, Object>) resultMap);
			managerConfig.getSandboxMap().addSandbox(
					new SandboxKey(sandbox.getUserId(), sandbox.getSessionId(), sandbox.getSandboxType()),
					containerModel);
			return containerModel;
		}
		return null;
	}

}

我按照你的实现,还是会报这个错误,我使用的是colima管理的docker环境,不清楚是否和这个有关

Renwukun avatar Jul 03 '26 09:07 Renwukun