deduplicateSessionBranches function
Deduplicate conversation branches within the same session.
When a session file has multiple leaf messages (from retries or branching), each branch shares the same root. This keeps only the branch with the most user messages (tie-break by longest duration) per session_id.
Implementation
List<SessionMeta> deduplicateSessionBranches(List<SessionMeta> metas) {
final bestBySession = <String, SessionMeta>{};
for (final meta in metas) {
final existing = bestBySession[meta.sessionId];
if (existing == null ||
meta.userMessageCount > existing.userMessageCount ||
(meta.userMessageCount == existing.userMessageCount &&
meta.durationMinutes > existing.durationMinutes)) {
bestBySession[meta.sessionId] = meta;
}
}
return bestBySession.values.toList();
}