feat: enhance /api/debug/memory endpoint with comprehensive metrics

- Add getStats() method to SubagentWatcher for internal resource tracking
- Return detailed Map sizes for all server and subagent Maps
- Track file watchers, directory watchers, and transcript watchers separately
- Track all timer types (respawn, idle, pending starts)
- Add totals for quick overview of resource usage
- Useful for debugging memory leaks and resource exhaustion

The enhanced endpoint now returns:
- Memory usage (heap, external, array buffers)
- Map sizes by component (server, subagent watcher)
- Watcher counts (file, directory, transcript)
- Timer counts by type
- Spawn agent statistics

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
arkon
2026-01-28 04:44:29 +01:00
co-authored by Claude Opus 4.5
parent 596098015f
commit c659b7769e
2 changed files with 74 additions and 10 deletions
+30
View File
@@ -419,6 +419,36 @@ export class SubagentWatcher extends EventEmitter {
return this.getProjectHash(workingDir);
}
/**
* Get internal statistics for memory monitoring.
* Returns counts of internal Maps and resources.
*/
getStats(): {
agentCount: number;
fileWatcherCount: number;
dirWatcherCount: number;
idleTimerCount: number;
pendingToolCallsCount: number;
knownDirsCount: number;
filePositionsCount: number;
} {
// Count pending tool calls across all agents
let pendingToolCallsCount = 0;
for (const agentCalls of this.pendingToolCalls.values()) {
pendingToolCallsCount += agentCalls.size;
}
return {
agentCount: this.agentInfo.size,
fileWatcherCount: this.fileWatchers.size,
dirWatcherCount: this.dirWatchers.size,
idleTimerCount: this.idleTimers.size,
pendingToolCallsCount,
knownDirsCount: this.knownSubagentDirs.size,
filePositionsCount: this.filePositions.size,
};
}
/**
* Get recent subagents (modified within specified minutes)
*/