fix(worker): cascade cancel of a WaitingForChildren parent to its non-terminal children
Confirmed via docs/open.md (Korrektheits-Review 2026-06-09): TaskStateService.CancelAsync only flipped the target row to Cancelled - a Queued/Running child kept going and could still commit into its worktree after the parent was gone. CancelAsync now also cancels the task's own non-terminal children (Idle/Queued/Running/ WaitingForReview/WaitingForChildren) in the same pass, clearing BlockedByTaskId so no successor is left wedged, and broadcasts TaskUpdated for each.
This commit is contained in:
@@ -220,6 +220,7 @@ public sealed class TaskStateService : ITaskStateService
|
||||
|
||||
public async Task<TransitionResult> CancelAsync(string taskId, DateTime finishedAt, CancellationToken ct)
|
||||
{
|
||||
List<string> cancelledChildIds;
|
||||
await using (var ctx = await _dbFactory.CreateDbContextAsync(ct))
|
||||
{
|
||||
var affected = await ctx.Tasks
|
||||
@@ -233,10 +234,31 @@ public sealed class TaskStateService : ITaskStateService
|
||||
|
||||
if (affected == 0)
|
||||
return new TransitionResult(false, "Task not in cancellable state.");
|
||||
|
||||
// Cascade to this task's own non-terminal children (one level — children are
|
||||
// leaves in the current model) so a Queued/Running child doesn't keep going
|
||||
// against a parent that's already gone, orphaning its worktree commit.
|
||||
cancelledChildIds = await ctx.Tasks
|
||||
.Where(t => t.ParentTaskId == taskId
|
||||
&& t.Status != TaskStatus.Done
|
||||
&& t.Status != TaskStatus.Failed
|
||||
&& t.Status != TaskStatus.Cancelled)
|
||||
.Select(t => t.Id)
|
||||
.ToListAsync(ct);
|
||||
|
||||
if (cancelledChildIds.Count > 0)
|
||||
await ctx.Tasks
|
||||
.Where(t => cancelledChildIds.Contains(t.Id))
|
||||
.ExecuteUpdateAsync(s => s
|
||||
.SetProperty(t => t.Status, TaskStatus.Cancelled)
|
||||
.SetProperty(t => t.FinishedAt, finishedAt)
|
||||
.SetProperty(t => t.BlockedByTaskId, (string?)null), ct);
|
||||
}
|
||||
|
||||
await OnChildTerminalAsync(taskId, TaskStatus.Cancelled);
|
||||
await _broadcaster.TaskUpdated(taskId);
|
||||
foreach (var childId in cancelledChildIds)
|
||||
await _broadcaster.TaskUpdated(childId);
|
||||
return new TransitionResult(true, null);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user