[Bug]: AgentRunDataPlaneHttp 使用错误的 API 版本前缀 /2025-09-10 导致 "Invalid request uri"
Describe the bug
AgentRunDataPlaneHttp 在构造数据面 API 请求 URL 时错误地添加了 /2025-09-10/ 版本路径前缀,但 AgentRun 沙箱数据面 API 的实际端点并不包含此版本前缀,导致所有沙箱操作(创建、查询、删除)均返回 400 Bad Request: Invalid request uri。
To Reproduce
使用 AgentRunFilesystemSpec 配置沙箱并调用 Agent:
@Bean
public SandboxFilesystemSpec sandboxFilesystemSpec() {
return new AgentRunFilesystemSpec()
.apiKey("ak_xxx")
.dataPlaneBaseUrl("https://{accountId}.agentrun-data.cn-hangzhou.aliyuncs.com")
.templateName("sandbox-code-xxx")
.mcpServerUrl("https://{accountId}.agentrun-data.cn-hangzhou.aliyuncs.com/templates/sandbox-code-xxx/mcp")
.sandboxIdleTimeoutSeconds(600)
.isolationScope(IsolationScope.USER);
}
然后通过 HarnessAgent.call() 发起对话请求。
Expected behavior
框架正确调用数据面 API,成功创建/获取沙箱并执行代码。
Error messages
io.agentscope.harness.agent.sandbox.SandboxException$SandboxRuntimeException:
AgentRun HTTP 400 Bad Request: {"code":"ERR_BAD_REQUEST","requestId":"e75ef565-...","message":"Invalid request uri"}
at io.agentscope.extensions.sandbox.agentrun.AgentRunDataPlaneHttp.getJson(AgentRunDataPlaneHttp.java:209)
at io.agentscope.extensions.sandbox.agentrun.AgentRunDataPlaneHttp.lambda$getSandbox$1(AgentRunDataPlaneHttp.java:102)
at io.agentscope.extensions.sandbox.agentrun.AgentRunDataPlaneHttp.getSandbox(AgentRunDataPlaneHttp.java:102)
at io.agentscope.extensions.sandbox.agentrun.AgentRunSandbox.ensureSandbox(AgentRunSandbox.java:218)
at io.agentscope.extensions.sandbox.agentrun.AgentRunSandbox.start(AgentRunSandbox.java:72)
Root Cause
AgentRunDataPlaneHttp.java 中三个方法的 URL 均硬编码了 /2025-09-10/ 前缀:
- 第 96 行 —
createSandbox:/2025-09-10/sandboxes - 第 101 行 —
getSandbox:/2025-09-10/sandboxes/{id} - 第 110 行 —
deleteSandbox:/2025-09-10/sandboxes/{id}
而 AgentRun 数据面 API 的实际路径为:
POST /sandboxesGET /sandboxes/{sandboxId}DELETE /sandboxes/{sandboxId}
对照阿里云官方文档 Code Interpreter API 参考,所有数据面 API 端点均无版本前缀。
Proposed Fix
将 AgentRunDataPlaneHttp.java 中三处 /2025-09-10/sandboxes 改为 /sandboxes:
- String url = opt.getResolvedDataPlaneBaseUrl() + "/2025-09-10/sandboxes";
+ String url = opt.getResolvedDataPlaneBaseUrl() + "/sandboxes";
- String url = opt.getResolvedDataPlaneBaseUrl() + "/2025-09-10/sandboxes/" + sandboxId;
+ String url = opt.getResolvedDataPlaneBaseUrl() + "/sandboxes/" + sandboxId;
涉及文件:
agentscope-extensions/agentscope-extensions-sandbox/agentscope-extensions-sandbox-agentrun/src/main/java/io/agentscope/extensions/sandbox/agentrun/AgentRunDataPlaneHttp.java
Environment
- AgentScope-Java Version:
2.0.0-RC3 - Java Version:
21 - OS: Windows 11
Additional context
已通过独立 HTTP 测试验证无 /2025-09-10 前缀的 API 路径可正常工作(创建沙箱、执行代码、文件操作、命令执行等全部通过)。测试代码见 AgentRunSandboxApiTest.java,测试用例全部通过。
/**
-
AgentRun Sandbox Code Interpreter API 测试用例。
-
覆盖:沙箱生命周期、上下文管理、文件系统、代码执行、终端命令、进程管理。 */ @DisplayName("AgentRun Sandbox Code Interpreter API 测试") @TestMethodOrder(MethodOrderer.OrderAnnotation.class) class AgentRunSandboxApiTest {
private static final String API_KEY = "ak_**"; private static final String BASE_URL = "https://{accountId}.agentrun-data.cn-hangzhou.aliyuncs.com"; private static final String PARENT_ID = "{accountId}";
private static final HttpClient http = HttpClient.newBuilder() .connectTimeout(Duration.ofSeconds(60)) .build();
private static final ObjectMapper json = new ObjectMapper();
private static final String sandboxId = "***"; private static String contextId;
/**
- 解包:有 data 字段取 data,否则返回原节点 */ private static JsonNode unwrap(JsonNode node) { return node.has("data") ? node.get("data") : node; }
/**
- 成功状态码:2xx */ private static boolean ok(int code) { return code >= 200 && code < 300; }
private HttpRequest.Builder req(String path) { return HttpRequest.newBuilder() .uri(URI.create(BASE_URL + path)) .header("X-Acs-Parent-Id", PARENT_ID) .header("X-API-Key", API_KEY); }
// ═══════════════ 沙箱生命周期 ═══════════════
@Test @Order(1) @DisplayName("检查 Sandbox 实例状态") void checkSandbox() throws Exception { HttpResponse
resp = http.send( req("/sandboxes/" + sandboxId).GET().build(), HttpResponse.BodyHandlers.ofString()); System.out.println("[检查 Sandbox] " + resp.statusCode() + ": " + resp.body()); assertEquals(200, resp.statusCode(), "Sandbox 应存在, 实际: " + resp.statusCode()); JsonNode d = unwrap(json.readTree(resp.body())); assertEquals(sandboxId, d.get("sandboxId").asText()); assertEquals("READY", d.get("status").asText(), "Sandbox 状态应为 READY"); assertEquals("CodeInterpreter", d.get("templateType").asText());}
// ═══════════════ 上下文管理 ═══════════════
@Test @Order(10) @DisplayName("创建执行上下文") void createContext() throws Exception { String body = json.writeValueAsString(json.createObjectNode() .put("language", "python"));
HttpResponse<String> resp = http.send( req("/sandboxes/" + sandboxId + "/contexts") .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(body)) .build(), HttpResponse.BodyHandlers.ofString()); System.out.println("[创建上下文] " + resp.statusCode() + ": " + resp.body()); assertTrue(ok(resp.statusCode()), "应为 2xx, 实际: " + resp.statusCode()); JsonNode d = unwrap(json.readTree(resp.body())); contextId = d.get("id").asText(); assertFalse(contextId.isEmpty(), "contextId 不应为空"); assertEquals("python", d.get("language").asText());}
@Test @Order(11) @DisplayName("列出所有上下文") void listContexts() throws Exception { HttpResponse
resp = http.send( req("/sandboxes/" + sandboxId + "/contexts").GET().build(), HttpResponse.BodyHandlers.ofString()); System.out.println("[列出上下文] " + resp.statusCode() + ": " + resp.body()); assertTrue(ok(resp.statusCode()), "应为 2xx, 实际: " + resp.statusCode());}
@Test @Order(12) @DisplayName("获取上下文详情") void getContext() throws Exception { HttpResponse
resp = http.send( req("/sandboxes/" + sandboxId + "/contexts/" + contextId).GET().build(), HttpResponse.BodyHandlers.ofString()); System.out.println("[获取上下文] " + resp.statusCode() + ": " + resp.body()); assertEquals(200, resp.statusCode()); JsonNode d = unwrap(json.readTree(resp.body())); assertEquals(contextId, d.get("id").asText());}
// ═══════════════ 代码执行 ═══════════════
@Test @Order(20) @DisplayName("同步执行 Python 代码") void executeCode() throws Exception { String body = json.writeValueAsString(json.createObjectNode() .put("contextId", contextId) .put("code", "print('Hello Sandbox!'); x = 1 + 1"));
HttpResponse<String> resp = http.send( req("/sandboxes/" + sandboxId + "/contexts/execute") .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(body)) .build(), HttpResponse.BodyHandlers.ofString()); System.out.println("[执行代码] " + resp.statusCode() + ": " + resp.body()); assertEquals(200, resp.statusCode()); JsonNode d = unwrap(json.readTree(resp.body())); assertTrue(d.has("results"), "应有 results 数组"); // 检查 stdout 输出 boolean foundHello = false; for (JsonNode r : d.get("results")) { if ("stdout".equals(r.get("type").asText()) && r.get("text").asText().contains("Hello Sandbox")) { foundHello = true; } } assertTrue(foundHello, "stdout 应包含 'Hello Sandbox!'");}
// ═══════════════ 文件系统操作 ═══════════════
@Test @Order(30) @DisplayName("写入文件") void writeFile() throws Exception { String body = json.writeValueAsString(json.createObjectNode() .put("path", "hello.txt") .put("content", "Hello World from Sandbox!"));
HttpResponse<String> resp = http.send( req("/sandboxes/" + sandboxId + "/files") .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(body)) .build(), HttpResponse.BodyHandlers.ofString()); System.out.println("[写入文件] " + resp.statusCode() + ": " + resp.body()); assertTrue(ok(resp.statusCode()), "应为 2xx, 实际: " + resp.statusCode()); assertTrue(json.readTree(resp.body()).has("path"), "响应应有 path 字段");}
@Test @Order(31) @DisplayName("读取文件内容") void readFile() throws Exception { HttpResponse
resp = http.send( req("/sandboxes/" + sandboxId + "/files?path=hello.txt").GET().build(), HttpResponse.BodyHandlers.ofString()); System.out.println("[读取文件] " + resp.statusCode() + ": " + resp.body()); assertEquals(200, resp.statusCode()); JsonNode d = unwrap(json.readTree(resp.body())); assertTrue(d.get("content").asText().contains("Hello World"), "文件内容应包含 'Hello World'");}
@Test @Order(32) @DisplayName("列出目录内容") void listDirectory() throws Exception { HttpResponse
resp = http.send( req("/sandboxes/" + sandboxId + "/filesystem?path=/home/user&depth=1").GET().build(), HttpResponse.BodyHandlers.ofString()); System.out.println("[列出目录] " + resp.statusCode() + ": " + resp.body()); assertTrue(ok(resp.statusCode()), "应为 2xx");}
@Test @Order(33) @DisplayName("获取文件信息 (stat)") void statFile() throws Exception { HttpResponse
resp = http.send( req("/sandboxes/" + sandboxId + "/filesystem/stat?path=hello.txt").GET().build(), HttpResponse.BodyHandlers.ofString()); System.out.println("[文件信息] " + resp.statusCode() + ": " + resp.body()); assertEquals(200, resp.statusCode()); JsonNode d = unwrap(json.readTree(resp.body())); JsonNode entry = d.has("entry") ? d.get("entry") : d; assertEquals("file", entry.get("type").asText());}
@Test @Order(34) @DisplayName("创建目录") void createDirectory() throws Exception { String body = json.writeValueAsString(json.createObjectNode() .put("path", "/home/user/test_dir"));
HttpResponse<String> resp = http.send( req("/sandboxes/" + sandboxId + "/filesystem/mkdir") .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(body)) .build(), HttpResponse.BodyHandlers.ofString()); System.out.println("[创建目录] " + resp.statusCode() + ": " + resp.body()); assertTrue(ok(resp.statusCode()), "应为 2xx"); JsonNode d = unwrap(json.readTree(resp.body())); JsonNode entry = d.has("entry") ? d.get("entry") : d; assertEquals("directory", entry.get("type").asText());}
@Test @Order(35) @DisplayName("移动/重命名文件") void moveFile() throws Exception { String body = json.writeValueAsString(json.createObjectNode() .put("source", "hello.txt") .put("destination", "hello_renamed.txt"));
HttpResponse<String> resp = http.send( req("/sandboxes/" + sandboxId + "/filesystem/move") .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(body)) .build(), HttpResponse.BodyHandlers.ofString()); System.out.println("[移动文件] " + resp.statusCode() + ": " + resp.body()); assertTrue(ok(resp.statusCode()), "应为 2xx");}
@Test @Order(36) @DisplayName("删除文件") void removeFile() throws Exception { String body = json.writeValueAsString(json.createObjectNode() .put("path", "hello_renamed.txt"));
HttpResponse<String> resp = http.send( req("/sandboxes/" + sandboxId + "/filesystem/remove") .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(body)) .build(), HttpResponse.BodyHandlers.ofString()); System.out.println("[删除文件] " + resp.statusCode() + ": " + resp.body()); assertTrue(ok(resp.statusCode()), "应为 2xx");}
// ═══════════════ 终端命令 ═══════════════
@Test @Order(40) @DisplayName("同步执行 Shell 命令") void executeCommand() throws Exception { String body = json.writeValueAsString(json.createObjectNode() .put("command", "echo 'hello from shell' && uname -s"));
HttpResponse<String> resp = http.send( req("/sandboxes/" + sandboxId + "/processes/cmd") .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(body)) .build(), HttpResponse.BodyHandlers.ofString()); System.out.println("[执行命令] " + resp.statusCode() + ": " + resp.body()); assertEquals(200, resp.statusCode()); JsonNode d = unwrap(json.readTree(resp.body())); assertEquals("completed", d.get("status").asText()); assertTrue(d.get("result").get("stdout").asText().contains("hello from shell")); assertEquals(0, d.get("result").get("exitCode").asInt());}
// ═══════════════ 进程管理 ═══════════════
@Test @Order(50) @DisplayName("列出所有进程") void listProcesses() throws Exception { HttpResponse
resp = http.send( req("/sandboxes/" + sandboxId + "/processes").GET().build(), HttpResponse.BodyHandlers.ofString()); System.out.println("[列出进程] " + resp.statusCode() + ": " + resp.body()); assertTrue(ok(resp.statusCode()), "应为 2xx");}
// ═══════════════ 清理 ═══════════════
@Test @Order(90) @DisplayName("删除上下文") void deleteContext() throws Exception { HttpResponse
resp = http.send( req("/sandboxes/" + sandboxId + "/contexts/" + contextId).DELETE().build(), HttpResponse.BodyHandlers.ofString()); System.out.println("[删除上下文] " + resp.statusCode()); assertTrue(ok(resp.statusCode()), "应为 2xx, 实际: " + resp.statusCode());} }
我提交了一个PR,https://github.com/agentscope-ai/agentscope-java/pull/1891
就起来的早