feat: comprehensive codebase improvements (cleanup, logic, performance)

Code Cleanups:
- Consolidated PlanTaskStatus and TddPhase types to types.ts
- Standardized node: prefix for Node.js builtin imports
- Fixed timer type to NodeJS.Timeout in respawn-controller

Logic Improvements:
- Fixed AI check race condition with UUID tracking in respawn-controller
- Added comprehensive _isStopped guards in session timer callbacks
- Made RalphTracker reset events non-reentrant via process.nextTick()
- Improved SessionManager mutex pattern reliability

Performance Optimizations:
- Fixed pendingToolCalls memory leak with TTL cleanup in subagent-watcher
- Improved LRUMap.newest() from O(n) to O(1) with _newestKey tracking

Documentation:
- Enhanced README antiflicker section with 6-layer technical details

Version: 0.1441

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
arkon
2026-01-30 19:05:46 +01:00
co-authored by Claude Opus 4.5
parent ea81d94173
commit 20283019f4
17 changed files with 342 additions and 820 deletions
+18
View File
@@ -35,6 +35,7 @@
*/
import { EventEmitter } from 'node:events';
import { randomUUID } from 'node:crypto';
import { Session } from './session.js';
import { AiIdleChecker, type AiCheckResult, type AiCheckState } from './ai-idle-checker.js';
import { AiPlanChecker, type AiPlanCheckResult } from './ai-plan-checker.js';
@@ -716,6 +717,9 @@ export class RespawnController extends EventEmitter {
/** Timestamp when plan check was started (to detect stale results) */
private planCheckStartTime: number = 0;
/** Unique ID for current AI check request (to detect stale results) */
private _currentAiCheckId: string | null = null;
/** Timer for /clear step fallback (sends /init if no prompt detected) */
private clearFallbackTimer: NodeJS.Timeout | null = null;
@@ -2131,6 +2135,10 @@ export class RespawnController extends EventEmitter {
this.logAction('ai-check', `Spawning AI idle checker (${reason})`);
this.emit('aiCheckStarted');
// Generate unique ID for this check to detect stale results
const checkId = randomUUID();
this._currentAiCheckId = checkId;
// Get the terminal buffer for analysis
const buffer = this.terminalBuffer.value;
@@ -2141,6 +2149,12 @@ export class RespawnController extends EventEmitter {
return;
}
// Validate this is the result for the current check (not a stale one)
if (this._currentAiCheckId !== checkId) {
this.log(`AI check result ignored (stale check ID: ${checkId.substring(0, 8)})`);
return;
}
if (result.verdict === 'IDLE') {
// Cancel any pending confirmation timers - AI has spoken
this.cancelTrackedTimer('completion-confirm', this.completionConfirmTimer, 'AI verdict: IDLE');
@@ -2173,6 +2187,10 @@ export class RespawnController extends EventEmitter {
this.startPreFilterTimer();
}
}).catch((err) => {
// Validate this is the error for the current check
if (this._currentAiCheckId !== checkId) {
return; // Stale check, ignore error
}
if (this._state === 'ai_checking') {
const errorMsg = err instanceof Error ? err.message : String(err);
this.logAction('ai-check', `Failed: ${errorMsg.substring(0, 50)}`);