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:
@@ -451,21 +451,9 @@ public sealed class TaskRepository
|
||||
.SetProperty(t => t.BlockedByTaskId, (string?)null), ct);
|
||||
}
|
||||
|
||||
// Terminal children (Done/Failed/Cancelled) survive the discard but cannot remain
|
||||
// attached: their parent's PlanningPhase is about to be reset to None, which would
|
||||
// make them orphans. Promote them to top-level.
|
||||
var terminalIds = children
|
||||
.Where(c => c.Status == TaskStatus.Done
|
||||
|| c.Status == TaskStatus.Failed
|
||||
|| c.Status == TaskStatus.Cancelled)
|
||||
.Select(c => c.Id)
|
||||
.ToList();
|
||||
if (terminalIds.Count > 0)
|
||||
{
|
||||
await _context.Tasks
|
||||
.Where(t => terminalIds.Contains(t.Id))
|
||||
.ExecuteUpdateAsync(s => s.SetProperty(t => t.ParentTaskId, (string?)null), ct);
|
||||
}
|
||||
// Terminal children (Done/Failed/Cancelled) stay attached to the parent even
|
||||
// though its PlanningPhase will be reset to None. The lineage is preserved as
|
||||
// historical context; the UI nests them under their parent regardless of phase.
|
||||
|
||||
// Idle children created during this planning session are dropped.
|
||||
await _context.Tasks
|
||||
@@ -488,13 +476,16 @@ public sealed class TaskRepository
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears <c>ParentTaskId</c> on rows whose parent is missing or no longer in a
|
||||
/// planning phase. Returns the number of rows repaired. Idempotent.
|
||||
/// Dequeues child tasks whose parent is missing or no longer in a planning phase:
|
||||
/// sets <c>Status</c> from <c>Queued</c> to <c>Idle</c> and clears
|
||||
/// <c>BlockedByTaskId</c>. <c>ParentTaskId</c> stays intact — the child remains
|
||||
/// part of its (former) planning chain for historical context. Returns the
|
||||
/// number of rows dequeued. Idempotent.
|
||||
/// </summary>
|
||||
internal async Task<int> RepairOrphanedChildrenAsync(CancellationToken ct = default)
|
||||
internal async Task<int> DequeueOrphanedChildrenAsync(CancellationToken ct = default)
|
||||
{
|
||||
var orphanIds = await _context.Tasks
|
||||
.Where(t => t.ParentTaskId != null)
|
||||
.Where(t => t.ParentTaskId != null && t.Status == TaskStatus.Queued)
|
||||
.Where(t => !_context.Tasks.Any(p =>
|
||||
p.Id == t.ParentTaskId && p.PlanningPhase != PlanningPhase.None))
|
||||
.Select(t => t.Id)
|
||||
@@ -504,7 +495,73 @@ public sealed class TaskRepository
|
||||
|
||||
return await _context.Tasks
|
||||
.Where(t => orphanIds.Contains(t.Id))
|
||||
.ExecuteUpdateAsync(s => s.SetProperty(t => t.ParentTaskId, (string?)null), ct);
|
||||
.ExecuteUpdateAsync(s => s
|
||||
.SetProperty(t => t.Status, TaskStatus.Idle)
|
||||
.SetProperty(t => t.BlockedByTaskId, (string?)null), ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Restores a planning-session lineage that lost its <c>parent_task_id</c> links.
|
||||
/// Given a candidate parent task and a single unambiguous orphan chain in the
|
||||
/// same list (linked via <c>BlockedByTaskId</c>), re-attaches the chain members
|
||||
/// to the parent, marks the parent as <c>Finalized</c>, and dequeues queued
|
||||
/// chain members. No-op if conditions are not met. Returns the number of
|
||||
/// re-attached children (0 if skipped).
|
||||
/// </summary>
|
||||
internal async Task<int> RestorePlanningLineageAsync(string parentId, CancellationToken ct = default)
|
||||
{
|
||||
var parent = await _context.Tasks.AsNoTracking()
|
||||
.FirstOrDefaultAsync(t => t.Id == parentId, ct);
|
||||
if (parent is null) return 0;
|
||||
if (parent.PlanningPhase != PlanningPhase.None) return 0;
|
||||
if (parent.Status is TaskStatus.Done or TaskStatus.Failed or TaskStatus.Cancelled) return 0;
|
||||
|
||||
// Candidates: unattached tasks in the same list, excluding the parent itself.
|
||||
var candidates = await _context.Tasks.AsNoTracking()
|
||||
.Where(t => t.ListId == parent.ListId && t.ParentTaskId == null && t.Id != parent.Id)
|
||||
.ToListAsync(ct);
|
||||
|
||||
// A chain is a maximal linear sequence linked via BlockedByTaskId. Find heads
|
||||
// (BlockedByTaskId == null) that have at least one successor.
|
||||
var bySource = candidates
|
||||
.Where(c => c.BlockedByTaskId != null)
|
||||
.ToLookup(c => c.BlockedByTaskId!);
|
||||
|
||||
var heads = candidates
|
||||
.Where(c => c.BlockedByTaskId == null && bySource[c.Id].Any())
|
||||
.ToList();
|
||||
|
||||
// Bail unless exactly one chain anchors a successor — anything else is
|
||||
// ambiguous and we refuse to guess.
|
||||
if (heads.Count != 1) return 0;
|
||||
|
||||
var chain = new List<TaskEntity> { heads[0] };
|
||||
var current = heads[0];
|
||||
while (true)
|
||||
{
|
||||
var next = bySource[current.Id].FirstOrDefault();
|
||||
if (next is null) break;
|
||||
chain.Add(next);
|
||||
current = next;
|
||||
}
|
||||
|
||||
var chainIds = chain.Select(c => c.Id).ToList();
|
||||
|
||||
await _context.Tasks
|
||||
.Where(t => t.Id == parentId)
|
||||
.ExecuteUpdateAsync(s => s.SetProperty(t => t.PlanningPhase, PlanningPhase.Finalized), ct);
|
||||
|
||||
await _context.Tasks
|
||||
.Where(t => chainIds.Contains(t.Id))
|
||||
.ExecuteUpdateAsync(s => s.SetProperty(t => t.ParentTaskId, (string?)parentId), ct);
|
||||
|
||||
// Dequeue queued chain members; blocked_by stays intact so chain order is
|
||||
// preserved for manual re-queueing.
|
||||
await _context.Tasks
|
||||
.Where(t => chainIds.Contains(t.Id) && t.Status == TaskStatus.Queued)
|
||||
.ExecuteUpdateAsync(s => s.SetProperty(t => t.Status, TaskStatus.Idle), ct);
|
||||
|
||||
return chainIds.Count;
|
||||
}
|
||||
|
||||
public async Task TryCompleteParentAsync(
|
||||
|
||||
@@ -12,6 +12,8 @@ public interface IWorkerClient : INotifyPropertyChanged
|
||||
event Action<string, string, DateTime>? TaskStartedEvent;
|
||||
event Action<string, string, string, DateTime>? TaskFinishedEvent;
|
||||
event Action<string>? TaskUpdatedEvent;
|
||||
/// <summary>Raised once when the SignalR connection is first established, and again on every reconnect.</summary>
|
||||
event Action? ConnectionRestoredEvent;
|
||||
event Action<string>? WorktreeUpdatedEvent;
|
||||
event Action<string, string>? TaskMessageEvent;
|
||||
|
||||
|
||||
@@ -46,6 +46,7 @@ public partial class WorkerClient : ObservableObject, IAsyncDisposable, IWorkerC
|
||||
public event Action<string, string, string, DateTime>? TaskFinishedEvent;
|
||||
public event Action<string, string>? TaskMessageEvent;
|
||||
public event Action<string>? TaskUpdatedEvent;
|
||||
public event Action? ConnectionRestoredEvent;
|
||||
public event Action<string>? WorktreeUpdatedEvent;
|
||||
public event Action<string>? RunNowRequestedEvent;
|
||||
public event Action<string>? ListUpdatedEvent;
|
||||
@@ -76,6 +77,7 @@ public partial class WorkerClient : ObservableObject, IAsyncDisposable, IWorkerC
|
||||
{
|
||||
Dispatcher.UIThread.Post(() => { IsConnected = true; IsReconnecting = false; });
|
||||
await SeedActiveTasksAsync();
|
||||
Dispatcher.UIThread.Post(() => ConnectionRestoredEvent?.Invoke());
|
||||
};
|
||||
|
||||
_hub.Reconnecting += _ =>
|
||||
@@ -200,6 +202,7 @@ public partial class WorkerClient : ObservableObject, IAsyncDisposable, IWorkerC
|
||||
await _hub.StartAsync(ct);
|
||||
Dispatcher.UIThread.Post(() => { IsConnected = true; IsReconnecting = false; });
|
||||
await SeedActiveTasksAsync();
|
||||
Dispatcher.UIThread.Post(() => ConnectionRestoredEvent?.Invoke());
|
||||
return;
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
|
||||
@@ -78,6 +78,7 @@ public sealed partial class ListsIslandViewModel : ViewModelBase
|
||||
_worker.TaskStartedEvent += (_slot, _id, _at) => _ = RefreshCountsAsync();
|
||||
_worker.TaskFinishedEvent += (_slot, _id, _status, _at) => _ = RefreshCountsAsync();
|
||||
_worker.TaskUpdatedEvent += _id => _ = RefreshCountsAsync();
|
||||
_worker.ConnectionRestoredEvent += () => _ = RefreshCountsAsync();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -55,9 +55,10 @@ public sealed partial class TasksIslandViewModel : ViewModelBase
|
||||
_worker = worker;
|
||||
if (_worker is not null)
|
||||
{
|
||||
_worker.TaskUpdatedEvent += OnWorkerTaskUpdated;
|
||||
_worker.WorktreeUpdatedEvent += OnWorkerTaskUpdated;
|
||||
_worker.TaskMessageEvent += OnWorkerTaskMessage;
|
||||
_worker.TaskUpdatedEvent += OnWorkerTaskUpdated;
|
||||
_worker.WorktreeUpdatedEvent += OnWorkerTaskUpdated;
|
||||
_worker.TaskMessageEvent += OnWorkerTaskMessage;
|
||||
_worker.ConnectionRestoredEvent += () => LoadForList(_currentList);
|
||||
_ = RefreshAllTagsAsync();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
69
src/ClaudeDo.Worker/Lifecycle/PlanningLineageRecovery.cs
Normal file
69
src/ClaudeDo.Worker/Lifecycle/PlanningLineageRecovery.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using ClaudeDo.Data;
|
||||
using ClaudeDo.Data.Repositories;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ClaudeDo.Worker.Lifecycle;
|
||||
|
||||
/// <summary>
|
||||
/// Startup-only sweep that tries to re-attach blocked-by chains to their original
|
||||
/// planning parent when the lineage was lost (parent_task_id cleared, parent
|
||||
/// reverted to <c>PlanningPhase.None</c>) but the planning-sessions directory
|
||||
/// still has the matching folder. Only restores lineage when the chain in the
|
||||
/// parent's list is unambiguously the parent's — refuses to guess otherwise.
|
||||
/// </summary>
|
||||
public sealed class PlanningLineageRecovery : IHostedService
|
||||
{
|
||||
private readonly IDbContextFactory<ClaudeDoDbContext> _dbFactory;
|
||||
private readonly string _sessionsRoot;
|
||||
private readonly ILogger<PlanningLineageRecovery> _logger;
|
||||
|
||||
public PlanningLineageRecovery(
|
||||
IDbContextFactory<ClaudeDoDbContext> dbFactory,
|
||||
string sessionsRoot,
|
||||
ILogger<PlanningLineageRecovery> logger)
|
||||
{
|
||||
_dbFactory = dbFactory;
|
||||
_sessionsRoot = sessionsRoot;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (!Directory.Exists(_sessionsRoot))
|
||||
{
|
||||
_logger.LogInformation("Planning lineage recovery: sessions directory missing, nothing to scan");
|
||||
return;
|
||||
}
|
||||
|
||||
var folders = Directory.GetDirectories(_sessionsRoot);
|
||||
if (folders.Length == 0)
|
||||
{
|
||||
_logger.LogInformation("Planning lineage recovery: no session folders");
|
||||
return;
|
||||
}
|
||||
|
||||
await using var ctx = await _dbFactory.CreateDbContextAsync(cancellationToken);
|
||||
var repo = new TaskRepository(ctx);
|
||||
|
||||
var restored = 0;
|
||||
foreach (var folder in folders)
|
||||
{
|
||||
var taskId = Path.GetFileName(folder);
|
||||
if (string.IsNullOrEmpty(taskId)) continue;
|
||||
|
||||
var count = await repo.RestorePlanningLineageAsync(taskId, cancellationToken);
|
||||
if (count > 0)
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"Planning lineage recovery: re-attached {Count} child(ren) to parent {ParentId}",
|
||||
count, taskId);
|
||||
restored++;
|
||||
}
|
||||
}
|
||||
|
||||
if (restored == 0)
|
||||
_logger.LogInformation("Planning lineage recovery: no candidates");
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
}
|
||||
@@ -101,6 +101,10 @@ builder.Services.AddSingleton(sp =>
|
||||
sp.GetRequiredService<ITaskStateService>(),
|
||||
sp.GetRequiredService<PlanningChainCoordinator>(),
|
||||
planningSessionsDir));
|
||||
builder.Services.AddHostedService(sp => new PlanningLineageRecovery(
|
||||
sp.GetRequiredService<IDbContextFactory<ClaudeDoDbContext>>(),
|
||||
planningSessionsDir,
|
||||
sp.GetRequiredService<ILogger<PlanningLineageRecovery>>()));
|
||||
builder.Services.AddSingleton<IPlanningTerminalLauncher>(sp =>
|
||||
new WindowsTerminalPlanningLauncher("wt.exe", cfg.ClaudeBin));
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
|
||||
Reference in New Issue
Block a user