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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,8 +41,16 @@ public sealed partial class LogVisualizerViewModel : ViewModelBase
|
||||
_worker = worker;
|
||||
_copyToClipboard = copyToClipboard;
|
||||
Rows.CollectionChanged += (_, _) => CopyLastCommand.NotifyCanExecuteChanged();
|
||||
// Phase 3 reconcile tick: this overlay is long-lived (stays open while the user reads
|
||||
// through the log), so refresh it on the same cadence as TasksIslandViewModel's tick
|
||||
// instead of leaving it frozen at the moment it was opened.
|
||||
_reconcileTimer.Elapsed += (_, _) =>
|
||||
Avalonia.Threading.Dispatcher.UIThread.Post(() => _ = RefreshAsync());
|
||||
_reconcileTimer.Start();
|
||||
}
|
||||
|
||||
private readonly System.Timers.Timer _reconcileTimer = new(4_000);
|
||||
|
||||
[RelayCommand]
|
||||
public async Task RefreshAsync()
|
||||
{
|
||||
@@ -65,7 +73,13 @@ public sealed partial class LogVisualizerViewModel : ViewModelBase
|
||||
: Loc.T("modals.logVisualizer.count", Rows.Count);
|
||||
}
|
||||
|
||||
[RelayCommand] private void Close() => CloseAction?.Invoke();
|
||||
[RelayCommand]
|
||||
private void Close()
|
||||
{
|
||||
_reconcileTimer.Stop();
|
||||
_reconcileTimer.Dispose();
|
||||
CloseAction?.Invoke();
|
||||
}
|
||||
|
||||
private bool CanCopyLast() => Rows.Count > 0;
|
||||
|
||||
|
||||
@@ -51,8 +51,18 @@ public sealed partial class MergeHelperSelectionModalViewModel : ViewModelBase
|
||||
public TaskCompletionSource<IReadOnlyList<string>?> Result { get; } = new();
|
||||
public Action? CloseAction { get; set; }
|
||||
|
||||
private readonly System.Timers.Timer _reconcileTimer = new(4_000);
|
||||
|
||||
public MergeHelperSelectionModalViewModel(IDbContextFactory<ClaudeDoDbContext> dbFactory)
|
||||
=> _dbFactory = dbFactory;
|
||||
{
|
||||
_dbFactory = dbFactory;
|
||||
// Phase 3 reconcile tick: this overlay is long-lived (stays open while the user ticks
|
||||
// through candidates), so refresh it on the same cadence as TasksIslandViewModel's tick
|
||||
// instead of leaving it frozen at the moment it was opened.
|
||||
_reconcileTimer.Elapsed += (_, _) =>
|
||||
Avalonia.Threading.Dispatcher.UIThread.Post(() => _ = ReconcileTickAsync());
|
||||
_reconcileTimer.Start();
|
||||
}
|
||||
|
||||
public void Configure(string listId, string listName)
|
||||
{
|
||||
@@ -60,6 +70,19 @@ public sealed partial class MergeHelperSelectionModalViewModel : ViewModelBase
|
||||
ScopeLabel = Loc.T("modals.mergeHelper.scopeList", listName);
|
||||
}
|
||||
|
||||
// LoadAsync rebuilds every row from scratch, which would wipe the user's ticks out from
|
||||
// under them on every tick — capture and restore selection by id around the reload.
|
||||
internal async Task ReconcileTickAsync()
|
||||
{
|
||||
if (string.IsNullOrEmpty(_listId)) return;
|
||||
var selected = Tasks.Where(t => t.IsSelected).Select(t => t.Id).ToHashSet();
|
||||
await LoadAsync();
|
||||
foreach (var t in Tasks)
|
||||
if (selected.Contains(t.Id))
|
||||
t.IsSelected = true;
|
||||
OnPropertyChanged(nameof(CanConfirm));
|
||||
}
|
||||
|
||||
public async Task LoadAsync(CancellationToken ct = default)
|
||||
{
|
||||
foreach (var row in Tasks) row.PropertyChanged -= OnRowChanged;
|
||||
@@ -163,6 +186,7 @@ public sealed partial class MergeHelperSelectionModalViewModel : ViewModelBase
|
||||
[RelayCommand]
|
||||
private void Confirm()
|
||||
{
|
||||
StopReconcileTick();
|
||||
Result.TrySetResult(Tasks.Where(t => t.IsSelected).Select(t => t.Id).ToList());
|
||||
CloseAction?.Invoke();
|
||||
}
|
||||
@@ -170,7 +194,17 @@ public sealed partial class MergeHelperSelectionModalViewModel : ViewModelBase
|
||||
[RelayCommand]
|
||||
private void Cancel()
|
||||
{
|
||||
StopReconcileTick();
|
||||
Result.TrySetResult(null);
|
||||
CloseAction?.Invoke();
|
||||
}
|
||||
|
||||
// Also called from the dialog's native-close fallback (WindowDialogService), which resolves
|
||||
// Result without going through Confirm/Cancel. Idempotent — Timer.Stop()/Dispose() tolerate
|
||||
// repeated calls.
|
||||
internal void StopReconcileTick()
|
||||
{
|
||||
_reconcileTimer.Stop();
|
||||
_reconcileTimer.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,8 +100,20 @@ public sealed partial class WorktreesOverviewModalViewModel : ViewModelBase
|
||||
_worker = worker;
|
||||
_diffVmFactory = diffVmFactory;
|
||||
_merge = merge;
|
||||
// Phase 3 reconcile tick: this overlay is long-lived (stays open while the user reviews
|
||||
// worktrees), so refresh it on the same cadence as TasksIslandViewModel's tick instead of
|
||||
// leaving it frozen at the moment it was opened. Skipped while a load or batch merge is
|
||||
// already in flight — reloading mid-merge would replace the row instances the merge loop
|
||||
// is still updating.
|
||||
_reconcileTimer.Elapsed += (_, _) =>
|
||||
Avalonia.Threading.Dispatcher.UIThread.Post(() => _ = ReconcileTickAsync());
|
||||
_reconcileTimer.Start();
|
||||
}
|
||||
|
||||
private readonly System.Timers.Timer _reconcileTimer = new(4_000);
|
||||
|
||||
internal Task ReconcileTickAsync() => IsBusy || IsMerging ? Task.CompletedTask : LoadAsync();
|
||||
|
||||
public void SelectRow(WorktreeOverviewRowViewModel row)
|
||||
{
|
||||
if (SelectedRow is not null) SelectedRow.IsSelected = false;
|
||||
@@ -179,7 +191,12 @@ public sealed partial class WorktreesOverviewModalViewModel : ViewModelBase
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void Close() => CloseAction?.Invoke();
|
||||
private void Close()
|
||||
{
|
||||
_reconcileTimer.Stop();
|
||||
_reconcileTimer.Dispose();
|
||||
CloseAction?.Invoke();
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void ShowDiff(WorktreeOverviewRowViewModel? row)
|
||||
|
||||
@@ -25,7 +25,11 @@ public partial class MainWindow : Window
|
||||
{
|
||||
base.OnPropertyChanged(change);
|
||||
if (change.Property == WindowStateProperty)
|
||||
{
|
||||
UpdateMaxIcon();
|
||||
if (DataContext is IslandsShellViewModel vm && vm.Tasks is not null)
|
||||
vm.Tasks.IsWindowVisible = WindowState != WindowState.Minimized;
|
||||
}
|
||||
if (change.Property == OffScreenMarginProperty)
|
||||
RootGrid.Margin = OffScreenMargin;
|
||||
}
|
||||
@@ -48,6 +52,8 @@ public partial class MainWindow : Window
|
||||
WindowState = WindowState.Normal;
|
||||
Activate();
|
||||
};
|
||||
if (vm.Tasks is not null)
|
||||
vm.Tasks.IsWindowVisible = WindowState != WindowState.Minimized;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -94,7 +94,11 @@ public sealed class WindowDialogService : IDialogService
|
||||
{
|
||||
var dlg = new MergeHelperSelectionModal { DataContext = vm };
|
||||
vm.CloseAction = () => dlg.Close();
|
||||
dlg.Closed += (_, _) => vm.Result.TrySetResult(null); // native close counts as cancel
|
||||
dlg.Closed += (_, _) =>
|
||||
{
|
||||
vm.StopReconcileTick();
|
||||
vm.Result.TrySetResult(null); // native close counts as cancel
|
||||
};
|
||||
await dlg.ShowDialog(_owner);
|
||||
return await vm.Result.Task;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user