|
| 1 | +# @lytics/dev-agent-subagents |
| 2 | + |
| 3 | +**The Central Nervous System** for dev-agent's multi-agent coordination. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The subagents package provides a robust, production-ready coordinator system for managing specialized AI agents. Inspired by human physiology, each component mirrors a part of the nervous system: |
| 8 | + |
| 9 | +- **🧠 Coordinator** - The brain, orchestrating all agents |
| 10 | +- **🔬 Context Manager** - The hippocampus, managing shared memory |
| 11 | +- **⚡ Task Queue** - The motor cortex, controlling execution |
| 12 | +- **📊 Logger** - Observability system (future: `@lytics/croak`) |
| 13 | + |
| 14 | +## Architecture |
| 15 | + |
| 16 | +``` |
| 17 | +packages/subagents/ |
| 18 | +├── coordinator/ # Central Nervous System |
| 19 | +│ ├── coordinator.ts # Main orchestrator |
| 20 | +│ ├── context-manager.ts # Shared state & repository access |
| 21 | +│ └── task-queue.ts # Task execution & concurrency |
| 22 | +│ |
| 23 | +├── logger/ # Structured logging (extractable) |
| 24 | +│ └── index.ts |
| 25 | +│ |
| 26 | +├── planner/ # Planning agent (stub) |
| 27 | +├── explorer/ # Code exploration agent (stub) |
| 28 | +├── pr/ # GitHub PR agent (stub) |
| 29 | +│ |
| 30 | +└── types.ts # Shared interfaces |
| 31 | +``` |
| 32 | + |
| 33 | +### Self-Contained Design |
| 34 | + |
| 35 | +Each folder is designed to be: |
| 36 | +- **Tree-shakable** - Import only what you need |
| 37 | +- **Extractable** - Easy to pull out into separate packages |
| 38 | +- **Independent** - Minimal cross-dependencies |
| 39 | +- **Testable** - Comprehensive test coverage (90%+) |
| 40 | + |
| 41 | +## Core Components |
| 42 | + |
| 43 | +### SubagentCoordinator |
| 44 | + |
| 45 | +The main orchestrator that manages agent lifecycle, message passing, and task execution. |
| 46 | + |
| 47 | +```typescript |
| 48 | +import { SubagentCoordinator, PlannerAgent } from '@lytics/dev-agent-subagents'; |
| 49 | + |
| 50 | +// Initialize coordinator |
| 51 | +const coordinator = new SubagentCoordinator(); |
| 52 | +await coordinator.initialize({ |
| 53 | + repositoryPath: '/path/to/repo', |
| 54 | + vectorStorePath: '/path/to/vectors', |
| 55 | + maxConcurrentTasks: 5, |
| 56 | +}); |
| 57 | + |
| 58 | +// Register agents |
| 59 | +coordinator.registerAgent(new PlannerAgent()); |
| 60 | + |
| 61 | +// Send messages |
| 62 | +const response = await coordinator.sendMessage({ |
| 63 | + id: 'msg-1', |
| 64 | + type: 'request', |
| 65 | + sender: 'user', |
| 66 | + recipient: 'planner', |
| 67 | + payload: { action: 'create-plan', goal: 'Implement authentication' }, |
| 68 | + timestamp: Date.now(), |
| 69 | +}); |
| 70 | + |
| 71 | +// Get stats |
| 72 | +const stats = coordinator.getStats(); |
| 73 | +console.log(`Active agents: ${stats.agentsRegistered}`); |
| 74 | +``` |
| 75 | + |
| 76 | +### ContextManager |
| 77 | + |
| 78 | +Manages shared state, repository access, and message history. |
| 79 | + |
| 80 | +```typescript |
| 81 | +import { ContextManagerImpl } from '@lytics/dev-agent-subagents'; |
| 82 | + |
| 83 | +const context = new ContextManagerImpl({ maxHistorySize: 1000 }); |
| 84 | + |
| 85 | +// Store shared state |
| 86 | +context.set('currentPhase', 'planning'); |
| 87 | +const phase = context.get('currentPhase'); |
| 88 | + |
| 89 | +// Message history |
| 90 | +context.addToHistory(message); |
| 91 | +const recent = context.getHistory(10); // Last 10 messages |
| 92 | + |
| 93 | +// Repository access |
| 94 | +context.setIndexer(repositoryIndexer); |
| 95 | +const indexer = context.getIndexer(); |
| 96 | +``` |
| 97 | + |
| 98 | +### TaskQueue |
| 99 | + |
| 100 | +Priority-based task queue with concurrency control and retry logic. |
| 101 | + |
| 102 | +```typescript |
| 103 | +import { TaskQueue, CoordinatorLogger } from '@lytics/dev-agent-subagents'; |
| 104 | + |
| 105 | +const logger = new CoordinatorLogger('my-app', 'info'); |
| 106 | +const queue = new TaskQueue(3, logger); // max 3 concurrent |
| 107 | + |
| 108 | +// Enqueue tasks |
| 109 | +queue.enqueue({ |
| 110 | + id: 'task-1', |
| 111 | + type: 'analyze-code', |
| 112 | + agentName: 'explorer', |
| 113 | + payload: { file: 'src/index.ts' }, |
| 114 | + priority: 8, // 0-10, higher = more priority |
| 115 | + status: 'pending', |
| 116 | + createdAt: Date.now(), |
| 117 | + retries: 0, |
| 118 | + maxRetries: 3, |
| 119 | +}); |
| 120 | + |
| 121 | +// Execute tasks |
| 122 | +const next = queue.getNext(); // Highest priority pending task |
| 123 | +if (next && queue.canRunMore()) { |
| 124 | + queue.markRunning(next.id); |
| 125 | + // ... execute task ... |
| 126 | + queue.markCompleted(next.id, result); |
| 127 | +} |
| 128 | + |
| 129 | +// Retry failed tasks |
| 130 | +if (queue.shouldRetry('task-1')) { |
| 131 | + queue.retry('task-1'); |
| 132 | +} |
| 133 | + |
| 134 | +// Stats |
| 135 | +const stats = queue.getStats(); |
| 136 | +console.log(`Pending: ${stats.pending}, Running: ${stats.running}`); |
| 137 | +``` |
| 138 | + |
| 139 | +### Logger |
| 140 | + |
| 141 | +Structured logging with context and log levels (future: `@lytics/croak`). |
| 142 | + |
| 143 | +```typescript |
| 144 | +import { CoordinatorLogger } from '@lytics/dev-agent-subagents'; |
| 145 | + |
| 146 | +const logger = new CoordinatorLogger('my-service', 'info'); |
| 147 | + |
| 148 | +logger.info('Service started', { port: 3000 }); |
| 149 | +logger.warn('High memory usage', { usage: '85%' }); |
| 150 | +logger.error('Connection failed', error, { retries: 3 }); |
| 151 | + |
| 152 | +// Child loggers |
| 153 | +const childLogger = logger.child('database'); |
| 154 | +childLogger.debug('Query executed', { duration: '45ms' }); |
| 155 | +``` |
| 156 | + |
| 157 | +## Message Protocol |
| 158 | + |
| 159 | +All agent communication uses standardized messages: |
| 160 | + |
| 161 | +```typescript |
| 162 | +interface Message { |
| 163 | + id: string; // Unique message ID |
| 164 | + type: 'request' | 'response' | 'event' | 'error'; |
| 165 | + sender: string; // Agent name or 'user' |
| 166 | + recipient: string; // Target agent name |
| 167 | + payload: Record<string, unknown>; |
| 168 | + timestamp: number; |
| 169 | + correlationId?: string; // Link responses to requests |
| 170 | + priority?: number; // 0-10, for task scheduling |
| 171 | + timeout?: number; // ms, for requests |
| 172 | + error?: { |
| 173 | + message: string; |
| 174 | + stack?: string; |
| 175 | + code?: string; |
| 176 | + }; |
| 177 | +} |
| 178 | +``` |
| 179 | + |
| 180 | +## Agent Interface |
| 181 | + |
| 182 | +All agents implement the `Agent` interface: |
| 183 | + |
| 184 | +```typescript |
| 185 | +interface Agent { |
| 186 | + name: string; |
| 187 | + capabilities: string[]; |
| 188 | + |
| 189 | + initialize(context: AgentContext): Promise<void>; |
| 190 | + handleMessage(message: Message): Promise<Message | null>; |
| 191 | + healthCheck(): Promise<boolean>; |
| 192 | + shutdown(): Promise<void>; |
| 193 | +} |
| 194 | +``` |
| 195 | + |
| 196 | +### Creating a Custom Agent |
| 197 | + |
| 198 | +```typescript |
| 199 | +import type { Agent, AgentContext, Message } from '@lytics/dev-agent-subagents'; |
| 200 | + |
| 201 | +class MyCustomAgent implements Agent { |
| 202 | + name = 'my-agent'; |
| 203 | + capabilities = ['analyze', 'summarize']; |
| 204 | + private context?: AgentContext; |
| 205 | + |
| 206 | + async initialize(context: AgentContext): Promise<void> { |
| 207 | + this.context = context; |
| 208 | + this.name = context.agentName; |
| 209 | + context.logger.info('Agent initialized'); |
| 210 | + } |
| 211 | + |
| 212 | + async handleMessage(message: Message): Promise<Message | null> { |
| 213 | + if (!this.context) { |
| 214 | + throw new Error('Agent not initialized'); |
| 215 | + } |
| 216 | + |
| 217 | + // Use repository indexer |
| 218 | + const results = await this.context.indexer.search( |
| 219 | + message.payload.query as string, |
| 220 | + { limit: 5 } |
| 221 | + ); |
| 222 | + |
| 223 | + // Return response |
| 224 | + return { |
| 225 | + id: `${message.id}-response`, |
| 226 | + type: 'response', |
| 227 | + sender: this.name, |
| 228 | + recipient: message.sender, |
| 229 | + correlationId: message.id, |
| 230 | + payload: { results }, |
| 231 | + timestamp: Date.now(), |
| 232 | + }; |
| 233 | + } |
| 234 | + |
| 235 | + async healthCheck(): Promise<boolean> { |
| 236 | + return !!this.context; |
| 237 | + } |
| 238 | + |
| 239 | + async shutdown(): Promise<void> { |
| 240 | + this.context?.logger.info('Agent shutting down'); |
| 241 | + this.context = undefined; |
| 242 | + } |
| 243 | +} |
| 244 | +``` |
| 245 | + |
| 246 | +## Current Agents |
| 247 | + |
| 248 | +### Planner (Stub) |
| 249 | +- **Status**: Basic stub implementation |
| 250 | +- **Capabilities**: `['plan', 'break-down-tasks']` |
| 251 | +- **Future**: Convert GitHub issues to actionable tasks |
| 252 | + |
| 253 | +### Explorer (Stub) |
| 254 | +- **Status**: Basic stub implementation |
| 255 | +- **Capabilities**: `['explore', 'analyze-patterns', 'find-similar']` |
| 256 | +- **Future**: Semantic code exploration and pattern detection |
| 257 | + |
| 258 | +### PR Agent (Stub) |
| 259 | +- **Status**: Basic stub implementation |
| 260 | +- **Capabilities**: `['create-pr', 'update-pr', 'manage-issues', 'comment']` |
| 261 | +- **Future**: GitHub integration for PRs and issues |
| 262 | + |
| 263 | +## Testing |
| 264 | + |
| 265 | +Comprehensive test suite with 90%+ coverage: |
| 266 | + |
| 267 | +```bash |
| 268 | +# Run all subagents tests |
| 269 | +pnpm test packages/subagents |
| 270 | + |
| 271 | +# Watch mode |
| 272 | +cd packages/subagents && pnpm test:watch |
| 273 | + |
| 274 | +# Coverage report |
| 275 | +pnpm vitest run packages/subagents --coverage |
| 276 | +``` |
| 277 | + |
| 278 | +**Test Coverage:** |
| 279 | +- Context Manager: 100% statements, 100% branches |
| 280 | +- Task Queue: 97% statements, 89% branches |
| 281 | +- Logger: 89% statements, 93% branches |
| 282 | + |
| 283 | +## Development |
| 284 | + |
| 285 | +```bash |
| 286 | +# Install dependencies |
| 287 | +pnpm install |
| 288 | + |
| 289 | +# Build |
| 290 | +pnpm -F "@lytics/dev-agent-subagents" build |
| 291 | + |
| 292 | +# Watch mode |
| 293 | +pnpm -F "@lytics/dev-agent-subagents" dev |
| 294 | + |
| 295 | +# Lint |
| 296 | +pnpm -F "@lytics/dev-agent-subagents" lint |
| 297 | + |
| 298 | +# Type check |
| 299 | +pnpm -F "@lytics/dev-agent-subagents" typecheck |
| 300 | +``` |
| 301 | + |
| 302 | +## Design Principles |
| 303 | + |
| 304 | +### 1. Central Nervous System Metaphor |
| 305 | +Every component is named and designed around human physiology: |
| 306 | +- **Coordinator** = Brain |
| 307 | +- **Context Manager** = Hippocampus (memory) |
| 308 | +- **Task Queue** = Motor Cortex (action) |
| 309 | +- **Agents** = Specialized neural regions |
| 310 | + |
| 311 | +### 2. Message-Driven Architecture |
| 312 | +All communication happens through standardized messages, enabling: |
| 313 | +- Async agent execution |
| 314 | +- Message correlation and tracking |
| 315 | +- Priority-based scheduling |
| 316 | +- Timeout handling |
| 317 | + |
| 318 | +### 3. Shared Context |
| 319 | +Agents access shared resources through `AgentContext`: |
| 320 | +- Repository indexer (semantic search) |
| 321 | +- GitHub API (future) |
| 322 | +- Shared state |
| 323 | +- Message history |
| 324 | +- Structured logger |
| 325 | + |
| 326 | +### 4. Graceful Degradation |
| 327 | +System remains operational even if: |
| 328 | +- Individual agents fail |
| 329 | +- Tasks timeout |
| 330 | +- Resources are unavailable |
| 331 | + |
| 332 | +## Future Plans |
| 333 | + |
| 334 | +### Logger Extraction (`@lytics/croak`) |
| 335 | +The logger is designed to be extracted into a standalone package for use across Lytics projects: |
| 336 | + |
| 337 | +```typescript |
| 338 | +// Future: @lytics/croak |
| 339 | +import { Croak } from '@lytics/croak'; |
| 340 | + |
| 341 | +const logger = new Croak('my-service', { |
| 342 | + level: 'info', |
| 343 | + outputs: ['console', 'file'], |
| 344 | + format: 'json', |
| 345 | +}); |
| 346 | +``` |
| 347 | + |
| 348 | +### Agent Implementations |
| 349 | +1. **Planner**: GitHub issue → task breakdown |
| 350 | +2. **Explorer**: Semantic code exploration |
| 351 | +3. **PR Agent**: Automated PR creation and management |
| 352 | + |
| 353 | +### Coordinator Enhancements |
| 354 | +- Agent discovery and registration |
| 355 | +- Health monitoring and auto-restart |
| 356 | +- Metrics and observability |
| 357 | +- Persistent task queue |
| 358 | + |
| 359 | +## Contributing |
| 360 | + |
| 361 | +This is an internal Lytics project, but designed with open-source best practices: |
| 362 | + |
| 363 | +1. **TypeScript strict mode** - Type safety first |
| 364 | +2. **Comprehensive tests** - 90%+ coverage target |
| 365 | +3. **Clear documentation** - READMEs and inline docs |
| 366 | +4. **Self-contained modules** - Easy to extract/refactor |
| 367 | + |
| 368 | +## License |
| 369 | + |
| 370 | +MIT © Lytics, Inc. |
| 371 | + |
0 commit comments