collectEntriesForBranchSummary function

Future<CollectBranchEntries> collectEntriesForBranchSummary(
  1. Session session,
  2. String? oldLeafId,
  3. String targetId
)

Collect the entries to summarize when navigating from oldLeafId to targetId: walks from the old leaf back to the common ancestor, returning entries in chronological order (omp's collectEntriesForBranchSummary). Compaction boundaries do NOT stop the walk — their summaries become context for the branch summary.

Implementation

Future<CollectBranchEntries> collectEntriesForBranchSummary(
  Session session,
  String? oldLeafId,
  String targetId,
) async {
  // No old position, nothing to summarize.
  if (oldLeafId == null) {
    return (entries: const <SessionRecord>[], commonAncestorId: null);
  }

  // Find the deepest record on both paths (the common ancestor).
  final oldPath = {
    for (final entry in await session.getBranch(fromId: oldLeafId)) entry.id,
  };
  final targetPath = await session.getBranch(fromId: targetId);
  String? commonAncestorId;
  for (var i = targetPath.length - 1; i >= 0; i--) {
    if (oldPath.contains(targetPath[i].id)) {
      commonAncestorId = targetPath[i].id;
      break;
    }
  }

  // Collect entries from the old leaf back to the common ancestor.
  final entries = <SessionRecord>[];
  String? current = oldLeafId;
  while (current != null && current != commonAncestorId) {
    final entry = await session.getEntry(current);
    if (entry == null) break;
    entries.add(entry);
    current = entry.parentId;
  }
  return (
    entries: entries.reversed.toList(growable: false),
    commonAncestorId: commonAncestorId,
  );
}