feat(ui): add reconcile tick to self-heal lost worker broadcasts
TasksIslandViewModel now runs a periodic tick that diffs the flat Items collection against SQLite and patches properties in place (capped at 500 rows, sharing the Phase 1 delta sequence guard so it never overtakes a fresher broadcast). Never rebuilds rows or triggers Regroup/LoadForList, so it stays decoupled from the parallel Phase 2b virtualization work. The same cadence now refreshes the long-lived Worktrees Overview, Log Visualizer, and Merge Helper selection overlays; short-lived modals are untouched.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Globalization;
|
||||
using Avalonia.Threading;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using ClaudeDo.Data;
|
||||
@@ -29,6 +30,8 @@ public sealed partial class TasksIslandViewModel : ViewModelBase, IDisposable
|
||||
// one task can be in flight at once. Only the newest may write to the row.
|
||||
private readonly Dictionary<string, long> _deltaSeq = new();
|
||||
private long _deltaCounter;
|
||||
// Phase 3 reconcile tick — see the block at the end of this class.
|
||||
private readonly System.Timers.Timer _reconcileTimer = new(4_000);
|
||||
|
||||
public event EventHandler? SelectionChanged;
|
||||
public event EventHandler? FocusAddTaskRequested;
|
||||
@@ -127,11 +130,15 @@ public sealed partial class TasksIslandViewModel : ViewModelBase, IDisposable
|
||||
}
|
||||
_langChangedHandler = (_, _) => RefreshLocalizedText();
|
||||
Loc.LanguageChanged += _langChangedHandler;
|
||||
_reconcileTimer.Elapsed += (_, _) => Dispatcher.UIThread.Post(() => _ = ReconcileTickAsync());
|
||||
_reconcileTimer.Start();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Loc.LanguageChanged -= _langChangedHandler;
|
||||
_reconcileTimer.Stop();
|
||||
_reconcileTimer.Dispose();
|
||||
}
|
||||
|
||||
private void RefreshLocalizedText()
|
||||
@@ -1225,4 +1232,81 @@ public sealed partial class TasksIslandViewModel : ViewModelBase, IDisposable
|
||||
foreach (var i in Items) i.IsSelected = ReferenceEquals(i, value);
|
||||
SelectionChanged?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
// ── Phase 3: reconcile tick ──────────────────────────────────────────────
|
||||
// Self-healing safety net for a lost broadcast: every few seconds, diff the flat `Items`
|
||||
// master collection against SQLite and patch properties in place. Never rebuilds a row,
|
||||
// never calls Regroup() (Phase 2b owns OverdueItems/OpenItems/CompletedItems/Regroup and is
|
||||
// rewriting them in parallel), and never falls back to LoadForList — the delta path already
|
||||
// owns that escalation.
|
||||
|
||||
// Above this many rows, only the first N (in Items order) are reconciled per tick, so the
|
||||
// query cost stays bounded instead of growing with an ever-larger list.
|
||||
internal const int ReconcileRowCap = 500;
|
||||
|
||||
// Set by the shell from the main window's WindowState; the tick no-ops while minimized.
|
||||
public bool IsWindowVisible { get; set; } = true;
|
||||
|
||||
// Test seam: awaited right after the reconcile query returns and before its results are
|
||||
// applied, so a test can land a fresher, higher-sequence update in that gap and exercise the
|
||||
// stale-sequence guard deterministically. Always null outside tests.
|
||||
internal Func<Task>? ReconcileTickTestBarrier { get; set; }
|
||||
|
||||
// Awaitable so tests can drive it deterministically (mirrors RefreshTaskFromWorkerAsync).
|
||||
internal async Task ReconcileTickAsync(CancellationToken ct = default)
|
||||
{
|
||||
var list = _currentList;
|
||||
if (!IsWindowVisible || list is null) return;
|
||||
|
||||
// Mirrors the full-reload guard in RefreshTaskFromWorkerAsync: matching virtual:queued /
|
||||
// virtual:running depends on a Planning parent's children, not a single entity.
|
||||
if (list.Kind == ListKind.Virtual &&
|
||||
(list.Id == "virtual:queued" || list.Id == "virtual:running"))
|
||||
return;
|
||||
|
||||
var ids = Items.Select(r => r.Id).Take(ReconcileRowCap).ToList();
|
||||
if (ids.Count == 0) return;
|
||||
|
||||
// Same monotonic per-task sequence as the Phase 1 delta path, so whichever of the two
|
||||
// started most recently for a given task id wins, regardless of completion order.
|
||||
var seqByTaskId = new Dictionary<string, long>(ids.Count);
|
||||
foreach (var id in ids)
|
||||
{
|
||||
var seq = ++_deltaCounter;
|
||||
seqByTaskId[id] = seq;
|
||||
_deltaSeq[id] = seq;
|
||||
}
|
||||
|
||||
List<TaskEntity> entities;
|
||||
try
|
||||
{
|
||||
var idSet = ids.ToHashSet();
|
||||
await using var db = await _dbFactory.CreateDbContextAsync(ct);
|
||||
entities = await db.Tasks
|
||||
.Include(t => t.List)
|
||||
.Include(t => t.Worktree)
|
||||
.Where(t => idSet.Contains(t.Id))
|
||||
.ToListAsync(ct);
|
||||
}
|
||||
catch (OperationCanceledException) { return; }
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"TasksIsland: reconcile tick failed ({ex.Message})");
|
||||
return;
|
||||
}
|
||||
|
||||
if (ReconcileTickTestBarrier is { } barrier) await barrier();
|
||||
|
||||
var byId = entities.ToDictionary(e => e.Id);
|
||||
foreach (var id in ids)
|
||||
{
|
||||
// Superseded by a fresher delta refresh or a later tick that landed while this one
|
||||
// was reading — its result is stale, discard it.
|
||||
if (!_deltaSeq.TryGetValue(id, out var current) || current != seqByTaskId[id]) continue;
|
||||
if (!byId.TryGetValue(id, out var entity)) continue; // deleted; the delta path removes rows, not the tick
|
||||
|
||||
var row = Items.FirstOrDefault(r => r.Id == id);
|
||||
row?.UpdateFromEntity(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user