fix(ui): stop the reconcile ticks from destroying user state

The 4s reconcile tick was added to three long-lived surfaces. On two of
them it reloads by rebuilding every row instance, which silently threw
away whatever the user had built up since the overlay opened; on the
third it healed a row's data but left it in the wrong section.

- WorktreesOverview: LoadAsync clears Rows, SelectedCount, ConflictRows
  and BatchProgress, so batch-merge ticks, the highlighted row and a
  finished batch's outcome badges were wiped every four seconds --
  assembling a multi-worktree selection was impossible. Carry that state
  across the reload, keyed by task id, and re-point SelectedRow at the
  fresh instance (or clear it when the worktree is gone).
- MergeHelperSelection: the remember/restore had no re-entrancy guard, so
  a second tick entering between the first one's reload and its restore
  snapshotted rows already back at IsTickedByDefault and wrote that
  default back, re-ticking what the user had unticked. One tick at a time,
  and hold the row instances instead of a value snapshot so a tick landed
  during the await survives.
- TasksIsland: the tick deliberately never called Regroup because Phase 2b
  owned Rows in parallel. 2b has landed, so a healed task that went Done
  stayed in the Open section under a stale count, and a healed depends-on
  link never pulled its dependent under the chain head. Regroup when a
  patch moved a grouping input, gated on a cheap key so an idle tick stays
  free.
This commit is contained in:
mika kuns
2026-08-11 16:39:20 +02:00
parent eb66ae7b8b
commit 79b35801ae
6 changed files with 335 additions and 21 deletions
@@ -1420,10 +1420,11 @@ public sealed partial class TasksIslandViewModel : ViewModelBase, IDisposable
// ── 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.
// master collection against SQLite and patch properties in place. Never rebuilds a row and
// never falls back to LoadForList — the delta path already owns that escalation. It DOES
// call Regroup(), but only when a patch actually moved a row's grouping inputs: a healed
// row can change section, order or rail label, and none of that shows until Rows is
// re-emitted.
// 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.
@@ -1487,6 +1488,7 @@ public sealed partial class TasksIslandViewModel : ViewModelBase, IDisposable
// scan per id is 250k comparisons every few seconds, for nothing.
var rowById = new Dictionary<string, TaskRowViewModel>(Items.Count);
foreach (var r in Items) rowById[r.Id] = r;
var groupingChanged = false;
foreach (var id in ids)
{
// Superseded by a fresher delta refresh or a later tick that landed while this one
@@ -1494,7 +1496,23 @@ public sealed partial class TasksIslandViewModel : ViewModelBase, IDisposable
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
if (rowById.TryGetValue(id, out var row)) row.UpdateFromEntity(entity);
if (!rowById.TryGetValue(id, out var row)) continue;
var before = GroupingKey(row);
row.UpdateFromEntity(entity);
if (!before.Equals(GroupingKey(row))) groupingChanged = true;
}
// A patched row can have left its section (Done), moved inside it (a new/removed
// depends-on link re-orders the chain), or changed what its rail label reads. Rows only
// reflects any of that after a Regroup — without this the tick would heal the row's data
// while leaving a completed task sitting in the Open section under a stale count. Gated
// on a real change so an idle tick stays free.
if (groupingChanged) Regroup();
}
// The slice of a row that Regroup reads: which section it lands in, where it sits inside it,
// and what its chain rail/after-chip says. Only fields UpdateFromEntity actually writes —
// IsExpanded and HasPlanningChildren are owned elsewhere and would produce false positives.
private static (bool, DateTime?, string?, string?, PlanningPhase, int, string) GroupingKey(TaskRowViewModel r) =>
(r.Done, r.ScheduledFor, r.ParentTaskId, r.DependsOnTaskId, r.PlanningPhase, r.Number, r.Title);
}