Show actual file changes recorded for each Agent task

This commit is contained in:
WebUp Cloud committed 2026-11-08 09:10:32 +08:00
1 parent d04777247b
commit 3296d378c9
14 files changed
+453 -16

No files matched your search

@@ -278,11 +278,17 @@ public class AgentWorker {
}
tasks.heartbeat(id, lease);
external = call.argumentError() == null;
String output =
call.argumentError() == null
? sandbox.execute(project, call)
: json.writeValueAsString(Map.of("error", call.argumentError()));
earlierToolFailed = tasks.tool(id, lease, call, output);
boolean trackChanges = external && SandboxClient.tracksChanges(call);
try {
String output =
external
? sandbox.execute(project, call)
: json.writeValueAsString(Map.of("error", call.argumentError()));
earlierToolFailed = tasks.tool(id, lease, call, output, trackChanges);
} catch (Exception failure) {
if (trackChanges) tasks.markFileChangesPartial(id, lease);
throw failure;
}
}
external = false;
}
@@ -31,7 +31,7 @@ public class ProjectFilesController {
projects.find(who, project);
try {
return json.readTree(
sandbox.execute(
sandbox.executeFileTool(
project,
new ModelGateway.Call(UUID.randomUUID().toString(), name, json.valueToTree(args))));
} catch (ApiError.Failure f) {
@@ -74,6 +74,15 @@ public class SandboxClient {
}
public String execute(UUID project, ModelGateway.Call call) {
if (tracksChanges(call)) return executeBounded(project, call, 512000, false, true);
return executeFileTool(project, call);
}
static boolean tracksChanges(ModelGateway.Call call) {
return Set.of("write_file", "delete_file", "run_command").contains(call.name());
}
String executeFileTool(UUID project, ModelGateway.Call call) {
return executeBounded(project, call, 131072);
}
@@ -175,6 +184,11 @@ public class SandboxClient {
}
private String executeBounded(UUID project, ModelGateway.Call call, int limit, boolean review) {
return executeBounded(project, call, limit, review, false);
}
private String executeBounded(
UUID project, ModelGateway.Call call, int limit, boolean review, boolean trackChanges) {
check();
var allowed =
review
@@ -192,8 +206,9 @@ public class SandboxClient {
"review_export");
if (!allowed.contains(call.name())) throw new ApiError.Failure(400, "不允许的工具");
try {
String payload =
json.writeValueAsString(Map.of("name", call.name(), "arguments", call.arguments()));
var body = json.createObjectNode().put("name", call.name());
body.set("arguments", call.arguments());
if (trackChanges) body.put("trackChanges", true);
var request =
HttpRequest.newBuilder(
URI.create(
@@ -204,7 +219,7 @@ public class SandboxClient {
.timeout(Duration.ofSeconds(150))
.header("Authorization", "Bearer " + token)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(payload))
.POST(HttpRequest.BodyPublishers.ofString(body.toString()))
.build();
var response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
try (var input = response.body()) {
@@ -81,6 +81,35 @@ public class TaskController {
return tasks.history(id);
}
@GetMapping("/{id}/changes")
public Map<String, Object> changes(
@AuthenticationPrincipal SecurityConfig.Identity who,
@PathVariable UUID id,
@RequestParam(defaultValue = "0") @Min(0) @Max(1000000) int page) {
var task = tasks.find(id, who.id());
var totals =
db.queryForMap(
"SELECT COUNT(*) AS file_count,COALESCE(SUM(added_lines),0) AS added_lines,"
+ " COALESCE(SUM(removed_lines),0) AS removed_lines"
+ " FROM ai_task_file_changes WHERE task_id=?",
id);
var files =
db.queryForList(
"SELECT file_path,last_status,added_lines,removed_lines,binary_change FROM"
+ " ai_task_file_changes WHERE task_id=? ORDER BY file_path LIMIT 100 OFFSET ?",
id,
page * 100);
return Map.of(
"checks",
task.get("file_change_checks"),
"partial",
task.get("file_changes_partial"),
"totals",
totals,
"files",
files);
}
@PostMapping("/{id}/{action:pause|resume|cancel}")
public Map<String, Object> action(
@AuthenticationPrincipal SecurityConfig.Identity who,
@@ -2,6 +2,7 @@ package run.webup.platform;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.*;
@@ -377,8 +378,20 @@ public class TaskService {
@Transactional
public boolean tool(UUID id, String lease, ModelGateway.Call call, String output)
throws JsonProcessingException {
return tool(id, lease, call, output, false);
}
@Transactional
public boolean tool(
UUID id, String lease, ModelGateway.Call call, String output, boolean trackChanges)
throws JsonProcessingException {
locked(id, lease);
var result = json.readTree(output);
if (trackChanges) {
var report = result instanceof ObjectNode object ? object.remove("fileChanges") : null;
recordFileChanges(id, report);
output = result.toString();
}
boolean failed =
result.hasNonNull("error")
|| result.path("timedOut").asBoolean(false)
@@ -395,6 +408,62 @@ public class TaskService {
return failed;
}
private void recordFileChanges(UUID task, JsonNode report) {
if (report == null || !report.path("files").isArray() || report.path("files").size() > 120) {
db.update("UPDATE ai_tasks SET file_changes_partial=TRUE WHERE id=?", task);
return;
}
boolean partial = !report.path("complete").asBoolean(false);
for (var file : report.path("files")) {
String path = file.path("path").asText(), status = file.path("status").asText();
int added = file.path("added").asInt(-1), removed = file.path("removed").asInt(-1);
if (path.isEmpty()
|| path.indexOf(0) >= 0
|| path.getBytes(java.nio.charset.StandardCharsets.UTF_8).length > 1000
|| !Set.of("ADDED", "MODIFIED", "DELETED").contains(status)
|| added < 0
|| removed < 0) {
partial = true;
continue;
}
boolean binary = file.path("binary").asBoolean(false);
int updated =
db.update(
"UPDATE ai_task_file_changes SET last_status=?,added_lines=added_lines+?,"
+ " removed_lines=removed_lines+?,binary_change=(binary_change OR ?)"
+ " WHERE task_id=? AND file_path=?",
status,
added,
removed,
binary,
task,
path);
if (updated == 0) {
db.update(
"INSERT INTO ai_task_file_changes"
+ " (task_id,file_path,last_status,added_lines,removed_lines,binary_change)"
+ " VALUES (?,?,?,?,?,?)",
task,
path,
status,
added,
removed,
binary);
}
}
db.update(
"UPDATE ai_tasks SET file_change_checks=file_change_checks+1,"
+ " file_changes_partial=(file_changes_partial OR ?) WHERE id=?",
partial,
task);
}
@Transactional
public void markFileChangesPartial(UUID task, String lease) {
locked(task, lease);
recordFileChanges(task, null);
}
@Transactional
public void requireSource(UUID id, String lease) {
locked(id, lease);
@@ -0,0 +1,12 @@
ALTER TABLE ai_tasks ADD COLUMN file_change_checks INTEGER NOT NULL DEFAULT 0;
ALTER TABLE ai_tasks ADD COLUMN file_changes_partial BOOLEAN NOT NULL DEFAULT FALSE;
CREATE TABLE ai_task_file_changes (
task_id UUID NOT NULL REFERENCES ai_tasks(id) ON DELETE CASCADE,
file_path VARCHAR(1000) NOT NULL,
last_status VARCHAR(12) NOT NULL CHECK (last_status IN ('ADDED', 'MODIFIED', 'DELETED')),
added_lines BIGINT NOT NULL CHECK (added_lines >= 0),
removed_lines BIGINT NOT NULL CHECK (removed_lines >= 0),
binary_change BOOLEAN NOT NULL,
PRIMARY KEY (task_id, file_path)
);
@@ -745,6 +745,67 @@ class AgentWorkerIntegrationTest {
return result;
}
@Test
void toolFileChangesAreSummedAndVisibleOnlyToTheTaskOwner() throws Exception {
UUID id =
(UUID)
tasks
.create(user, project, model, "Build", new BigDecimal("20"), "file-changes")
.get("id");
var claimed = tasks.claim();
when(transport.request(anyString(), anyString(), anyString(), anyString(), anyInt(), any()))
.thenReturn(
response("Write", "write_file"),
response("Edit", "write_file"),
response("Done", null));
when(sandbox.execute(eq(project), any()))
.thenReturn(
"""
{"written":12,"fileChanges":{"complete":true,"files":[{"path":"index.html","status":"ADDED","added":3,"removed":0,"binary":false}]}}
""",
"""
{"written":10,"fileChanges":{"complete":true,"files":[{"path":"index.html","status":"MODIFIED","added":1,"removed":2,"binary":false}]}}
""");
new AgentWorker(tasks, gateway, sandbox, vault, json).run(claimed);
assertEquals("COMPLETED", tasks.config(id).get("status"));
assertTrue(
tasks.history(id).stream()
.noneMatch(message -> message.toString().contains("fileChanges")));
String token =
users
.login("worker@test.com", "StrongPassword123", "test", "test", 1)
.get("token")
.toString();
mvc.perform(get("/api/tasks/" + id + "/changes").header("Authorization", "Bearer " + token))
.andExpect(status().isOk())
.andExpect(
org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath(
"$.totals.file_count")
.value(1))
.andExpect(
org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath(
"$.totals.added_lines")
.value(4))
.andExpect(
org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath(
"$.totals.removed_lines")
.value(2))
.andExpect(
org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath(
"$.files[0].last_status")
.value("MODIFIED"));
users.create("other-owner@test.com", "Other", "StrongPassword123", "test");
String other =
users
.login("other-owner@test.com", "StrongPassword123", "test", "test", 1)
.get("token")
.toString();
mvc.perform(get("/api/tasks/" + id + "/changes").header("Authorization", "Bearer " + other))
.andExpect(status().isNotFound());
}
@Test
void failedToolReturnsResultsForWholeBatchAndLetsAgentRecover() throws Exception {
var batch = response("Build, then update the site", "run_command");