Show actual file changes recorded for each Agent task
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 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);