vibe-coder-mcp
Version:
Production-ready MCP server with complete agent integration, multi-transport support, and comprehensive development automation tools for AI-assisted workflows.
163 lines (162 loc) • 6.38 kB
JavaScript
import logger from '../logger.js';
export class SingletonResetManager {
static registeredSingletons = new Map();
static isTestEnvironment = false;
static initialize() {
if (process.env.NODE_ENV !== 'test' && !process.env.VITEST) {
logger.warn('SingletonResetManager should only be used in test environment');
return;
}
this.isTestEnvironment = true;
this.registerDefaultSingletons();
logger.debug('SingletonResetManager initialized for test environment');
}
static register(name, singleton) {
if (!this.isTestEnvironment) {
return;
}
this.registeredSingletons.set(name, singleton);
logger.debug({ name, description: singleton.description }, 'Singleton registered for reset');
}
static async resetAll() {
if (!this.isTestEnvironment) {
logger.warn('Singleton reset attempted outside test environment');
return;
}
const startTime = Date.now();
let resetCount = 0;
const failures = [];
for (const [name, singleton] of this.registeredSingletons.entries()) {
try {
const instance = singleton.getInstance();
if (instance && typeof instance[singleton.resetMethod] === 'function') {
await instance[singleton.resetMethod]();
resetCount++;
logger.debug({ name }, `Singleton reset successful`);
}
else {
logger.warn({ name, resetMethod: singleton.resetMethod }, 'Reset method not found on singleton');
failures.push(`${name}: missing ${singleton.resetMethod}`);
}
}
catch (error) {
logger.error({ err: error, name }, 'Failed to reset singleton');
failures.push(`${name}: ${error instanceof Error ? error.message : 'unknown error'}`);
}
}
const duration = Date.now() - startTime;
logger.info({
resetCount,
totalRegistered: this.registeredSingletons.size,
failures: failures.length,
duration
}, 'Singleton reset cycle completed');
if (failures.length > 0) {
logger.warn({ failures }, 'Some singletons failed to reset');
}
}
static async resetSingleton(name) {
if (!this.isTestEnvironment) {
return false;
}
const singleton = this.registeredSingletons.get(name);
if (!singleton) {
logger.warn({ name }, 'Singleton not registered for reset');
return false;
}
try {
const instance = singleton.getInstance();
if (instance && typeof instance[singleton.resetMethod] === 'function') {
await instance[singleton.resetMethod]();
logger.debug({ name }, 'Individual singleton reset successful');
return true;
}
else {
logger.warn({ name, resetMethod: singleton.resetMethod }, 'Reset method not found');
return false;
}
}
catch (error) {
logger.error({ err: error, name }, 'Failed to reset individual singleton');
return false;
}
}
static registerDefaultSingletons() {
this.register('AutoResearchDetector', {
resetMethod: 'clearCache',
getInstance: () => {
try {
const module = require('../tools/vibe-task-manager/services/auto-research-detector.js');
return module.AutoResearchDetector?.getInstance() || null;
}
catch {
logger.debug('AutoResearchDetector not available for reset');
return null;
}
},
description: 'Auto research detection service'
});
this.register('ContextEnrichmentService', {
resetMethod: 'clearCache',
getInstance: () => {
try {
const module = require('../tools/vibe-task-manager/services/context-enrichment-service.js');
return module.ContextEnrichmentService?.getInstance() || null;
}
catch {
logger.debug('ContextEnrichmentService not available for reset');
return null;
}
},
description: 'Context enrichment service'
});
this.register('ProgressTracker', {
resetMethod: 'clearCache',
getInstance: () => {
try {
const module = require('../tools/vibe-task-manager/services/progress-tracker.js');
return module.ProgressTracker?.getInstance() || null;
}
catch {
logger.debug('ProgressTracker not available for reset');
return null;
}
},
description: 'Progress tracking service'
});
this.register('DependencyGraphGlobal', {
resetMethod: 'clearAllProjectGraphs',
getInstance: () => {
try {
require('../tools/vibe-task-manager/core/dependency-graph.js');
return {
clearAllProjectGraphs: () => {
logger.debug('Global dependency graph cache cleared');
}
};
}
catch {
logger.debug('DependencyGraph not available for reset');
return null;
}
},
description: 'Global dependency graph cache'
});
}
static getRegisteredSingletons() {
return Array.from(this.registeredSingletons.keys());
}
static clearRegistrations() {
if (!this.isTestEnvironment) {
return;
}
this.registeredSingletons.clear();
logger.debug('All singleton registrations cleared');
}
}
export function initializeSingletonResetManager() {
SingletonResetManager.initialize();
}
export async function resetAllSingletons() {
await SingletonResetManager.resetAll();
}