Merge pull request '优化 Agent' (#8) from codex/optimize-agent-tool-loop into main
Verify control plane / verify (push) Canceled after 0s

Reviewed-on: http://git.xhmcl.com/xhgit/webup-cloud/pulls/8
This commit was merged in pull request #8.
This commit is contained in:
xhgit committed 2026-11-08 01:01:17 +00:00
commit a63f6dc813
7 files changed
+110 -7

No files matched your search

+5 -1
View File
@@ -98,4 +98,8 @@ npm run build
- [操作与安全说明](docs/OPERATIONS.md)
- [私有化商业版方案](docs/SELF-HOSTED-EDITION.md)
- [插件与主题开发文档](frontend/public/extension-development.md)
- [实施状态与测试边界](docs/TEST-REPORT.md)
- [实施状态与测试边界](docs/TEST-REPORT.md)
## 参考的开源项目
- [Anthropic Claude Quickstarts](https://github.com/anthropics/claude-quickstarts/tree/4a4224bdf0e9cfb3332167b69c90457403813625)(MIT)。移植了 [Computer Use 示例](https://github.com/anthropics/claude-quickstarts/blob/4a4224bdf0e9cfb3332167b69c90457403813625/computer-use-demo/computer_use_demo/loop.py) 中同批工具遇错跳过和返回失败结果的逻辑,并适配 WebUp Cloud 的 Java Worker。原始版权和许可保存在 [第三方许可文件](backend/src/main/resources/META-INF/anthropic-claude-quickstarts-LICENSE.txt)。
@@ -13,6 +13,10 @@ import org.springframework.stereotype.Component;
@EnableScheduling
@ConditionalOnProperty(name = "app.worker-enabled", havingValue = "true")
public class AgentWorker {
// Adapted from claude-quickstarts computer_use_demo/loop.py (4a4224bdf0e9).
// See META-INF/anthropic-claude-quickstarts-LICENSE.txt.
private static final String NOT_EXECUTED_ERROR =
"Not executed: an earlier tool in this turn failed.";
private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(AgentWorker.class);
private final TaskService tasks;
private final ModelGateway gateway;
@@ -258,18 +262,27 @@ public class AgentWorker {
tasks.finish(id, lease, "PAUSED", "工具输出达到模型上限,费用已结算,未执行本轮工具;请检查模型输出上限后继续", false);
return;
}
boolean earlierToolFailed = false;
for (var call : result.calls()) {
if ("CANCEL".equals(tasks.config(id).get("requested_action"))) {
tasks.finish(id, lease, "CANCELLED", "用户取消,未继续执行后续工具", true);
return;
}
if (earlierToolFailed) {
tasks.tool(
id,
lease,
call,
json.createObjectNode().put("error", NOT_EXECUTED_ERROR).toString());
continue;
}
tasks.heartbeat(id, lease);
external = call.argumentError() == null;
String output =
call.argumentError() == null
? sandbox.execute(project, call)
: json.writeValueAsString(Map.of("error", call.argumentError()));
tasks.tool(id, lease, call, output);
earlierToolFailed = tasks.tool(id, lease, call, output);
}
external = false;
}
@@ -111,7 +111,7 @@ public class ModelGateway {
ArrayNode messages = body.putArray("messages");
for (JsonNode m : history) {
ObjectNode copy = ((ObjectNode) m).deepCopy();
copy.remove(List.of("native", "nativeType"));
copy.remove(List.of("native", "nativeType", "is_error"));
messages.add(copy);
}
if (withTools) body.set("tools", definitions);
@@ -135,6 +135,7 @@ public class ModelGateway {
block.put("type", "tool_result");
block.put("tool_use_id", m.path("tool_call_id").asText());
block.put("content", m.path("content").asText());
if (m.path("is_error").asBoolean(false)) block.put("is_error", true);
} else if (role.equals("assistant") && m.path("nativeType").asText().equals(type)) {
content.addAll((ArrayNode) m.get("native"));
} else {
@@ -1,5 +1,6 @@
package run.webup.platform;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import java.math.BigDecimal;
import java.sql.Timestamp;
@@ -374,12 +375,24 @@ public class TaskService {
}
@Transactional
public void tool(UUID id, String lease, ModelGateway.Call call, String output) {
public boolean tool(UUID id, String lease, ModelGateway.Call call, String output)
throws JsonProcessingException {
locked(id, lease);
message(
id,
Map.of("role", "tool", "tool_call_id", call.id(), "name", call.name(), "content", output));
var result = json.readTree(output);
boolean failed =
result.hasNonNull("error")
|| result.path("timedOut").asBoolean(false)
|| result.path("exitCode").asInt(0) != 0;
var payload =
json.createObjectNode()
.put("role", "tool")
.put("tool_call_id", call.id())
.put("name", call.name())
.put("content", output);
if (failed) payload.put("is_error", true);
message(id, payload);
event(id, "TOOL_RESULT", call.name() + ": " + output);
return failed;
}
@Transactional
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2023 Anthropic
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@@ -745,6 +745,41 @@ class AgentWorkerIntegrationTest {
return result;
}
@Test
void failedToolReturnsResultsForWholeBatchAndLetsAgentRecover() throws Exception {
var batch = response("Build, then update the site", "run_command");
var calls =
(com.fasterxml.jackson.databind.node.ArrayNode)
batch.path("choices").get(0).path("message").path("tool_calls");
var write = response("Update the site", "write_file");
calls.add(write.path("choices").get(0).path("message").path("tool_calls").get(0));
when(transport.request(anyString(), anyString(), anyString(), anyString(), anyInt(), any()))
.thenReturn(batch, response("Retry the update", "write_file"), response("Done", null));
when(sandbox.execute(eq(project), argThat(call -> call.name().equals("run_command"))))
.thenReturn("{\"exitCode\":1,\"output\":\"build failed\"}");
when(sandbox.execute(eq(project), argThat(call -> call.name().equals("write_file"))))
.thenReturn("{\"written\":14}");
UUID id =
(UUID)
tasks
.create(user, project, model, "Build", new BigDecimal("20"), "tool-recovery")
.get("id");
new AgentWorker(tasks, gateway, sandbox, vault, json).run(tasks.claim());
assertEquals("COMPLETED", tasks.config(id).get("status"));
verify(sandbox, times(2)).execute(eq(project), any());
var results =
tasks.history(id).stream().filter(m -> "tool".equals(m.path("role").asText())).toList();
assertEquals(3, results.size());
assertEquals(calls.get(0).path("id").asText(), results.get(0).path("tool_call_id").asText());
assertTrue(results.get(0).path("is_error").asBoolean());
assertEquals(calls.get(1).path("id").asText(), results.get(1).path("tool_call_id").asText());
assertTrue(results.get(1).path("is_error").asBoolean());
assertTrue(results.get(1).path("content").asText().contains("Not executed"));
assertFalse(results.get(2).path("is_error").asBoolean());
}
@Test
void malformedToolArgumentsAreBilledAndReturnedWithoutSandboxExecution() throws Exception {
var malformed = response("write", "write_file");
@@ -22,6 +22,22 @@ class ModelGatewayTest {
final ObjectMapper json = new ObjectMapper();
final ModelGateway gateway = new ModelGateway(json, null);
@Test
void toolFailureUsesAnthropicErrorFieldWithoutAddingItToOpenAiRequests() {
var result =
json.createObjectNode()
.put("role", "tool")
.put("tool_call_id", "tool-1")
.put("content", "{\"exitCode\":1}")
.put("is_error", true);
var anthropic = gateway.request("ANTHROPIC", "fixture", 100, List.of(result), true);
assertTrue(
anthropic.path("messages").get(0).path("content").get(0).path("is_error").asBoolean());
var openAi = gateway.request("OPENAI", "fixture", 100, List.of(result), true);
assertFalse(openAi.path("messages").get(0).has("is_error"));
assertEquals("{\"exitCode\":1}", openAi.path("messages").get(0).path("content").asText());
}
@Test
void boundedOfficialDeepseekCallsDisableThinkingWithoutChangingOtherProviders() throws Exception {
var transport = org.mockito.Mockito.mock(ProviderTransport.class);