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:
mika kuns
2026-07-23 17:14:33 +02:00
parent 85c7e650c9
commit 941c8b98cc
3 changed files with 53 additions and 1 deletions
-1
View File
@@ -51,7 +51,6 @@ Alle 9 Review-Tasks (5 Refactorings, 4 Bugfixes) sind umgesetzt und gemerged; De
**Plausibel, noch nicht einzeln verifiziert (bei Gelegenheit prüfen):**
- Cancel eines `WaitingForChildren`-Parents kaskadiert nicht auf laufende/queued Kinder (verwaiste Worktree-Commits).
- Ketten-Kaskade stoppt an einem `Idle`-Mittelglied (`OnChildFinishedAsync` prüft `CancelAsync`-Ergebnis nicht) → Rest bleibt `Queued+blocked`.
- Delete des *letzten* nicht-terminalen Kindes triggert kein `TryAdvanceParentAsync` → Parent kann in `WaitingForChildren` hängen (FK `SET NULL` rettet nur die Blocked-Kette).
- `ContinueMergeAsync` staged per `git add -A` vor dem Konflikt-Check (Marker im Index, Abort danach ggf. unsauber).
@@ -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);
}
@@ -183,6 +183,37 @@ public sealed class WaitingForChildrenLifecycleTests : IDisposable
}
}
[Fact]
public async Task Cancelling_WaitingForChildren_parent_cascades_to_nonterminal_children()
{
using (var ctx = _db.CreateContext())
{
ctx.Lists.Add(new ListEntity { Id = "l1", Name = "L", CreatedAt = DateTime.UtcNow });
ctx.Tasks.Add(new TaskEntity { Id = "par", ListId = "l1", Title = "Parent",
Status = TaskStatus.WaitingForChildren, CreatedAt = DateTime.UtcNow });
ctx.Tasks.Add(new TaskEntity { Id = "c_running", ListId = "l1", Title = "Running child",
Status = TaskStatus.Running, ParentTaskId = "par", CreatedAt = DateTime.UtcNow });
ctx.Tasks.Add(new TaskEntity { Id = "c_queued", ListId = "l1", Title = "Queued child",
Status = TaskStatus.Queued, ParentTaskId = "par", BlockedByTaskId = "c_running",
CreatedAt = DateTime.UtcNow });
await ctx.SaveChangesAsync();
}
var result = await _built.State.CancelAsync("par", DateTime.UtcNow, default);
Assert.True(result.Ok);
using var ctx2 = _db.CreateContext();
var repo = new TaskRepository(ctx2);
var parent = await repo.GetByIdAsync("par");
var runningChild = await repo.GetByIdAsync("c_running");
var queuedChild = await repo.GetByIdAsync("c_queued");
Assert.Equal(TaskStatus.Cancelled, parent!.Status);
Assert.Equal(TaskStatus.Cancelled, runningChild!.Status);
Assert.Equal(TaskStatus.Cancelled, queuedChild!.Status);
Assert.Null(queuedChild.BlockedByTaskId);
}
// ─── FinalizePlanningAsync ────────────────────────────────────────────
private async Task<string> SeedActivePlanningParentAsync(string id = "par")