| 2563 | } |
| 2564 | |
| 2565 | async function trackSessionBranchingAnalytics( |
| 2566 | logs: LogOption[], |
| 2567 | ): Promise<void> { |
| 2568 | const sessionIdCounts = new Map<string, number>() |
| 2569 | let maxCount = 0 |
| 2570 | for (const log of logs) { |
| 2571 | const sessionId = getSessionIdFromLog(log) |
| 2572 | if (sessionId) { |
| 2573 | const newCount = (sessionIdCounts.get(sessionId) || 0) + 1 |
| 2574 | sessionIdCounts.set(sessionId, newCount) |
| 2575 | maxCount = Math.max(newCount, maxCount) |
| 2576 | } |
| 2577 | } |
| 2578 | |
| 2579 | // Early exit if no duplicates detected |
| 2580 | if (maxCount <= 1) { |
| 2581 | return |
| 2582 | } |
| 2583 | |
| 2584 | // Count sessions with branches and calculate stats using functional approach |
| 2585 | const branchCounts = Array.from(sessionIdCounts.values()).filter(c => c > 1) |
| 2586 | const sessionsWithBranches = branchCounts.length |
| 2587 | const totalBranches = branchCounts.reduce((sum, count) => sum + count, 0) |
| 2588 | |
| 2589 | logEvent('tengu_session_forked_branches_fetched', { |
| 2590 | total_sessions: sessionIdCounts.size, |
| 2591 | sessions_with_branches: sessionsWithBranches, |
| 2592 | max_branches_per_session: Math.max(...branchCounts), |
| 2593 | avg_branches_per_session: Math.round(totalBranches / sessionsWithBranches), |
| 2594 | total_transcript_count: logs.length, |
| 2595 | }) |
| 2596 | } |
| 2597 | |
| 2598 | export async function fetchLogs(limit?: number): Promise<LogOption[]> { |
| 2599 | const projectDir = getProjectDir(getOriginalCwd()) |