From 8e00e0ef8c9ffd10a39781008941d4664a870003 Mon Sep 17 00:00:00 2001 From: Hibryda Date: Tue, 10 Mar 2026 23:46:52 +0100 Subject: [PATCH] perf(health): auto-stop tick timer when no active sessions Health tick now self-stops when no tracked project has a running/starting agent session. Auto-restarts on next recordActivity() call. Eliminates 5-second polling overhead when all agents are idle/done. --- v2/src/lib/stores/health.svelte.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/v2/src/lib/stores/health.svelte.ts b/v2/src/lib/stores/health.svelte.ts index d6ecf5d..27163a3 100644 --- a/v2/src/lib/stores/health.svelte.ts +++ b/v2/src/lib/stores/health.svelte.ts @@ -104,7 +104,7 @@ export function updateProjectSession(projectId: string, sessionId: string): void } } -/** Record activity — call on every agent message */ +/** Record activity — call on every agent message. Auto-starts tick if stopped. */ export function recordActivity(projectId: string, toolName?: string): void { const t = trackers.get(projectId); if (!t) return; @@ -113,6 +113,8 @@ export function recordActivity(projectId: string, toolName?: string): void { t.lastToolName = toolName; t.toolInFlight = true; } + // Auto-start tick when activity resumes + if (!tickInterval) startHealthTick(); } /** Record tool completion */ @@ -136,10 +138,24 @@ export function recordTokenSnapshot(projectId: string, totalTokens: number, cost t.costSnapshots = t.costSnapshots.filter(([ts]) => ts > cutoff); } -/** Start the health tick timer */ +/** Check if any tracked project has an active (running/starting) session */ +function hasActiveSession(): boolean { + for (const t of trackers.values()) { + if (!t.sessionId) continue; + const session = getAgentSession(t.sessionId); + if (session && (session.status === 'running' || session.status === 'starting')) return true; + } + return false; +} + +/** Start the health tick timer (auto-stops when no active sessions) */ export function startHealthTick(): void { if (tickInterval) return; tickInterval = setInterval(() => { + if (!hasActiveSession()) { + stopHealthTick(); + return; + } tickTs = Date.now(); }, TICK_INTERVAL_MS); }