feat: add resource management types and utilities for memory optimization

- Add Disposable, BufferConfig, MemoryMetrics, CleanupRegistration types
- Create src/config/buffer-limits.ts with consolidated buffer size constants
- Create src/config/map-limits.ts with Map size limits to prevent unbounded growth
- Implement BufferAccumulator utility with configurable trim and onTrim callback
- Implement LRUMap with automatic eviction and O(1) operations
- Implement CleanupManager for unified resource cleanup with isStopped guard
- Add comprehensive tests for all new utilities

This lays the foundation for memory leak prevention and performance improvements.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
arkon
2026-01-28 04:35:20 +01:00
co-authored by Claude Opus 4.5
parent 0a928144da
commit 83e5b0b78e
10 changed files with 1802 additions and 0 deletions
+71
View File
@@ -11,6 +11,77 @@
* - Inner loop tracking (Ralph Wiggum detection)
*/
// ========== Resource Management Types ==========
/**
* Interface for objects that hold resources requiring explicit cleanup.
* Implementing classes should release timers, watchers, and other resources in dispose().
*/
export interface Disposable {
/** Release all held resources. Safe to call multiple times. */
dispose(): void;
/** Whether this object has been disposed */
readonly isDisposed: boolean;
}
/**
* Configuration for buffer accumulator instances.
* Used for terminal buffers, text output, and other size-limited string storage.
*/
export interface BufferConfig {
/** Maximum buffer size in bytes before trimming */
maxSize: number;
/** Size to trim to when maxSize is exceeded */
trimSize: number;
/** Optional callback invoked when buffer is trimmed */
onTrim?: (trimmedBytes: number) => void;
}
/**
* Memory metrics for monitoring and debugging.
* Extends Node.js process.memoryUsage() with application-specific tracking.
*/
export interface MemoryMetrics {
/** Heap memory used by V8 (bytes) */
heapUsed: number;
/** Total heap size allocated by V8 (bytes) */
heapTotal: number;
/** Memory used by C++ objects bound to JavaScript (bytes) */
external: number;
/** Memory used by ArrayBuffers and SharedArrayBuffers (bytes) */
arrayBuffers: number;
/** Sizes of tracked Maps by name */
mapSizes: Record<string, number>;
/** Number of active timers (setTimeout/setInterval) */
timerCount: number;
/** Number of active file system watchers */
watcherCount: number;
/** Timestamp when metrics were collected */
timestamp: number;
}
/**
* Resource types that can be registered for cleanup.
*/
export type CleanupResourceType = 'timer' | 'interval' | 'watcher' | 'listener' | 'stream';
/**
* Registration entry for a cleanup resource.
* Used by CleanupManager to track and dispose resources.
*/
export interface CleanupRegistration {
/** Unique identifier for this registration */
id: string;
/** Type of resource */
type: CleanupResourceType;
/** Human-readable description for debugging */
description: string;
/** Cleanup function to call on dispose */
cleanup: () => void;
/** Timestamp when registered */
registeredAt: number;
}
// ========== Core Status Types ==========
/** Status of a Claude session */