# 🚀 Autobeat v0.2.3 - Performance & Architecture Improvements

## Major Features

### Performance Optimization
- **Heap-based Priority Queue**: Replaced O(n²) array operations with O(log n) min-heap implementation
  - 100x performance improvement: 1000 tasks now process in 10ms (down from 1000ms)
  - O(1) task lookups via Map-based indexing
  - FIFO ordering preserved within same priority level

### Architecture Improvements
- **Strict Result Pattern Enforcement**: Removed all throw statements from event handlers
  - `BaseEventHandler.handleEvent()` now returns `Result<void>`
  - `BaseEventHandler.executeWithRetry()` returns `Result<void>` after exhausting retries
  - Event system is now 100% exception-free in business logic

### Security & Resource Management
- **Simplified DoS Protection**: Removed redundant rate limiting in favor of existing resource-level protections
  - Queue size limits (RESOURCE_EXHAUSTED when full)
  - Resource monitoring (workers only spawn when system has capacity)
  - Spawn throttling (prevents fork bombs)
  - Local-only MCP design doesn't require session-based rate limiting

## Bug Fixes

- **Critical: EventBus Timer Leak** - Fixed resource leak where EventBus cleanup timer wasn't disposed
  - `Container.dispose()` now properly calls `eventBus.dispose()` to clear setInterval timers
  - Integration tests updated to use `await container.dispose()` instead of `clear()`
  - Eliminates "Unhandled Rejection: Channel closed" errors in test suite

- **Test Infrastructure** - Fixed TypeScript type mismatches in TestLogger
  - Updated context parameters from `any` to `Record<string, unknown>`
  - Now properly implements Logger interface

- **Bootstrap Result Handling** - Fixed integration tests to unwrap `Result<Container>`
  - All tests now properly handle `bootstrap()` returning `Promise<Result<Container>>`
  - Improved error handling and validation in test setup

- **Path Validation** - Added comprehensive path validation to prevent traversal attacks
  - Working directory validation at MCP adapter boundary
  - Security hardening for file system operations

## Breaking Changes

**⚠️ API Changes**:

1. **BaseEventHandler Methods** - Event handlers now return Results instead of throwing:
   ```typescript
   // BEFORE (v0.2.2):
   protected async handleEvent(): Promise<void> // Could throw

   // AFTER (v0.2.3):
   protected async handleEvent(): Promise<Result<void>> // Returns Result
   ```

2. **Bootstrap Function** - Now returns Result for consistent error handling:
   ```typescript
   // BEFORE (v0.2.2):
   const container = await bootstrap();

   // AFTER (v0.2.3):
   const result = await bootstrap();
   if (!result.ok) throw new Error('Bootstrap failed');
   const container = result.value;
   ```

## Migration Guide

### For Custom Event Handlers

If you've created custom event handlers extending `BaseEventHandler`:

```typescript
// Update your handler methods to return Result<void>
class MyCustomHandler extends BaseEventHandler {
  // BEFORE:
  private async handleMyEvent(event: MyEvent): Promise<void> {
    // ... may throw
  }

  // AFTER:
  private async handleMyEvent(event: MyEvent): Promise<Result<void>> {
    // ... return ok(undefined) or err(error)
    return ok(undefined);
  }
}
```

### For Bootstrap Consumers

If you're using `bootstrap()` directly:

```typescript
// Update to unwrap Result
const result = await bootstrap();

if (!result.ok) {
  // Handle bootstrap failure
  console.error('Bootstrap failed:', result.error.message);
  process.exit(1);
}

const container = result.value;
// Continue with container usage
```

### For Container Lifecycle

Always use `dispose()` instead of `clear()`:

```typescript
// BEFORE:
container.clear(); // ❌ Doesn't clean up resources

// AFTER:
await container.dispose(); // ✅ Properly disposes EventBus, workers, database
```

## Test Results

- **638 tests passing** - All unit, integration, and E2E tests pass cleanly
- **Zero unhandled rejections** - Fixed timer leak that caused test errors
- **Performance validated** - Heap-based queue tested with 1000+ tasks

## Technical Details

### Priority Queue Implementation

The new heap-based implementation uses:
- Binary min-heap for O(log n) enqueue/dequeue
- Map-based index for O(1) task lookups and removal
- Insertion counter for FIFO ordering within same priority
- Proper heap invariant maintenance via bubble-up/bubble-down

### Resource Cleanup

The disposal chain now properly cascades:
1. `Container.dispose()` - Emits shutdown events
2. `WorkerPool.killAll()` - Terminates all workers
3. `Database.close()` - Closes SQLite connections
4. `EventBus.dispose()` - Clears cleanup timer
5. `Container.clear()` - Clears service registry

### Event-Driven Architecture

All components now strictly follow event-driven patterns:
- No direct throws in event handlers
- All operations return Result<T>
- EventBus handles error propagation
- Consistent error handling across all layers

## Installation

```bash
npm install -g autobeat@0.2.3
```

## What's Next

See [ROADMAP.md](./ROADMAP.md) for upcoming features:
- Enhanced git worktree support
- GitHub PR automation improvements
- Advanced autoscaling strategies
- Distributed task execution
