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:
@@ -0,0 +1,218 @@
|
||||
using ClaudeDo.Data;
|
||||
using ClaudeDo.Data.Models;
|
||||
using ClaudeDo.Ui.ViewModels.Islands;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using TaskStatus = ClaudeDo.Data.Models.TaskStatus;
|
||||
|
||||
namespace ClaudeDo.Ui.Tests.ViewModels;
|
||||
|
||||
// Phase 3 reconcile tick: a periodic, awaitable-for-tests diff of the flat `Items` master
|
||||
// collection against SQLite that patches properties in place. It must never rebuild a row,
|
||||
// never call Regroup(), and never fall back to LoadForList — see
|
||||
// docs/superpowers/specs/2026-08-07-ui-reaktivitaet-und-listen-performance-design.md.
|
||||
public class TasksIslandReconcileTickTests : IDisposable
|
||||
{
|
||||
private readonly string _dbPath;
|
||||
|
||||
public TasksIslandReconcileTickTests()
|
||||
{
|
||||
_dbPath = Path.Combine(Path.GetTempPath(), $"claudedo_ui_reconcile_{Guid.NewGuid():N}.db");
|
||||
using var ctx = NewContext();
|
||||
ctx.Database.EnsureCreated();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
try { File.Delete(_dbPath); } catch { }
|
||||
try { File.Delete(_dbPath + "-wal"); } catch { }
|
||||
try { File.Delete(_dbPath + "-shm"); } catch { }
|
||||
}
|
||||
|
||||
private ClaudeDoDbContext NewContext()
|
||||
{
|
||||
var opts = new DbContextOptionsBuilder<ClaudeDoDbContext>()
|
||||
.UseSqlite($"Data Source={_dbPath}")
|
||||
.Options;
|
||||
return new ClaudeDoDbContext(opts);
|
||||
}
|
||||
|
||||
private sealed class CountingDbFactory : IDbContextFactory<ClaudeDoDbContext>
|
||||
{
|
||||
private readonly Func<ClaudeDoDbContext> _create;
|
||||
public int CreateCalls { get; private set; }
|
||||
public CountingDbFactory(Func<ClaudeDoDbContext> create) => _create = create;
|
||||
public ClaudeDoDbContext CreateDbContext()
|
||||
{
|
||||
CreateCalls++;
|
||||
return _create();
|
||||
}
|
||||
}
|
||||
|
||||
private static ListNavItemViewModel UserList(string listEntityId, string name) =>
|
||||
new() { Id = $"user:{listEntityId}", Kind = ListKind.User, Name = name };
|
||||
|
||||
private static async Task LoadAndWaitAsync(TasksIslandViewModel vm, ListNavItemViewModel list, int expectedCount = 1)
|
||||
{
|
||||
vm.LoadForList(list);
|
||||
var deadline = DateTime.UtcNow.AddSeconds(5);
|
||||
while (DateTime.UtcNow < deadline)
|
||||
{
|
||||
await Task.Delay(25);
|
||||
if (vm.Items.Count >= expectedCount) break;
|
||||
}
|
||||
await Task.Delay(50);
|
||||
}
|
||||
|
||||
private async Task SeedListAsync()
|
||||
{
|
||||
await using var db = NewContext();
|
||||
db.Lists.Add(new ListEntity { Id = "L1", Name = "Work", CreatedAt = DateTime.UtcNow });
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
private async Task SeedTaskAsync(string id, string title, TaskStatus status, int sortOrder)
|
||||
{
|
||||
await using var db = NewContext();
|
||||
db.Tasks.Add(new TaskEntity
|
||||
{
|
||||
Id = id, ListId = "L1", Title = title,
|
||||
Status = status, CreatedAt = DateTime.UtcNow, SortOrder = sortOrder,
|
||||
});
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Tick_patches_a_diverging_property()
|
||||
{
|
||||
await SeedListAsync();
|
||||
await SeedTaskAsync("T1", "Original title", TaskStatus.Idle, 0);
|
||||
|
||||
var vm = new TasksIslandViewModel(new CountingDbFactory(NewContext), worker: null);
|
||||
await LoadAndWaitAsync(vm, UserList("L1", "Work"));
|
||||
Assert.Equal("Original title", vm.Items.Single(r => r.Id == "T1").Title);
|
||||
|
||||
await using (var db = NewContext())
|
||||
{
|
||||
var t = await db.Tasks.FirstAsync(x => x.Id == "T1");
|
||||
t.Title = "Renamed elsewhere";
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
await vm.ReconcileTickAsync();
|
||||
|
||||
Assert.Equal("Renamed elsewhere", vm.Items.Single(r => r.Id == "T1").Title);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Tick_never_creates_a_new_row_instance()
|
||||
{
|
||||
await SeedListAsync();
|
||||
await SeedTaskAsync("T1", "Task one", TaskStatus.Idle, 0);
|
||||
|
||||
var vm = new TasksIslandViewModel(new CountingDbFactory(NewContext), worker: null);
|
||||
await LoadAndWaitAsync(vm, UserList("L1", "Work"));
|
||||
var rowBefore = vm.Items.Single(r => r.Id == "T1");
|
||||
|
||||
await using (var db = NewContext())
|
||||
{
|
||||
var t = await db.Tasks.FirstAsync(x => x.Id == "T1");
|
||||
t.Status = TaskStatus.Running;
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
await vm.ReconcileTickAsync();
|
||||
|
||||
var rowAfter = vm.Items.Single(r => r.Id == "T1");
|
||||
Assert.True(ReferenceEquals(rowBefore, rowAfter));
|
||||
Assert.Equal(TaskStatus.Running, rowAfter.Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Tick_with_no_changes_raises_no_property_changed()
|
||||
{
|
||||
await SeedListAsync();
|
||||
await SeedTaskAsync("T1", "Task one", TaskStatus.Idle, 0);
|
||||
|
||||
var vm = new TasksIslandViewModel(new CountingDbFactory(NewContext), worker: null);
|
||||
await LoadAndWaitAsync(vm, UserList("L1", "Work"));
|
||||
var row = vm.Items.Single(r => r.Id == "T1");
|
||||
|
||||
var changedProperties = new List<string?>();
|
||||
row.PropertyChanged += (_, e) => changedProperties.Add(e.PropertyName);
|
||||
|
||||
await vm.ReconcileTickAsync();
|
||||
|
||||
Assert.Empty(changedProperties);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Tick_discards_a_stale_result_when_a_newer_refresh_wins_the_race()
|
||||
{
|
||||
await SeedListAsync();
|
||||
await SeedTaskAsync("T1", "Task one", TaskStatus.Queued, 0);
|
||||
|
||||
var vm = new TasksIslandViewModel(new CountingDbFactory(NewContext), worker: null);
|
||||
await LoadAndWaitAsync(vm, UserList("L1", "Work"));
|
||||
Assert.Equal(TaskStatus.Queued, vm.Items.Single(r => r.Id == "T1").Status);
|
||||
|
||||
var barrierReached = new TaskCompletionSource();
|
||||
var proceed = new TaskCompletionSource();
|
||||
vm.ReconcileTickTestBarrier = async () =>
|
||||
{
|
||||
barrierReached.TrySetResult();
|
||||
await proceed.Task;
|
||||
};
|
||||
|
||||
// Tick reads "Queued" (nothing has changed yet), then parks at the barrier before
|
||||
// applying its (about to become stale) result.
|
||||
var tickTask = vm.ReconcileTickAsync();
|
||||
await barrierReached.Task;
|
||||
|
||||
// A fresher broadcast lands and wins: the DB flips to Running and the delta path applies
|
||||
// it with a newer sequence number while the tick is still parked.
|
||||
await using (var db = NewContext())
|
||||
{
|
||||
var t = await db.Tasks.FirstAsync(x => x.Id == "T1");
|
||||
t.Status = TaskStatus.Running;
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
await vm.RefreshTaskFromWorkerAsync("T1");
|
||||
Assert.Equal(TaskStatus.Running, vm.Items.Single(r => r.Id == "T1").Status);
|
||||
|
||||
// Let the tick's stale "Queued" apply attempt run — the sequence guard must discard it.
|
||||
proceed.TrySetResult();
|
||||
await tickTask;
|
||||
|
||||
Assert.Equal(TaskStatus.Running, vm.Items.Single(r => r.Id == "T1").Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Tick_caps_reconciliation_at_the_row_limit()
|
||||
{
|
||||
await SeedListAsync();
|
||||
var total = TasksIslandViewModel.ReconcileRowCap + 1;
|
||||
for (var i = 0; i < total; i++)
|
||||
await SeedTaskAsync($"T{i}", $"Task {i}", TaskStatus.Queued, i);
|
||||
|
||||
var factory = new CountingDbFactory(NewContext);
|
||||
var vm = new TasksIslandViewModel(factory, worker: null);
|
||||
await LoadAndWaitAsync(vm, UserList("L1", "Work"), expectedCount: total);
|
||||
Assert.Equal(total, vm.Items.Count);
|
||||
|
||||
await using (var db = NewContext())
|
||||
{
|
||||
await db.Tasks.ExecuteUpdateAsync(s => s.SetProperty(t => t.Status, TaskStatus.Running));
|
||||
}
|
||||
|
||||
var createCallsBefore = factory.CreateCalls;
|
||||
await vm.ReconcileTickAsync();
|
||||
Assert.Equal(createCallsBefore + 1, factory.CreateCalls); // exactly one DB query for the whole tick
|
||||
|
||||
var reconciled = vm.Items.Count(r => r.Status == TaskStatus.Running);
|
||||
Assert.Equal(TasksIslandViewModel.ReconcileRowCap, reconciled);
|
||||
|
||||
// The row beyond the cap (last one loaded, per SortOrder) must not have been touched.
|
||||
var lastRow = vm.Items.Single(r => r.Id == $"T{total - 1}");
|
||||
Assert.Equal(TaskStatus.Queued, lastRow.Status);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user