| 2524 | } |
| 2525 | |
| 2526 | async function trackSessionBranchingAnalytics( |
| 2527 | logs: LogOption[], |
| 2528 | ): Promise<void> { |
| 2529 | const sessionIdCounts = new Map<string, number>() |
| 2530 | let maxCount = 0 |
| 2531 | for (const log of logs) { |
| 2532 | const sessionId = getSessionIdFromLog(log) |
| 2533 | if (sessionId) { |
| 2534 | const newCount = (sessionIdCounts.get(sessionId) || 0) + 1 |
| 2535 | sessionIdCounts.set(sessionId, newCount) |
| 2536 | maxCount = Math.max(newCount, maxCount) |
| 2537 | } |
| 2538 | } |
| 2539 | |
| 2540 | // Early exit if no duplicates detected |
| 2541 | if (maxCount <= 1) { |
| 2542 | return |
| 2543 | } |
| 2544 | |
| 2545 | // Count sessions with branches and calculate stats using functional approach |
| 2546 | const branchCounts = Array.from(sessionIdCounts.values()).filter(c => c > 1) |
| 2547 | const sessionsWithBranches = branchCounts.length |
| 2548 | const totalBranches = branchCounts.reduce((sum, count) => sum + count, 0) |
| 2549 | |
| 2550 | logEvent('tengu_session_forked_branches_fetched', { |
| 2551 | total_sessions: sessionIdCounts.size, |
| 2552 | sessions_with_branches: sessionsWithBranches, |
| 2553 | max_branches_per_session: Math.max(...branchCounts), |
| 2554 | avg_branches_per_session: Math.round(totalBranches / sessionsWithBranches), |
| 2555 | total_transcript_count: logs.length, |
| 2556 | }) |
| 2557 | } |
| 2558 | |
| 2559 | export async function fetchLogs(limit?: number): Promise<LogOption[]> { |
| 2560 | const projectDir = getProjectDir(getOriginalCwd()) |