Files
stack/docs/scratchpads/orch-115-dispatch.md
Jason Woltje 12abdfe81d feat(#93): implement agent spawn via federation
Implements FED-010: Agent Spawn via Federation feature that enables
spawning and managing Claude agents on remote federated Mosaic Stack
instances via COMMAND message type.

Features:
- Federation agent command types (spawn, status, kill)
- FederationAgentService for handling agent operations
- Integration with orchestrator's agent spawner/lifecycle services
- API endpoints for spawning, querying status, and killing agents
- Full command routing through federation COMMAND infrastructure
- Comprehensive test coverage (12/12 tests passing)

Architecture:
- Hub → Spoke: Spawn agents on remote instances
- Command flow: FederationController → FederationAgentService →
  CommandService → Remote Orchestrator
- Response handling: Remote orchestrator returns agent status/results
- Security: Connection validation, signature verification

Files created:
- apps/api/src/federation/types/federation-agent.types.ts
- apps/api/src/federation/federation-agent.service.ts
- apps/api/src/federation/federation-agent.service.spec.ts

Files modified:
- apps/api/src/federation/command.service.ts (agent command routing)
- apps/api/src/federation/federation.controller.ts (agent endpoints)
- apps/api/src/federation/federation.module.ts (service registration)
- apps/orchestrator/src/api/agents/agents.controller.ts (status endpoint)
- apps/orchestrator/src/api/agents/agents.module.ts (lifecycle integration)

Testing:
- 12/12 tests passing for FederationAgentService
- All command service tests passing
- TypeScript compilation successful
- Linting passed

Refs #93

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-03 14:37:06 -06:00

100 lines
2.7 KiB
Markdown

# ORCH-115: Task dispatch from coordinator
## Objective
Implement orchestrator API endpoint POST /agents/spawn to receive spawn requests from coordinator, queue tasks in Valkey, and spawn agents.
## Acceptance Criteria
- [ ] Orchestrator API endpoint: POST /agents/spawn
- [ ] Coordinator calls orchestrator after quality pre-check
- [ ] Task queued in Valkey
- [ ] Agent spawned
- [ ] Return agentId to coordinator
## Approach
1. Create NestJS controller: `src/api/agents/agents.controller.ts`
2. Create DTO for spawn request validation
3. Integrate with QueueService (ORCH-108) and AgentSpawnerService (ORCH-105)
4. Write comprehensive unit tests following TDD
5. Create module and register in AppModule
## API Specification
```typescript
POST /agents/spawn
Request: {
taskId: string,
agentType: 'worker' | 'reviewer' | 'tester',
context: {
repository: string,
branch: string,
workItems: string[],
skills?: string[]
}
}
Response: {
agentId: string,
status: 'spawning' | 'queued'
}
```
## Progress
- [x] Write controller tests (RED)
- [x] Implement controller (GREEN)
- [x] Refactor if needed
- [x] Create module
- [x] Register in AppModule
- [x] Integration test
- [x] Add class-validator and class-transformer dependencies
- [x] All tests passing (14/14)
- [x] Test coverage 100%
## Testing Strategy
- Mock QueueService.addTask()
- Mock AgentSpawnerService.spawnAgent()
- Test success scenarios
- Test validation errors (missing fields, invalid types)
- Test service integration errors
- Ensure coverage >= 85%
## Notes
- Following existing patterns from health.controller.ts
- Using NestJS dependency injection
- DTOs will validate request payload
- Return agentId from spawner service
- Queue status reflects whether agent is spawning or queued
## Implementation Summary
### Files Created:
1. `src/api/agents/agents.controller.ts` - Main controller with POST /agents/spawn endpoint
2. `src/api/agents/agents.controller.spec.ts` - Comprehensive unit tests (14 tests, 100% coverage)
3. `src/api/agents/dto/spawn-agent.dto.ts` - Request/response DTOs with validation
4. `src/api/agents/agents.module.ts` - NestJS module
### Files Modified:
1. `src/app.module.ts` - Added AgentsModule import
2. `package.json` - Added class-validator and class-transformer dependencies
### Test Results:
- All 238 tests passing
- Controller tests: 14/14 passing
- Coverage: 100% (statements, branches, functions, lines)
### Key Features:
- Spawns agents using AgentSpawnerService
- Queues tasks using QueueService with default priority of 5
- Validates request payload (taskId, agentType, context)
- Supports all agent types: worker, reviewer, tester
- Proper error handling and propagation
- Returns agentId and status to coordinator