mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-10-07 07:59:42 +02:00
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:
@@ -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 */
|
||||
|
||||
Reference in New Issue
Block a user