Files
proffesor-for-testing__agen…/docs/reports-archive/fixes/fleetmanager-initialization-fix.md
Profa b3d2018ee2 feat: v1.8.0 - Quality Hardening & Pattern Isolation
Critical bug fixes and code quality improvements from brutal-honest-review.

## Critical Fixes (9/10 completed, 1 deferred)

1. Fixed Agent Pattern Isolation (CRITICAL)
   - Added SwarmMemoryManager.queryPatternsByAgent()
   - SQL filtering by agent_id in metadata
   - Each agent now isolated

2. Fixed Async Method Cascade
   - Changed getPatterns() to async
   - Fixed 10 callers across 6 files
   - Build passes

3. Fixed Misleading Logging
   - Added isRealAgentDB() detection
   - Honest mock vs real reporting

4. Consolidated Embedding Generation
   - Created EmbeddingGenerator utility
   - Removed 50+ duplicate lines

5. Added AgentDB Initialization Checks
   - Empty DB detection
   - HNSW index verification

6. Integration Test Suite
   - 26 new test cases
   - Error path coverage

7. Repository Cleanup
   - Removed 19 temp test files

8. New QE Skill: sherlock-review
   - Evidence-based investigative review
   - 38 QE skills total

9. Deferred: Wire Up Real Test Execution
   - Deferred to v1.9.0 (documented)

## Statistics
- Fixes: 9/10 (90%, 1 deferred)
- Files Modified: 16
- Files Created: 3
- Files Deleted: 19
- Integration Tests: 26
- Build: PASSING

## Breaking Changes
- getPatterns() now async - add await

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-17 12:32:03 +00:00

6.1 KiB

FleetManager Initialization Fix

Problem Summary

Error: TypeError: Cannot read properties of undefined (reading 'initialize') Location: FleetManager.spawnAgent() at line 227 Impact: 77.5% test failure rate (224 errors) Date: 2025-10-21

Root Cause Analysis

Initial Investigation (CORRECT)

The error occurred because FleetManager.spawnAgent() was calling agent.initialize() without proper validation:

// Line 224-227 (BEFORE FIX)
const agent = await createAgent(type, agentId, config, this.eventBus);
this.agents.set(agentId, agent as any);
await agent.initialize();  // ← agent could be undefined!

Scenario Analysis

Scenario 1: BaseAgent.initialize() missing ❌

  • BaseAgent DOES have initialize() method (line 133 in BaseAgent.ts)

Scenario 2: FleetManager spawning broken ✅ PRIMARY ISSUE

  • FleetManager wasn't passing memoryStore in config
  • Agent factory expects config.memoryStore (line 768 in agents/index.ts)
  • Without memoryStore, agents fail to initialize properly

Scenario 3: Agent constructor issues ❌

  • All agents properly extend BaseAgent
  • Constructors call super() correctly

Secondary Issue Discovered

Missing MemoryManager integration in FleetManager:

  • FleetManager had no memoryManager field
  • No way to provide memoryStore to agents
  • Agent initialization hooks require memoryStore

Fixes Applied

Fix 1: Add Defensive Checks in spawnAgent()

File: /workspaces/agentic-qe-cf/src/core/FleetManager.ts

async spawnAgent(type: string, config: any = {}): Promise<Agent> {
  const agentId = uuidv4();

  try {
    // Ensure config has required dependencies
    const agentConfig = {
      memoryStore: config.memoryStore || this.getMemoryStore(),
      context: config.context || { id: agentId, type, status: 'initializing' as any },
      ...config
    };

    // Create agent using static import (enables proper mocking in tests)
    const agent = await createAgent(type, agentId, agentConfig, this.eventBus);

    // Validate agent was created successfully
    if (!agent) {
      throw new Error(`Failed to create agent of type: ${type}. Agent factory returned null/undefined.`);
    }

    // Validate agent has initialize method
    if (typeof agent.initialize !== 'function') {
      throw new Error(
        `Agent of type ${type} does not have initialize() method. ` +
        `Agent class must extend BaseAgent and implement initialize().`
      );
    }

    // Register agent before initialization (allows initialization hooks to access fleet)
    this.agents.set(agentId, agent as any);

    // Initialize the agent with error handling
    try {
      await agent.initialize();
    } catch (initError) {
      // Remove agent from registry if initialization fails
      this.agents.delete(agentId);
      throw new Error(
        `Failed to initialize agent ${type} (${agentId}): ${
          initError instanceof Error ? initError.message : String(initError)
        }`
      );
    }

    this.logger.info(`Agent spawned: ${type} (${agentId})`);
    this.emit('agent:spawned', { agentId, type });

    return agent as any;

  } catch (error) {
    // Enhanced error reporting
    const errorMessage = error instanceof Error ? error.message : String(error);
    this.logger.error(`Failed to spawn agent ${type}:`, errorMessage);

    // Re-throw with context
    throw new Error(
      `Agent spawning failed for type '${type}': ${errorMessage}. ` +
      `Ensure agent type is registered and properly implements BaseAgent interface.`
    );
  }
}

Fix 2: Add MemoryManager Integration

File: /workspaces/agentic-qe-cf/src/core/FleetManager.ts

Imports:

import { MemoryManager } from './MemoryManager';

Class Fields:

export class FleetManager extends EventEmitter {
  private readonly memoryManager: MemoryManager;
  // ... other fields
}

Constructor:

constructor(config: FleetConfig) {
  super();
  // ... existing initialization
  this.memoryManager = new MemoryManager(this.database);
  this.setupEventHandlers();
}

Initialize Method:

async initialize(): Promise<void> {
  this.logger.info(`Initializing Fleet Manager ${this.id}`);

  try {
    await this.database.initialize();
    await this.eventBus.initialize();
    await this.memoryManager.initialize();  // ← NEW
    await this.createInitialAgents();

    this.status = 'running';
    this.logger.info('Fleet Manager initialized successfully');
  } catch (error) {
    this.logger.error('Failed to initialize Fleet Manager:', error);
    throw error;
  }
}

Getter Method:

private getMemoryStore(): MemoryManager {
  return this.memoryManager;
}

Current Status

Compilation ✅

  • TypeScript compiles without errors
  • No type conflicts

Tests ⚠️ IN PROGRESS

  • Still encountering logger initialization errors in tests
  • Investigating: Cannot read properties of undefined (reading 'info')
  • Likely related to Winston logger initialization in test environment
  • Next steps: Add defensive checks in Logger class or fix test mocks

Impact Assessment

Files Modified

  1. /workspaces/agentic-qe-cf/src/core/FleetManager.ts - Core fix
  2. /workspaces/agentic-qe-cf/docs/fixes/fleetmanager-initialization-fix.md - This documentation

Expected Impact

  • Should fix 224 test failures (77.5% of total failures)
  • Improves error messages for easier debugging
  • Adds proper null checks and validation
  • Integrates memory management for agent coordination

Remaining Issues

  1. Logger initialization in test environment
  2. MemoryManager cleanup interval causing open handles in tests
  3. Need to verify agent initialization chain works end-to-end

Next Steps

  1. Fix Logger test initialization issues
  2. Add cleanup for MemoryManager intervals in tests
  3. Run full test suite to verify fix
  4. Document any remaining edge cases
  5. Update CHANGELOG.md with fix details

Technical Debt

  • Consider using dependency injection for FleetManager dependencies
  • Add FleetManager.destroy() method for proper cleanup
  • Consider making MemoryManager interval configurable for tests
  • Add integration tests for agent spawning flow