| 2686 | } |
| 2687 | |
| 2688 | async function trackSessionBranchingAnalytics( |
| 2689 | logs: LogOption[], |
| 2690 | ): Promise<void> { |
| 2691 | const sessionIdCounts = new Map<string, number>() |
| 2692 | let maxCount = 0 |
| 2693 | for (const log of logs) { |
| 2694 | const sessionId = getSessionIdFromLog(log) |
| 2695 | if (sessionId) { |
| 2696 | const newCount = (sessionIdCounts.get(sessionId) || 0) + 1 |
| 2697 | sessionIdCounts.set(sessionId, newCount) |
| 2698 | maxCount = Math.max(newCount, maxCount) |
| 2699 | } |
| 2700 | } |
| 2701 | |
| 2702 | // Early exit if no duplicates detected |
| 2703 | if (maxCount <= 1) { |
| 2704 | return |
| 2705 | } |
| 2706 | |
| 2707 | // Count sessions with branches and calculate stats using functional approach |
| 2708 | const branchCounts = Array.from(sessionIdCounts.values()).filter(c => c > 1) |
| 2709 | const sessionsWithBranches = branchCounts.length |
| 2710 | const totalBranches = branchCounts.reduce((sum, count) => sum + count, 0) |
| 2711 | |
| 2712 | logEvent('ncode_session_forked_branches_fetched', { |
| 2713 | total_sessions: sessionIdCounts.size, |
| 2714 | sessions_with_branches: sessionsWithBranches, |
| 2715 | max_branches_per_session: Math.max(...branchCounts), |
| 2716 | avg_branches_per_session: Math.round(totalBranches / sessionsWithBranches), |
| 2717 | total_transcript_count: logs.length, |
| 2718 | }) |
| 2719 | } |
| 2720 | |
| 2721 | export async function fetchLogs(limit?: number): Promise<LogOption[]> { |
| 2722 | const projectDir = getProjectDir(getOriginalCwd()) |