refactor(planning): dequeue orphans instead of promoting, restore lost lineage

Three behavioral changes around stuck planning subtasks:

- OrphanRecovery no longer clears ParentTaskId. Queued children of a
  parent that is not in a planning phase are dequeued (Status: Queued
  -> Idle, BlockedByTaskId cleared) but stay attached to the parent so
  the historical lineage is preserved.
- DiscardPlanningAsync stops promoting terminal (Done/Failed/Cancelled)
  children to top-level for the same reason - they remain ChildTasks of
  the (now non-planning) parent.
- New PlanningLineageRecovery hosted service scans
  ~/.todo-app/planning-sessions/ and re-attaches a single, unambiguous
  blocked-by chain to its original planning parent when the
  parent_task_id links were lost. Refuses to guess when multiple
  candidate chains exist.

UI now exposes ConnectionRestoredEvent on IWorkerClient, fired on first
connect and every reconnect. ListsIslandViewModel refreshes counters
and TasksIslandViewModel reloads the current list - so stale counts no
longer survive a worker restart.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
mika kuns
2026-05-18 16:28:57 +02:00
co-authored by Claude Opus 4.7
parent d094a21e09
commit 0d55002e5e
13 changed files with 270 additions and 44 deletions
@@ -5,10 +5,10 @@ using Microsoft.EntityFrameworkCore;
namespace ClaudeDo.Worker.Lifecycle;
/// <summary>
/// Startup-only sweep: clears <c>ParentTaskId</c> on rows whose parent is missing or
/// no longer in a planning phase. These rows would otherwise be invisible in the UI
/// (the parent doesn't render as a planning header) and cannot reach a terminal state
/// through the chain coordinator. Promoting them to top-level restores both.
/// Startup-only sweep: dequeues queued tasks whose parent is missing or no longer
/// in a planning phase. The child stays attached (<c>ParentTaskId</c> intact) but
/// drops out of the queue so it can't run against a dead chain. The user can
/// re-queue or detach manually.
/// </summary>
public sealed class OrphanRecovery : IHostedService
{
@@ -27,11 +27,11 @@ public sealed class OrphanRecovery : IHostedService
{
await using var ctx = await _dbFactory.CreateDbContextAsync(cancellationToken);
var repo = new TaskRepository(ctx);
var repaired = await repo.RepairOrphanedChildrenAsync(cancellationToken);
if (repaired > 0)
_logger.LogWarning("Orphan recovery: promoted {Count} orphaned child task(s) to top-level", repaired);
var dequeued = await repo.DequeueOrphanedChildrenAsync(cancellationToken);
if (dequeued > 0)
_logger.LogWarning("Orphan recovery: dequeued {Count} stuck child task(s)", dequeued);
else
_logger.LogInformation("Orphan recovery: no orphans found");
_logger.LogInformation("Orphan recovery: no stuck child tasks found");
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;