From 941c8b98ccc4435897084a3a2d4721028321a2f9 Mon Sep 17 00:00:00 2001 From: mika kuns Date: Thu, 23 Jul 2026 17:14:33 +0200 Subject: [PATCH] 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. --- docs/open.md | 1 - src/ClaudeDo.Worker/State/TaskStateService.cs | 22 +++++++++++++ .../WaitingForChildrenLifecycleTests.cs | 31 +++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/docs/open.md b/docs/open.md index dc279b7c..48178e83 100644 --- a/docs/open.md +++ b/docs/open.md @@ -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). diff --git a/src/ClaudeDo.Worker/State/TaskStateService.cs b/src/ClaudeDo.Worker/State/TaskStateService.cs index d6162194..a3e7adbd 100644 --- a/src/ClaudeDo.Worker/State/TaskStateService.cs +++ b/src/ClaudeDo.Worker/State/TaskStateService.cs @@ -220,6 +220,7 @@ public sealed class TaskStateService : ITaskStateService public async Task CancelAsync(string taskId, DateTime finishedAt, CancellationToken ct) { + List 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); } diff --git a/tests/ClaudeDo.Worker.Tests/WaitingForChildrenLifecycleTests.cs b/tests/ClaudeDo.Worker.Tests/WaitingForChildrenLifecycleTests.cs index bb634d77..262f9447 100644 --- a/tests/ClaudeDo.Worker.Tests/WaitingForChildrenLifecycleTests.cs +++ b/tests/ClaudeDo.Worker.Tests/WaitingForChildrenLifecycleTests.cs @@ -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 SeedActivePlanningParentAsync(string id = "par")