Merge pull request 'AI 开发支持确认后手动停止任务' (#4) from codex/confirm-stop-ai-task into main
Verify control plane / verify (push) Canceled after 0s

Reviewed-on: http://git.xhmcl.com/xhgit/webup-cloud/pulls/4
This commit was merged in pull request #4.
This commit is contained in:
xhgit committed 2026-11-07 16:37:22 +00:00
commit 43b5e24453
4 files changed
+132 -14

No files matched your search

@@ -37,7 +37,7 @@ public class TaskController {
public List<Map<String, Object>> list(@AuthenticationPrincipal SecurityConfig.Identity who) {
return db.queryForList(
"SELECT t.id,t.project_id,p.name AS"
+ " project_name,t.model_id,t.prompt,t.status,t.step_count,t.error,t.created_at,t.admin_priority,t.run_next,t.paid_priority,t.started_at,r.amount"
+ " project_name,t.model_id,t.prompt,t.status,t.requested_action,t.step_count,t.error,t.created_at,t.admin_priority,t.run_next,t.paid_priority,t.started_at,r.amount"
+ " AS budget,r.spent FROM ai_tasks t JOIN projects p ON p.id=t.project_id JOIN"
+ " wallet_reservations r ON r.id=t.reservation_id WHERE t.user_id=? ORDER BY"
+ " t.created_at DESC LIMIT 100",
@@ -400,6 +400,12 @@ public class TaskService {
@Transactional
public void finish(UUID id, String lease, String status, String error, boolean release) {
var t = locked(id, lease);
if ("CANCEL".equals(t.get("requested_action"))
&& !Set.of("CANCELLED", "REVIEW_REQUIRED").contains(status)) {
status = "CANCELLED";
error = "用户已停止任务,已结算产生的费用";
release = true;
}
db.update(
"UPDATE ai_tasks SET"
+ " status=?,error=?,requested_action='',lease_token=NULL,lease_expires_at=NULL,updated_at=CURRENT_TIMESTAMP"
@@ -419,6 +425,9 @@ public class TaskService {
find(id, user);
var t = db.queryForMap("SELECT * FROM ai_tasks WHERE id=? FOR UPDATE", id);
String status = t.get("status").toString();
boolean stopping = status.equals("RUNNING") && "CANCEL".equals(t.get("requested_action"));
if (action.equals("cancel") && (stopping || status.equals("CANCELLED"))) return find(id, user);
if (stopping) throw new ApiError.Failure(409, "任务正在停止");
if (action.equals("resume") && status.equals("PAUSED")) {
db.update(
"UPDATE ai_tasks SET status='QUEUED',error='',requested_action='',max_steps=CASE WHEN"
@@ -430,7 +439,10 @@ public class TaskService {
else db.update("UPDATE ai_tasks SET requested_action='PAUSE' WHERE id=?", id);
event(id, "PAUSE_REQUESTED", "当前步骤结束后暂停");
} else if (action.equals("cancel") && (status.equals("QUEUED") || status.equals("PAUSED"))) {
db.update("UPDATE ai_tasks SET status='CANCELLED' WHERE id=?", id);
db.update(
"UPDATE ai_tasks SET status='CANCELLED',requested_action='',updated_at=CURRENT_TIMESTAMP"
+ " WHERE id=?",
id);
wallets.release(UUID.fromString(t.get("reservation_id").toString()));
queue.refundUnused(id);
event(id, "CANCELLED", "用户取消,未使用预留已释放");
@@ -170,6 +170,42 @@ class WalletTaskIntegrationTest {
assertEquals(0, BigDecimal.ZERO.compareTo((BigDecimal) wallets.get(user).get("reserved")));
}
@Test
void runningStopIsIdempotentAndCannotBeOverwrittenByPauseOrCompletion() {
UUID id = task();
var claimed = tasks.claim();
String lease = claimed.get("lease_token").toString();
UUID reservation = UUID.fromString(claimed.get("reservation_id").toString());
wallets.charge(reservation, n("1.25"), "usage:before-stop");
assertEquals("CANCEL", tasks.action(id, user, "cancel").get("requested_action"));
assertEquals("CANCEL", tasks.action(id, user, "cancel").get("requested_action"));
assertEquals(
1,
identity
.db()
.queryForObject(
"SELECT COUNT(*) FROM ai_task_steps WHERE task_id=? AND event='CANCEL_REQUESTED'",
Integer.class,
id));
assertThrows(ApiError.Failure.class, () -> tasks.action(id, user, "pause"));
tasks.finish(id, lease, "COMPLETED", "Finished after the stop request", true);
assertEquals("CANCELLED", tasks.action(id, user, "cancel").get("status"));
assertEquals(0, n("98.75").compareTo((BigDecimal) wallets.get(user).get("balance")));
assertEquals(0, BigDecimal.ZERO.compareTo((BigDecimal) wallets.get(user).get("reserved")));
}
@Test
void stoppingDoesNotDiscardUnconfirmedCharges() {
UUID id = task();
String lease = tasks.claim().get("lease_token").toString();
tasks.action(id, user, "cancel");
tasks.finish(id, lease, "REVIEW_REQUIRED", "Unknown provider result", false);
assertEquals("REVIEW_REQUIRED", tasks.find(id, user).get("status"));
assertEquals(0, n("20").compareTo((BigDecimal) wallets.get(user).get("reserved")));
}
@Test
void exactUsageChargesWithSnapshot() throws Exception {
UUID id = task();