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.
This commit is contained in:
parent
1b61f10532
commit
8e00e0ef8c
1 changed files with 18 additions and 2 deletions
|
|
@ -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 {
|
export function recordActivity(projectId: string, toolName?: string): void {
|
||||||
const t = trackers.get(projectId);
|
const t = trackers.get(projectId);
|
||||||
if (!t) return;
|
if (!t) return;
|
||||||
|
|
@ -113,6 +113,8 @@ export function recordActivity(projectId: string, toolName?: string): void {
|
||||||
t.lastToolName = toolName;
|
t.lastToolName = toolName;
|
||||||
t.toolInFlight = true;
|
t.toolInFlight = true;
|
||||||
}
|
}
|
||||||
|
// Auto-start tick when activity resumes
|
||||||
|
if (!tickInterval) startHealthTick();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Record tool completion */
|
/** Record tool completion */
|
||||||
|
|
@ -136,10 +138,24 @@ export function recordTokenSnapshot(projectId: string, totalTokens: number, cost
|
||||||
t.costSnapshots = t.costSnapshots.filter(([ts]) => ts > cutoff);
|
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 {
|
export function startHealthTick(): void {
|
||||||
if (tickInterval) return;
|
if (tickInterval) return;
|
||||||
tickInterval = setInterval(() => {
|
tickInterval = setInterval(() => {
|
||||||
|
if (!hasActiveSession()) {
|
||||||
|
stopHealthTick();
|
||||||
|
return;
|
||||||
|
}
|
||||||
tickTs = Date.now();
|
tickTs = Date.now();
|
||||||
}, TICK_INTERVAL_MS);
|
}, TICK_INTERVAL_MS);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue