Optimize the agent tool execution loop
Verify control plane / verify (push) Canceled after 0s
Verify control plane / verify (pull_request) Canceled after 0s

This commit is contained in:
WebUp Cloud committed 2026-11-08 08:57:47 +08:00
1 parent 572371d7af
commit 4291adfdfb
7 files changed
+110 -7

No files matched your search

@@ -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);