Merge branch 'develop' into feature/229-performance-testing
This commit is contained in:
@@ -6,59 +6,187 @@ Agent orchestration service for Mosaic Stack built with NestJS.
|
||||
|
||||
The Orchestrator is the execution plane of Mosaic Stack, responsible for:
|
||||
|
||||
- Spawning and managing Claude agents
|
||||
- Task queue management (Valkey-backed)
|
||||
- Agent health monitoring and recovery
|
||||
- Git workflow automation
|
||||
- Quality gate enforcement callbacks
|
||||
- Killswitch emergency stop
|
||||
- Spawning and managing Claude agents (worker, reviewer, tester)
|
||||
- Task queue management via BullMQ with Valkey backend
|
||||
- Agent lifecycle state machine (spawning → running → completed/failed/killed)
|
||||
- Git workflow automation with worktree isolation per agent
|
||||
- Quality gate enforcement via Coordinator integration
|
||||
- Killswitch emergency stop with cleanup
|
||||
- Docker sandbox isolation (optional)
|
||||
- Secret scanning on agent commits
|
||||
|
||||
## Architecture
|
||||
|
||||
Part of the Mosaic Stack monorepo at `apps/orchestrator/`.
|
||||
```
|
||||
AppModule
|
||||
├── HealthModule → GET /health, GET /health/ready
|
||||
├── AgentsModule → POST /agents/spawn, GET /agents/:id/status, kill endpoints
|
||||
│ ├── QueueModule → BullMQ task queue (priority 1-10, retry with backoff)
|
||||
│ ├── SpawnerModule → Agent session management, Docker sandbox, lifecycle FSM
|
||||
│ ├── KillswitchModule → Emergency kill + cleanup (Docker, worktree, Valkey state)
|
||||
│ └── ValkeyModule → Distributed state persistence and pub/sub events
|
||||
├── CoordinatorModule → Quality gate checks (typecheck, lint, tests, coverage, AI review)
|
||||
├── GitModule → Clone, branch, commit, push, conflict detection, secret scanning
|
||||
└── MonitorModule → Agent health monitoring (placeholder)
|
||||
```
|
||||
|
||||
Part of the Mosaic Stack monorepo at `apps/orchestrator/`.
|
||||
Controlled by `apps/coordinator/` (Quality Coordinator).
|
||||
Monitored via `apps/web/` (Agent Dashboard).
|
||||
|
||||
## API Reference
|
||||
|
||||
### Health
|
||||
|
||||
| Method | Path | Description |
|
||||
| ------ | --------------- | ----------------- |
|
||||
| GET | `/health` | Uptime and status |
|
||||
| GET | `/health/ready` | Readiness check |
|
||||
|
||||
### Agents
|
||||
|
||||
| Method | Path | Description |
|
||||
| ------ | ------------------------- | ---------------------- |
|
||||
| POST | `/agents/spawn` | Spawn a new agent |
|
||||
| GET | `/agents/:agentId/status` | Get agent status |
|
||||
| POST | `/agents/:agentId/kill` | Kill a single agent |
|
||||
| POST | `/agents/kill-all` | Kill all active agents |
|
||||
|
||||
#### POST /agents/spawn
|
||||
|
||||
```json
|
||||
{
|
||||
"taskId": "string (required)",
|
||||
"agentType": "worker | reviewer | tester",
|
||||
"gateProfile": "strict | standard | minimal | custom (optional)",
|
||||
"context": {
|
||||
"repository": "https://git.example.com/repo.git",
|
||||
"branch": "main",
|
||||
"workItems": ["US-001"],
|
||||
"skills": ["typescript"]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Response:
|
||||
|
||||
```json
|
||||
{
|
||||
"agentId": "uuid",
|
||||
"status": "spawning"
|
||||
}
|
||||
```
|
||||
|
||||
#### GET /agents/:agentId/status
|
||||
|
||||
Response:
|
||||
|
||||
```json
|
||||
{
|
||||
"agentId": "uuid",
|
||||
"taskId": "string",
|
||||
"status": "spawning | running | completed | failed | killed",
|
||||
"spawnedAt": "ISO timestamp",
|
||||
"startedAt": "ISO timestamp (optional)",
|
||||
"completedAt": "ISO timestamp (optional)",
|
||||
"error": "string (optional)"
|
||||
}
|
||||
```
|
||||
|
||||
#### POST /agents/kill-all
|
||||
|
||||
Response:
|
||||
|
||||
```json
|
||||
{
|
||||
"message": "Kill all completed: 3 killed, 0 failed",
|
||||
"total": 3,
|
||||
"killed": 3,
|
||||
"failed": 0,
|
||||
"errors": []
|
||||
}
|
||||
```
|
||||
|
||||
## Services
|
||||
|
||||
| Service | Module | Responsibility |
|
||||
| ------------------------ | ----------- | ---------------------------------------------------- |
|
||||
| AgentSpawnerService | Spawner | Create agent sessions, generate UUIDs, track state |
|
||||
| AgentLifecycleService | Spawner | State machine transitions with Valkey pub/sub events |
|
||||
| DockerSandboxService | Spawner | Container creation with memory/CPU limits |
|
||||
| QueueService | Queue | BullMQ priority queue with exponential backoff retry |
|
||||
| KillswitchService | Killswitch | Emergency agent termination with audit logging |
|
||||
| CleanupService | Killswitch | Multi-step cleanup (Docker, worktree, Valkey state) |
|
||||
| GitOperationsService | Git | Clone, branch, commit, push operations |
|
||||
| WorktreeManagerService | Git | Per-agent worktree isolation |
|
||||
| ConflictDetectionService | Git | Merge conflict detection before push |
|
||||
| SecretScannerService | Git | Detect hardcoded secrets (AWS, API keys, JWTs, etc.) |
|
||||
| ValkeyService | Valkey | Distributed state and event pub/sub |
|
||||
| CoordinatorClientService | Coordinator | HTTP client for quality gate API with retry |
|
||||
| QualityGatesService | Coordinator | Pre-commit and post-commit gate evaluation |
|
||||
|
||||
## Valkey State Keys
|
||||
|
||||
```
|
||||
orchestrator:task:{taskId} → TaskState (status, agentId, context, timestamps)
|
||||
orchestrator:agent:{agentId} → AgentState (status, taskId, timestamps, error)
|
||||
orchestrator:events → Pub/sub channel for lifecycle events
|
||||
```
|
||||
|
||||
## Quality Gate Profiles
|
||||
|
||||
| Profile | Default For | Gates |
|
||||
| -------- | ----------- | --------------------------------------------------------------------- |
|
||||
| strict | reviewer | typecheck, lint, tests, coverage (85%), build, integration, AI review |
|
||||
| standard | worker | typecheck, lint, tests, coverage (85%) |
|
||||
| minimal | tester | tests only |
|
||||
|
||||
## Development
|
||||
|
||||
```bash
|
||||
# Install dependencies (from monorepo root)
|
||||
pnpm install
|
||||
|
||||
# Run in dev mode (watch mode)
|
||||
# Run in dev mode
|
||||
pnpm --filter @mosaic/orchestrator dev
|
||||
|
||||
# Build
|
||||
pnpm --filter @mosaic/orchestrator build
|
||||
|
||||
# Start production
|
||||
pnpm --filter @mosaic/orchestrator start:prod
|
||||
|
||||
# Test
|
||||
# Run unit tests
|
||||
pnpm --filter @mosaic/orchestrator test
|
||||
|
||||
# Generate module (NestJS CLI)
|
||||
cd apps/orchestrator
|
||||
nest generate module <name>
|
||||
nest generate controller <name>
|
||||
nest generate service <name>
|
||||
# Run E2E/integration tests
|
||||
pnpm --filter @mosaic/orchestrator test:e2e
|
||||
|
||||
# Type check
|
||||
pnpm --filter @mosaic/orchestrator typecheck
|
||||
|
||||
# Lint
|
||||
pnpm --filter @mosaic/orchestrator lint
|
||||
```
|
||||
|
||||
## NestJS Architecture
|
||||
## Testing
|
||||
|
||||
- **Modules:** Feature-based organization (spawner, queue, monitor, etc.)
|
||||
- **Controllers:** HTTP endpoints (health, agents, tasks)
|
||||
- **Services:** Business logic
|
||||
- **Providers:** Dependency injection
|
||||
- **Unit tests:** Co-located `*.spec.ts` files (19 test files, 447+ tests)
|
||||
- **Integration tests:** `tests/integration/*.e2e-spec.ts` (17 E2E tests)
|
||||
- **Coverage threshold:** 85% (lines, functions, branches, statements)
|
||||
|
||||
## Configuration
|
||||
|
||||
Environment variables loaded via @nestjs/config.
|
||||
See `.env.example` for required vars.
|
||||
Environment variables loaded via `@nestjs/config`. Key variables:
|
||||
|
||||
## Documentation
|
||||
| Variable | Description |
|
||||
| ------------------- | -------------------------------------- |
|
||||
| `ORCHESTRATOR_PORT` | HTTP port (default: 3001) |
|
||||
| `CLAUDE_API_KEY` | Claude API key for agents |
|
||||
| `VALKEY_HOST` | Valkey/Redis host (default: localhost) |
|
||||
| `VALKEY_PORT` | Valkey/Redis port (default: 6379) |
|
||||
| `COORDINATOR_URL` | Quality Coordinator base URL |
|
||||
| `SANDBOX_ENABLED` | Enable Docker sandbox (true/false) |
|
||||
|
||||
- Architecture: `/docs/ORCHESTRATOR-MONOREPO-SETUP.md`
|
||||
- API Contracts: `/docs/M6-ISSUE-AUDIT.md`
|
||||
## Related Documentation
|
||||
|
||||
- Design: `docs/design/agent-orchestration.md`
|
||||
- Setup: `docs/ORCHESTRATOR-MONOREPO-SETUP.md`
|
||||
- Milestone: M6-AgentOrchestration (0.0.6)
|
||||
|
||||
@@ -13,6 +13,8 @@ describe("AgentsController", () => {
|
||||
};
|
||||
let spawnerService: {
|
||||
spawnAgent: ReturnType<typeof vi.fn>;
|
||||
listAgentSessions: ReturnType<typeof vi.fn>;
|
||||
getAgentSession: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
let lifecycleService: {
|
||||
getAgentLifecycleState: ReturnType<typeof vi.fn>;
|
||||
@@ -30,6 +32,8 @@ describe("AgentsController", () => {
|
||||
|
||||
spawnerService = {
|
||||
spawnAgent: vi.fn(),
|
||||
listAgentSessions: vi.fn(),
|
||||
getAgentSession: vi.fn(),
|
||||
};
|
||||
|
||||
lifecycleService = {
|
||||
@@ -58,6 +62,109 @@ describe("AgentsController", () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
|
||||
describe("listAgents", () => {
|
||||
it("should return empty array when no agents exist", () => {
|
||||
// Arrange
|
||||
spawnerService.listAgentSessions.mockReturnValue([]);
|
||||
|
||||
// Act
|
||||
const result = controller.listAgents();
|
||||
|
||||
// Assert
|
||||
expect(spawnerService.listAgentSessions).toHaveBeenCalled();
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it("should return all agent sessions with mapped status", () => {
|
||||
// Arrange
|
||||
const sessions = [
|
||||
{
|
||||
agentId: "agent-1",
|
||||
taskId: "task-1",
|
||||
agentType: "worker" as const,
|
||||
state: "running" as const,
|
||||
context: {
|
||||
repository: "repo",
|
||||
branch: "main",
|
||||
workItems: [],
|
||||
},
|
||||
spawnedAt: new Date("2026-02-05T12:00:00Z"),
|
||||
},
|
||||
{
|
||||
agentId: "agent-2",
|
||||
taskId: "task-2",
|
||||
agentType: "reviewer" as const,
|
||||
state: "completed" as const,
|
||||
context: {
|
||||
repository: "repo",
|
||||
branch: "main",
|
||||
workItems: [],
|
||||
},
|
||||
spawnedAt: new Date("2026-02-05T11:00:00Z"),
|
||||
completedAt: new Date("2026-02-05T11:30:00Z"),
|
||||
},
|
||||
{
|
||||
agentId: "agent-3",
|
||||
taskId: "task-3",
|
||||
agentType: "tester" as const,
|
||||
state: "failed" as const,
|
||||
context: {
|
||||
repository: "repo",
|
||||
branch: "main",
|
||||
workItems: [],
|
||||
},
|
||||
spawnedAt: new Date("2026-02-05T10:00:00Z"),
|
||||
error: "Test execution failed",
|
||||
},
|
||||
];
|
||||
spawnerService.listAgentSessions.mockReturnValue(sessions);
|
||||
|
||||
// Act
|
||||
const result = controller.listAgents();
|
||||
|
||||
// Assert
|
||||
expect(spawnerService.listAgentSessions).toHaveBeenCalled();
|
||||
expect(result).toHaveLength(3);
|
||||
expect(result[0]).toEqual({
|
||||
agentId: "agent-1",
|
||||
taskId: "task-1",
|
||||
status: "running",
|
||||
agentType: "worker",
|
||||
spawnedAt: "2026-02-05T12:00:00.000Z",
|
||||
completedAt: undefined,
|
||||
error: undefined,
|
||||
});
|
||||
expect(result[1]).toEqual({
|
||||
agentId: "agent-2",
|
||||
taskId: "task-2",
|
||||
status: "completed",
|
||||
agentType: "reviewer",
|
||||
spawnedAt: "2026-02-05T11:00:00.000Z",
|
||||
completedAt: "2026-02-05T11:30:00.000Z",
|
||||
error: undefined,
|
||||
});
|
||||
expect(result[2]).toEqual({
|
||||
agentId: "agent-3",
|
||||
taskId: "task-3",
|
||||
status: "failed",
|
||||
agentType: "tester",
|
||||
spawnedAt: "2026-02-05T10:00:00.000Z",
|
||||
completedAt: undefined,
|
||||
error: "Test execution failed",
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle errors gracefully", () => {
|
||||
// Arrange
|
||||
spawnerService.listAgentSessions.mockImplementation(() => {
|
||||
throw new Error("Service unavailable");
|
||||
});
|
||||
|
||||
// Act & Assert
|
||||
expect(() => controller.listAgents()).toThrow("Failed to list agents: Service unavailable");
|
||||
});
|
||||
});
|
||||
|
||||
describe("spawn", () => {
|
||||
const validRequest = {
|
||||
taskId: "task-123",
|
||||
|
||||
@@ -70,6 +70,47 @@ export class AgentsController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* List all agents
|
||||
* @returns Array of all agent sessions with their status
|
||||
*/
|
||||
@Get()
|
||||
listAgents(): {
|
||||
agentId: string;
|
||||
taskId: string;
|
||||
status: string;
|
||||
agentType: string;
|
||||
spawnedAt: string;
|
||||
completedAt?: string;
|
||||
error?: string;
|
||||
}[] {
|
||||
this.logger.log("Received request to list all agents");
|
||||
|
||||
try {
|
||||
// Get all sessions from spawner service
|
||||
const sessions = this.spawnerService.listAgentSessions();
|
||||
|
||||
// Map to response format
|
||||
const agents = sessions.map((session) => ({
|
||||
agentId: session.agentId,
|
||||
taskId: session.taskId,
|
||||
status: session.state,
|
||||
agentType: session.agentType,
|
||||
spawnedAt: session.spawnedAt.toISOString(),
|
||||
completedAt: session.completedAt?.toISOString(),
|
||||
error: session.error,
|
||||
}));
|
||||
|
||||
this.logger.log(`Found ${agents.length.toString()} agents`);
|
||||
|
||||
return agents;
|
||||
} catch (error: unknown) {
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
this.logger.error(`Failed to list agents: ${errorMessage}`);
|
||||
throw new Error(`Failed to list agents: ${errorMessage}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get agent status
|
||||
* @param agentId Agent ID to query
|
||||
|
||||
242
apps/orchestrator/tests/integration/agent-lifecycle.e2e-spec.ts
Normal file
242
apps/orchestrator/tests/integration/agent-lifecycle.e2e-spec.ts
Normal file
@@ -0,0 +1,242 @@
|
||||
/**
|
||||
* E2E Test: Full Agent Lifecycle
|
||||
*
|
||||
* Tests the complete lifecycle of an agent from spawn to completion/failure.
|
||||
* Uses mocked services to simulate the full flow without external dependencies.
|
||||
*
|
||||
* Lifecycle: spawn → running → completed/failed/killed
|
||||
*
|
||||
* Covers issue #226 (ORCH-125)
|
||||
*/
|
||||
import { describe, it, expect, beforeEach, vi } from "vitest";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { AgentSpawnerService } from "../../src/spawner/agent-spawner.service";
|
||||
import { AgentLifecycleService } from "../../src/spawner/agent-lifecycle.service";
|
||||
import { QueueService } from "../../src/queue/queue.service";
|
||||
import { KillswitchService } from "../../src/killswitch/killswitch.service";
|
||||
import { AgentsController } from "../../src/api/agents/agents.controller";
|
||||
import type { AgentState } from "../../src/valkey/types";
|
||||
|
||||
describe("E2E: Full Agent Lifecycle", () => {
|
||||
let controller: AgentsController;
|
||||
let spawnerService: AgentSpawnerService;
|
||||
let lifecycleService: AgentLifecycleService;
|
||||
let queueService: QueueService;
|
||||
|
||||
const mockConfigService = {
|
||||
get: vi.fn((key: string, defaultValue?: unknown) => {
|
||||
const config: Record<string, unknown> = {
|
||||
"orchestrator.claude.apiKey": "test-api-key",
|
||||
"orchestrator.queue.name": "test-queue",
|
||||
"orchestrator.queue.maxRetries": 3,
|
||||
"orchestrator.queue.baseDelay": 100,
|
||||
"orchestrator.queue.maxDelay": 1000,
|
||||
"orchestrator.valkey.host": "localhost",
|
||||
"orchestrator.valkey.port": 6379,
|
||||
};
|
||||
return config[key] ?? defaultValue;
|
||||
}),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.clearAllMocks();
|
||||
|
||||
// Create real spawner service with mock config
|
||||
spawnerService = new AgentSpawnerService(mockConfigService as unknown as ConfigService);
|
||||
|
||||
// Create mock lifecycle service
|
||||
lifecycleService = {
|
||||
transitionToRunning: vi.fn(),
|
||||
transitionToCompleted: vi.fn(),
|
||||
transitionToFailed: vi.fn(),
|
||||
getAgentLifecycleState: vi.fn(),
|
||||
} as unknown as AgentLifecycleService;
|
||||
|
||||
// Create mock queue service
|
||||
queueService = {
|
||||
addTask: vi.fn().mockResolvedValue(undefined),
|
||||
getStats: vi.fn(),
|
||||
} as unknown as QueueService;
|
||||
|
||||
const killswitchService = {
|
||||
killAgent: vi.fn(),
|
||||
killAllAgents: vi.fn(),
|
||||
} as unknown as KillswitchService;
|
||||
|
||||
controller = new AgentsController(
|
||||
queueService,
|
||||
spawnerService,
|
||||
lifecycleService,
|
||||
killswitchService
|
||||
);
|
||||
});
|
||||
|
||||
describe("Happy path: spawn → queue → track", () => {
|
||||
it("should spawn an agent, register it, and queue the task", async () => {
|
||||
// Step 1: Spawn agent
|
||||
const spawnResult = await controller.spawn({
|
||||
taskId: "e2e-task-001",
|
||||
agentType: "worker",
|
||||
context: {
|
||||
repository: "https://git.example.com/repo.git",
|
||||
branch: "main",
|
||||
workItems: ["US-001"],
|
||||
skills: ["typescript"],
|
||||
},
|
||||
});
|
||||
|
||||
expect(spawnResult.agentId).toBeDefined();
|
||||
expect(spawnResult.status).toBe("spawning");
|
||||
|
||||
// Step 2: Verify agent appears in list
|
||||
const agents = spawnerService.listAgentSessions();
|
||||
expect(agents).toHaveLength(1);
|
||||
expect(agents[0].state).toBe("spawning");
|
||||
expect(agents[0].taskId).toBe("e2e-task-001");
|
||||
|
||||
// Step 3: Verify agent status
|
||||
const session = spawnerService.getAgentSession(spawnResult.agentId);
|
||||
expect(session).toBeDefined();
|
||||
expect(session?.state).toBe("spawning");
|
||||
expect(session?.agentType).toBe("worker");
|
||||
|
||||
// Step 4: Verify task was queued
|
||||
expect(queueService.addTask).toHaveBeenCalledWith(
|
||||
"e2e-task-001",
|
||||
expect.objectContaining({
|
||||
repository: "https://git.example.com/repo.git",
|
||||
branch: "main",
|
||||
}),
|
||||
{ priority: 5 }
|
||||
);
|
||||
});
|
||||
|
||||
it("should track multiple agents spawned sequentially", async () => {
|
||||
// Spawn 3 agents
|
||||
const agents = [];
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const result = await controller.spawn({
|
||||
taskId: `e2e-task-${String(i).padStart(3, "0")}`,
|
||||
agentType: "worker",
|
||||
context: {
|
||||
repository: "https://git.example.com/repo.git",
|
||||
branch: "main",
|
||||
workItems: [`US-${String(i).padStart(3, "0")}`],
|
||||
},
|
||||
});
|
||||
agents.push(result);
|
||||
}
|
||||
|
||||
// Verify all 3 agents are listed
|
||||
const listedAgents = spawnerService.listAgentSessions();
|
||||
expect(listedAgents).toHaveLength(3);
|
||||
|
||||
// Verify each agent has unique ID
|
||||
const agentIds = listedAgents.map((a) => a.agentId);
|
||||
const uniqueIds = new Set(agentIds);
|
||||
expect(uniqueIds.size).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Failure path: spawn → running → failed", () => {
|
||||
it("should handle agent spawn with invalid parameters", async () => {
|
||||
await expect(
|
||||
controller.spawn({
|
||||
taskId: "",
|
||||
agentType: "worker",
|
||||
context: {
|
||||
repository: "https://git.example.com/repo.git",
|
||||
branch: "main",
|
||||
workItems: ["US-001"],
|
||||
},
|
||||
})
|
||||
).rejects.toThrow("taskId is required");
|
||||
});
|
||||
|
||||
it("should reject invalid agent types", async () => {
|
||||
await expect(
|
||||
controller.spawn({
|
||||
taskId: "e2e-task-001",
|
||||
agentType: "invalid" as "worker",
|
||||
context: {
|
||||
repository: "https://git.example.com/repo.git",
|
||||
branch: "main",
|
||||
workItems: ["US-001"],
|
||||
},
|
||||
})
|
||||
).rejects.toThrow("agentType must be one of");
|
||||
});
|
||||
});
|
||||
|
||||
describe("Multi-type agents", () => {
|
||||
it("should support worker, reviewer, and tester agent types", async () => {
|
||||
const types = ["worker", "reviewer", "tester"] as const;
|
||||
|
||||
for (const agentType of types) {
|
||||
const result = await controller.spawn({
|
||||
taskId: `e2e-task-${agentType}`,
|
||||
agentType,
|
||||
context: {
|
||||
repository: "https://git.example.com/repo.git",
|
||||
branch: "main",
|
||||
workItems: ["US-001"],
|
||||
},
|
||||
});
|
||||
|
||||
expect(result.agentId).toBeDefined();
|
||||
expect(result.status).toBe("spawning");
|
||||
}
|
||||
|
||||
const agents = spawnerService.listAgentSessions();
|
||||
expect(agents).toHaveLength(3);
|
||||
|
||||
const agentTypes = agents.map((a) => a.agentType);
|
||||
expect(agentTypes).toContain("worker");
|
||||
expect(agentTypes).toContain("reviewer");
|
||||
expect(agentTypes).toContain("tester");
|
||||
});
|
||||
});
|
||||
|
||||
describe("Agent status tracking", () => {
|
||||
it("should track spawn timestamp", async () => {
|
||||
const before = new Date();
|
||||
|
||||
const result = await controller.spawn({
|
||||
taskId: "e2e-task-time",
|
||||
agentType: "worker",
|
||||
context: {
|
||||
repository: "https://git.example.com/repo.git",
|
||||
branch: "main",
|
||||
workItems: ["US-001"],
|
||||
},
|
||||
});
|
||||
|
||||
const after = new Date();
|
||||
const agents = spawnerService.listAgentSessions();
|
||||
const agent = agents.find((a) => a.agentId === result.agentId);
|
||||
expect(agent).toBeDefined();
|
||||
|
||||
const spawnedAt = new Date(agent!.spawnedAt);
|
||||
expect(spawnedAt.getTime()).toBeGreaterThanOrEqual(before.getTime());
|
||||
expect(spawnedAt.getTime()).toBeLessThanOrEqual(after.getTime());
|
||||
});
|
||||
|
||||
it("should return correct status for each agent", async () => {
|
||||
// Mock lifecycle to return specific states
|
||||
const mockState: AgentState = {
|
||||
agentId: "mock-agent-1",
|
||||
taskId: "e2e-task-001",
|
||||
status: "running",
|
||||
startedAt: new Date().toISOString(),
|
||||
};
|
||||
|
||||
(lifecycleService.getAgentLifecycleState as ReturnType<typeof vi.fn>).mockResolvedValue(
|
||||
mockState
|
||||
);
|
||||
|
||||
const status = await controller.getAgentStatus("mock-agent-1");
|
||||
expect(status.status).toBe("running");
|
||||
expect(status.taskId).toBe("e2e-task-001");
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,218 @@
|
||||
/**
|
||||
* E2E Test: Concurrent Agents
|
||||
*
|
||||
* Tests multiple agents running concurrently with proper isolation.
|
||||
* Verifies agent-level isolation, queue management, and concurrent operations.
|
||||
*
|
||||
* Covers issue #228 (ORCH-127)
|
||||
*/
|
||||
import { describe, it, expect, beforeEach, vi } from "vitest";
|
||||
import { AgentSpawnerService } from "../../src/spawner/agent-spawner.service";
|
||||
import { AgentsController } from "../../src/api/agents/agents.controller";
|
||||
import { QueueService } from "../../src/queue/queue.service";
|
||||
import { AgentLifecycleService } from "../../src/spawner/agent-lifecycle.service";
|
||||
import { KillswitchService } from "../../src/killswitch/killswitch.service";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
|
||||
describe("E2E: Concurrent Agents", () => {
|
||||
let controller: AgentsController;
|
||||
let spawnerService: AgentSpawnerService;
|
||||
|
||||
const mockConfigService = {
|
||||
get: vi.fn((key: string, defaultValue?: unknown) => {
|
||||
const config: Record<string, unknown> = {
|
||||
"orchestrator.claude.apiKey": "test-api-key",
|
||||
};
|
||||
return config[key] ?? defaultValue;
|
||||
}),
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
|
||||
spawnerService = new AgentSpawnerService(mockConfigService as unknown as ConfigService);
|
||||
|
||||
const queueService = {
|
||||
addTask: vi.fn().mockResolvedValue(undefined),
|
||||
} as unknown as QueueService;
|
||||
|
||||
const lifecycleService = {
|
||||
getAgentLifecycleState: vi.fn(),
|
||||
} as unknown as AgentLifecycleService;
|
||||
|
||||
const killswitchService = {
|
||||
killAgent: vi.fn(),
|
||||
killAllAgents: vi.fn(),
|
||||
} as unknown as KillswitchService;
|
||||
|
||||
controller = new AgentsController(
|
||||
queueService,
|
||||
spawnerService,
|
||||
lifecycleService,
|
||||
killswitchService
|
||||
);
|
||||
});
|
||||
|
||||
describe("Concurrent spawning", () => {
|
||||
it("should spawn multiple agents simultaneously without conflicts", async () => {
|
||||
// Spawn 5 agents in parallel
|
||||
const spawnPromises = Array.from({ length: 5 }, (_, i) =>
|
||||
controller.spawn({
|
||||
taskId: `concurrent-task-${String(i)}`,
|
||||
agentType: "worker",
|
||||
context: {
|
||||
repository: "https://git.example.com/repo.git",
|
||||
branch: `feature/task-${String(i)}`,
|
||||
workItems: [`US-${String(i).padStart(3, "0")}`],
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
const results = await Promise.all(spawnPromises);
|
||||
|
||||
// All should succeed
|
||||
expect(results).toHaveLength(5);
|
||||
results.forEach((result) => {
|
||||
expect(result.agentId).toBeDefined();
|
||||
expect(result.status).toBe("spawning");
|
||||
});
|
||||
|
||||
// All IDs should be unique
|
||||
const ids = new Set(results.map((r) => r.agentId));
|
||||
expect(ids.size).toBe(5);
|
||||
|
||||
// All should appear in the list
|
||||
const agents = spawnerService.listAgentSessions();
|
||||
expect(agents).toHaveLength(5);
|
||||
});
|
||||
|
||||
it("should assign unique IDs to every agent even under concurrent load", async () => {
|
||||
const allIds = new Set<string>();
|
||||
const batchSize = 10;
|
||||
|
||||
// Spawn agents in batches
|
||||
for (let batch = 0; batch < 3; batch++) {
|
||||
const promises = Array.from({ length: batchSize }, (_, i) =>
|
||||
controller.spawn({
|
||||
taskId: `batch-${String(batch)}-task-${String(i)}`,
|
||||
agentType: "worker",
|
||||
context: {
|
||||
repository: "https://git.example.com/repo.git",
|
||||
branch: "main",
|
||||
workItems: [`US-${String(batch * batchSize + i)}`],
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
const results = await Promise.all(promises);
|
||||
results.forEach((r) => allIds.add(r.agentId));
|
||||
}
|
||||
|
||||
// All 30 IDs should be unique
|
||||
expect(allIds.size).toBe(30);
|
||||
|
||||
// All 30 should be listed
|
||||
const agents = spawnerService.listAgentSessions();
|
||||
expect(agents).toHaveLength(30);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Mixed agent types concurrently", () => {
|
||||
it("should handle mixed worker/reviewer/tester agents concurrently", async () => {
|
||||
const types = ["worker", "reviewer", "tester"] as const;
|
||||
|
||||
const promises = types.flatMap((agentType, typeIndex) =>
|
||||
Array.from({ length: 3 }, (_, i) =>
|
||||
controller.spawn({
|
||||
taskId: `mixed-${agentType}-${String(i)}`,
|
||||
agentType,
|
||||
context: {
|
||||
repository: "https://git.example.com/repo.git",
|
||||
branch: `branch-${String(typeIndex * 3 + i)}`,
|
||||
workItems: [`US-${String(typeIndex * 3 + i)}`],
|
||||
},
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
const results = await Promise.all(promises);
|
||||
expect(results).toHaveLength(9);
|
||||
|
||||
const agents = spawnerService.listAgentSessions();
|
||||
expect(agents).toHaveLength(9);
|
||||
|
||||
// Verify type distribution
|
||||
const typeCounts = agents.reduce(
|
||||
(acc, a) => {
|
||||
acc[a.agentType] = (acc[a.agentType] ?? 0) + 1;
|
||||
return acc;
|
||||
},
|
||||
{} as Record<string, number>
|
||||
);
|
||||
|
||||
expect(typeCounts["worker"]).toBe(3);
|
||||
expect(typeCounts["reviewer"]).toBe(3);
|
||||
expect(typeCounts["tester"]).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Agent isolation", () => {
|
||||
it("should isolate agent contexts from each other", async () => {
|
||||
const agent1 = await controller.spawn({
|
||||
taskId: "isolated-task-1",
|
||||
agentType: "worker",
|
||||
context: {
|
||||
repository: "https://git.example.com/repo-a.git",
|
||||
branch: "main",
|
||||
workItems: ["US-001"],
|
||||
skills: ["typescript"],
|
||||
},
|
||||
});
|
||||
|
||||
const agent2 = await controller.spawn({
|
||||
taskId: "isolated-task-2",
|
||||
agentType: "reviewer",
|
||||
context: {
|
||||
repository: "https://git.example.com/repo-b.git",
|
||||
branch: "develop",
|
||||
workItems: ["US-002"],
|
||||
skills: ["python"],
|
||||
},
|
||||
});
|
||||
|
||||
// Verify sessions are independent
|
||||
const session1 = spawnerService.getAgentSession(agent1.agentId);
|
||||
const session2 = spawnerService.getAgentSession(agent2.agentId);
|
||||
|
||||
expect(session1?.context.repository).toBe("https://git.example.com/repo-a.git");
|
||||
expect(session2?.context.repository).toBe("https://git.example.com/repo-b.git");
|
||||
expect(session1?.context.branch).toBe("main");
|
||||
expect(session2?.context.branch).toBe("develop");
|
||||
});
|
||||
|
||||
it("should not leak state between concurrent agent operations", async () => {
|
||||
// Spawn agents with different task contexts
|
||||
const spawnPromises = Array.from({ length: 5 }, (_, i) =>
|
||||
controller.spawn({
|
||||
taskId: `leak-test-${String(i)}`,
|
||||
agentType: "worker",
|
||||
context: {
|
||||
repository: `https://git.example.com/repo-${String(i)}.git`,
|
||||
branch: `branch-${String(i)}`,
|
||||
workItems: [`US-${String(i).padStart(3, "0")}`],
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
const results = await Promise.all(spawnPromises);
|
||||
|
||||
// Verify each agent has its own isolated context
|
||||
results.forEach((result, i) => {
|
||||
const session = spawnerService.getAgentSession(result.agentId);
|
||||
expect(session?.taskId).toBe(`leak-test-${String(i)}`);
|
||||
expect(session?.context.repository).toBe(`https://git.example.com/repo-${String(i)}.git`);
|
||||
expect(session?.context.branch).toBe(`branch-${String(i)}`);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
158
apps/orchestrator/tests/integration/killswitch.e2e-spec.ts
Normal file
158
apps/orchestrator/tests/integration/killswitch.e2e-spec.ts
Normal file
@@ -0,0 +1,158 @@
|
||||
/**
|
||||
* E2E Test: Killswitch
|
||||
*
|
||||
* Tests the emergency stop mechanism for terminating agents.
|
||||
* Verifies single agent kill, kill-all, and cleanup operations.
|
||||
*
|
||||
* Covers issue #227 (ORCH-126)
|
||||
*/
|
||||
import { describe, it, expect, beforeEach, vi } from "vitest";
|
||||
import { KillswitchService } from "../../src/killswitch/killswitch.service";
|
||||
import { AgentSpawnerService } from "../../src/spawner/agent-spawner.service";
|
||||
import { AgentsController } from "../../src/api/agents/agents.controller";
|
||||
import { QueueService } from "../../src/queue/queue.service";
|
||||
import { AgentLifecycleService } from "../../src/spawner/agent-lifecycle.service";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
|
||||
describe("E2E: Killswitch", () => {
|
||||
let controller: AgentsController;
|
||||
let spawnerService: AgentSpawnerService;
|
||||
let killswitchService: KillswitchService;
|
||||
|
||||
const mockConfigService = {
|
||||
get: vi.fn((key: string, defaultValue?: unknown) => {
|
||||
const config: Record<string, unknown> = {
|
||||
"orchestrator.claude.apiKey": "test-api-key",
|
||||
};
|
||||
return config[key] ?? defaultValue;
|
||||
}),
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
|
||||
spawnerService = new AgentSpawnerService(mockConfigService as unknown as ConfigService);
|
||||
|
||||
killswitchService = {
|
||||
killAgent: vi.fn().mockResolvedValue(undefined),
|
||||
killAllAgents: vi.fn().mockResolvedValue({
|
||||
total: 3,
|
||||
killed: 3,
|
||||
failed: 0,
|
||||
}),
|
||||
} as unknown as KillswitchService;
|
||||
|
||||
const queueService = {
|
||||
addTask: vi.fn().mockResolvedValue(undefined),
|
||||
} as unknown as QueueService;
|
||||
|
||||
const lifecycleService = {
|
||||
getAgentLifecycleState: vi.fn(),
|
||||
} as unknown as AgentLifecycleService;
|
||||
|
||||
controller = new AgentsController(
|
||||
queueService,
|
||||
spawnerService,
|
||||
lifecycleService,
|
||||
killswitchService
|
||||
);
|
||||
});
|
||||
|
||||
describe("Single agent kill", () => {
|
||||
it("should kill a single agent by ID", async () => {
|
||||
// Spawn an agent first
|
||||
const spawnResult = await controller.spawn({
|
||||
taskId: "kill-test-001",
|
||||
agentType: "worker",
|
||||
context: {
|
||||
repository: "https://git.example.com/repo.git",
|
||||
branch: "main",
|
||||
workItems: ["US-001"],
|
||||
},
|
||||
});
|
||||
|
||||
// Kill the agent
|
||||
const result = await controller.killAgent(spawnResult.agentId);
|
||||
|
||||
expect(result.message).toContain("killed successfully");
|
||||
expect(killswitchService.killAgent).toHaveBeenCalledWith(spawnResult.agentId);
|
||||
});
|
||||
|
||||
it("should handle kill of non-existent agent gracefully", async () => {
|
||||
(killswitchService.killAgent as ReturnType<typeof vi.fn>).mockRejectedValue(
|
||||
new Error("Agent not found")
|
||||
);
|
||||
|
||||
await expect(controller.killAgent("non-existent")).rejects.toThrow("Agent not found");
|
||||
});
|
||||
});
|
||||
|
||||
describe("Kill all agents", () => {
|
||||
it("should kill all active agents", async () => {
|
||||
// Spawn multiple agents to verify they exist before kill-all
|
||||
const spawned = [];
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const result = await controller.spawn({
|
||||
taskId: `kill-all-test-${String(i)}`,
|
||||
agentType: "worker",
|
||||
context: {
|
||||
repository: "https://git.example.com/repo.git",
|
||||
branch: "main",
|
||||
workItems: [`US-${String(i)}`],
|
||||
},
|
||||
});
|
||||
spawned.push(result);
|
||||
}
|
||||
|
||||
// Verify agents were spawned
|
||||
expect(spawnerService.listAgentSessions()).toHaveLength(3);
|
||||
|
||||
// Kill all (mock returns hardcoded result matching spawn count)
|
||||
const result = await controller.killAllAgents();
|
||||
|
||||
expect(result.total).toBe(3);
|
||||
expect(result.killed).toBe(3);
|
||||
expect(result.failed).toBe(0);
|
||||
expect(killswitchService.killAllAgents).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should report partial failures in kill-all", async () => {
|
||||
(killswitchService.killAllAgents as ReturnType<typeof vi.fn>).mockResolvedValue({
|
||||
total: 3,
|
||||
killed: 2,
|
||||
failed: 1,
|
||||
errors: ["Agent abc123 unresponsive"],
|
||||
});
|
||||
|
||||
const result = await controller.killAllAgents();
|
||||
|
||||
expect(result.total).toBe(3);
|
||||
expect(result.killed).toBe(2);
|
||||
expect(result.failed).toBe(1);
|
||||
expect(result.errors).toContain("Agent abc123 unresponsive");
|
||||
});
|
||||
});
|
||||
|
||||
describe("Kill during lifecycle states", () => {
|
||||
it("should be able to kill agent in spawning state", async () => {
|
||||
const spawnResult = await controller.spawn({
|
||||
taskId: "kill-spawning-test",
|
||||
agentType: "worker",
|
||||
context: {
|
||||
repository: "https://git.example.com/repo.git",
|
||||
branch: "main",
|
||||
workItems: ["US-001"],
|
||||
},
|
||||
});
|
||||
|
||||
// Verify agent is spawning
|
||||
const agents = spawnerService.listAgentSessions();
|
||||
const agent = agents.find((a) => a.agentId === spawnResult.agentId);
|
||||
expect(agent?.state).toBe("spawning");
|
||||
|
||||
// Kill should succeed even in spawning state
|
||||
const result = await controller.killAgent(spawnResult.agentId);
|
||||
expect(result.message).toContain("killed successfully");
|
||||
});
|
||||
});
|
||||
});
|
||||
10
apps/orchestrator/tests/integration/vitest.config.ts
Normal file
10
apps/orchestrator/tests/integration/vitest.config.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { defineConfig } from "vitest/config";
|
||||
|
||||
export default defineConfig({
|
||||
test: {
|
||||
globals: true,
|
||||
environment: "node",
|
||||
include: ["**/*.e2e-spec.ts"],
|
||||
testTimeout: 30000,
|
||||
},
|
||||
});
|
||||
@@ -7,76 +7,103 @@ import { Bot, Activity, AlertCircle, CheckCircle, Clock } from "lucide-react";
|
||||
import type { WidgetProps } from "@mosaic/shared";
|
||||
|
||||
interface Agent {
|
||||
id: string;
|
||||
name: string;
|
||||
status: "IDLE" | "WORKING" | "WAITING" | "ERROR" | "TERMINATED";
|
||||
currentTask?: string;
|
||||
lastHeartbeat: string;
|
||||
taskCount: number;
|
||||
agentId: string;
|
||||
taskId: string;
|
||||
status: string;
|
||||
agentType: string;
|
||||
spawnedAt: string;
|
||||
completedAt?: string;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export function AgentStatusWidget({ id: _id, config: _config }: WidgetProps): React.JSX.Element {
|
||||
const [agents, setAgents] = useState<Agent[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
// Mock data for now - will fetch from API later
|
||||
// Fetch agents from orchestrator API
|
||||
useEffect(() => {
|
||||
setIsLoading(true);
|
||||
setTimeout(() => {
|
||||
setAgents([
|
||||
{
|
||||
id: "1",
|
||||
name: "Code Review Agent",
|
||||
status: "WORKING",
|
||||
currentTask: "Reviewing PR #123",
|
||||
lastHeartbeat: new Date().toISOString(),
|
||||
taskCount: 42,
|
||||
},
|
||||
{
|
||||
id: "2",
|
||||
name: "Documentation Agent",
|
||||
status: "IDLE",
|
||||
lastHeartbeat: new Date().toISOString(),
|
||||
taskCount: 15,
|
||||
},
|
||||
{
|
||||
id: "3",
|
||||
name: "Test Runner Agent",
|
||||
status: "ERROR",
|
||||
currentTask: "Failed to run tests",
|
||||
lastHeartbeat: new Date(Date.now() - 300000).toISOString(),
|
||||
taskCount: 28,
|
||||
},
|
||||
]);
|
||||
setIsLoading(false);
|
||||
}, 500);
|
||||
const fetchAgents = async (): Promise<void> => {
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
// Get orchestrator URL from environment or default to localhost
|
||||
const orchestratorUrl = process.env.NEXT_PUBLIC_ORCHESTRATOR_URL ?? "http://localhost:8001";
|
||||
|
||||
const response = await fetch(`${orchestratorUrl}/agents`, {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch agents: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const data = (await response.json()) as Agent[];
|
||||
setAgents(data);
|
||||
} catch (err: unknown) {
|
||||
const errorMessage = err instanceof Error ? err.message : "Unknown error";
|
||||
console.error("Failed to fetch agents:", errorMessage);
|
||||
setError(errorMessage);
|
||||
setAgents([]); // Clear agents on error
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
void fetchAgents();
|
||||
|
||||
// Refresh every 30 seconds
|
||||
const interval = setInterval(() => {
|
||||
void fetchAgents();
|
||||
}, 30000);
|
||||
|
||||
return (): void => {
|
||||
clearInterval(interval);
|
||||
};
|
||||
}, []);
|
||||
|
||||
const getStatusIcon = (status: Agent["status"]): React.JSX.Element => {
|
||||
switch (status) {
|
||||
case "WORKING":
|
||||
const getStatusIcon = (status: string): React.JSX.Element => {
|
||||
const statusLower = status.toLowerCase();
|
||||
switch (statusLower) {
|
||||
case "running":
|
||||
case "working":
|
||||
return <Activity className="w-4 h-4 text-blue-500 animate-pulse" />;
|
||||
case "IDLE":
|
||||
return <Clock className="w-4 h-4 text-gray-400" />;
|
||||
case "WAITING":
|
||||
case "spawning":
|
||||
case "queued":
|
||||
return <Clock className="w-4 h-4 text-yellow-500" />;
|
||||
case "ERROR":
|
||||
case "completed":
|
||||
return <CheckCircle className="w-4 h-4 text-green-500" />;
|
||||
case "failed":
|
||||
case "error":
|
||||
return <AlertCircle className="w-4 h-4 text-red-500" />;
|
||||
case "TERMINATED":
|
||||
case "terminated":
|
||||
case "killed":
|
||||
return <CheckCircle className="w-4 h-4 text-gray-500" />;
|
||||
default:
|
||||
return <Clock className="w-4 h-4 text-gray-400" />;
|
||||
}
|
||||
};
|
||||
|
||||
const getStatusText = (status: Agent["status"]): string => {
|
||||
const getStatusText = (status: string): string => {
|
||||
return status.charAt(0).toUpperCase() + status.slice(1).toLowerCase();
|
||||
};
|
||||
|
||||
const getTimeSinceLastHeartbeat = (timestamp: string): string => {
|
||||
const getAgentName = (agent: Agent): string => {
|
||||
const typeMap: Record<string, string> = {
|
||||
worker: "Worker Agent",
|
||||
reviewer: "Code Review Agent",
|
||||
tester: "Test Runner Agent",
|
||||
};
|
||||
return typeMap[agent.agentType] ?? `${getStatusText(agent.agentType)} Agent`;
|
||||
};
|
||||
|
||||
const getTimeSinceSpawn = (timestamp: string): string => {
|
||||
const now = new Date();
|
||||
const last = new Date(timestamp);
|
||||
const diffMs = now.getTime() - last.getTime();
|
||||
const spawned = new Date(timestamp);
|
||||
const diffMs = now.getTime() - spawned.getTime();
|
||||
|
||||
if (diffMs < 60000) return "Just now";
|
||||
if (diffMs < 3600000) return `${String(Math.floor(diffMs / 60000))}m ago`;
|
||||
@@ -86,9 +113,9 @@ export function AgentStatusWidget({ id: _id, config: _config }: WidgetProps): Re
|
||||
|
||||
const stats = {
|
||||
total: agents.length,
|
||||
working: agents.filter((a) => a.status === "WORKING").length,
|
||||
idle: agents.filter((a) => a.status === "IDLE").length,
|
||||
error: agents.filter((a) => a.status === "ERROR").length,
|
||||
working: agents.filter((a) => a.status.toLowerCase() === "running").length,
|
||||
idle: agents.filter((a) => a.status.toLowerCase() === "spawning").length,
|
||||
error: agents.filter((a) => a.status.toLowerCase() === "failed").length,
|
||||
};
|
||||
|
||||
if (isLoading) {
|
||||
@@ -99,6 +126,17 @@ export function AgentStatusWidget({ id: _id, config: _config }: WidgetProps): Re
|
||||
);
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<div className="flex items-center justify-center h-full">
|
||||
<div className="text-red-500 text-sm">
|
||||
<AlertCircle className="w-4 h-4 inline mr-1" />
|
||||
{error}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex flex-col h-full space-y-3">
|
||||
{/* Summary stats */}
|
||||
@@ -124,15 +162,15 @@ export function AgentStatusWidget({ id: _id, config: _config }: WidgetProps): Re
|
||||
{/* Agent list */}
|
||||
<div className="flex-1 overflow-auto space-y-2">
|
||||
{agents.length === 0 ? (
|
||||
<div className="text-center text-gray-500 text-sm py-4">No agents configured</div>
|
||||
<div className="text-center text-gray-500 text-sm py-4">No agents running</div>
|
||||
) : (
|
||||
agents.map((agent) => (
|
||||
<div
|
||||
key={agent.id}
|
||||
key={agent.agentId}
|
||||
className={`p-3 rounded-lg border ${
|
||||
agent.status === "ERROR"
|
||||
agent.status.toLowerCase() === "failed"
|
||||
? "bg-red-50 border-red-200"
|
||||
: agent.status === "WORKING"
|
||||
: agent.status.toLowerCase() === "running"
|
||||
? "bg-blue-50 border-blue-200"
|
||||
: "bg-gray-50 border-gray-200"
|
||||
}`}
|
||||
@@ -140,7 +178,7 @@ export function AgentStatusWidget({ id: _id, config: _config }: WidgetProps): Re
|
||||
<div className="flex items-center justify-between mb-2">
|
||||
<div className="flex items-center gap-2">
|
||||
<Bot className="w-4 h-4 text-gray-600" />
|
||||
<span className="text-sm font-medium text-gray-900">{agent.name}</span>
|
||||
<span className="text-sm font-medium text-gray-900">{getAgentName(agent)}</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-1 text-xs text-gray-500">
|
||||
{getStatusIcon(agent.status)}
|
||||
@@ -148,13 +186,13 @@ export function AgentStatusWidget({ id: _id, config: _config }: WidgetProps): Re
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{agent.currentTask && (
|
||||
<div className="text-xs text-gray-600 mb-1">{agent.currentTask}</div>
|
||||
)}
|
||||
<div className="text-xs text-gray-600 mb-1">Task: {agent.taskId}</div>
|
||||
|
||||
{agent.error && <div className="text-xs text-red-600 mb-1">{agent.error}</div>}
|
||||
|
||||
<div className="flex items-center justify-between text-xs text-gray-400">
|
||||
<span>{agent.taskCount} tasks completed</span>
|
||||
<span>{getTimeSinceLastHeartbeat(agent.lastHeartbeat)}</span>
|
||||
<span>Agent ID: {agent.agentId.slice(0, 8)}...</span>
|
||||
<span>{getTimeSinceSpawn(agent.spawnedAt)}</span>
|
||||
</div>
|
||||
</div>
|
||||
))
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
import { render, screen, waitFor } from "@testing-library/react";
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
import { AgentStatusWidget } from "../AgentStatusWidget";
|
||||
|
||||
describe("AgentStatusWidget", () => {
|
||||
const mockFetch = vi.fn();
|
||||
|
||||
beforeEach(() => {
|
||||
global.fetch = mockFetch as unknown as typeof fetch;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should render loading state initially", () => {
|
||||
mockFetch.mockImplementation(
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||
() => new Promise(() => {}) // Never resolves
|
||||
);
|
||||
|
||||
render(<AgentStatusWidget id="test-widget" config={{}} />);
|
||||
|
||||
expect(screen.getByText("Loading agents...")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("should fetch and display agents from API", async () => {
|
||||
const mockAgents = [
|
||||
{
|
||||
agentId: "agent-1",
|
||||
taskId: "task-1",
|
||||
status: "running",
|
||||
agentType: "worker",
|
||||
spawnedAt: new Date().toISOString(),
|
||||
},
|
||||
{
|
||||
agentId: "agent-2",
|
||||
taskId: "task-2",
|
||||
status: "completed",
|
||||
agentType: "reviewer",
|
||||
spawnedAt: new Date().toISOString(),
|
||||
completedAt: new Date().toISOString(),
|
||||
},
|
||||
];
|
||||
|
||||
mockFetch.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve(mockAgents),
|
||||
});
|
||||
|
||||
render(<AgentStatusWidget id="test-widget" config={{}} />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText("Worker Agent")).toBeInTheDocument();
|
||||
expect(screen.getByText("Code Review Agent")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
expect(screen.getByText("Task: task-1")).toBeInTheDocument();
|
||||
expect(screen.getByText("Task: task-2")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("should display error message when fetch fails", async () => {
|
||||
mockFetch.mockResolvedValue({
|
||||
ok: false,
|
||||
statusText: "Internal Server Error",
|
||||
});
|
||||
|
||||
render(<AgentStatusWidget id="test-widget" config={{}} />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText(/Failed to fetch agents: Internal Server Error/)).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it("should display no agents message when list is empty", async () => {
|
||||
mockFetch.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve([]),
|
||||
});
|
||||
|
||||
render(<AgentStatusWidget id="test-widget" config={{}} />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText("No agents running")).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it("should display agent error messages", async () => {
|
||||
const mockAgents = [
|
||||
{
|
||||
agentId: "agent-1",
|
||||
taskId: "task-1",
|
||||
status: "failed",
|
||||
agentType: "tester",
|
||||
spawnedAt: new Date().toISOString(),
|
||||
error: "Test execution failed",
|
||||
},
|
||||
];
|
||||
|
||||
mockFetch.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve(mockAgents),
|
||||
});
|
||||
|
||||
render(<AgentStatusWidget id="test-widget" config={{}} />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText("Test execution failed")).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it("should display correct stats summary", async () => {
|
||||
const mockAgents = [
|
||||
{
|
||||
agentId: "agent-1",
|
||||
taskId: "task-1",
|
||||
status: "running",
|
||||
agentType: "worker",
|
||||
spawnedAt: new Date().toISOString(),
|
||||
},
|
||||
{
|
||||
agentId: "agent-2",
|
||||
taskId: "task-2",
|
||||
status: "running",
|
||||
agentType: "reviewer",
|
||||
spawnedAt: new Date().toISOString(),
|
||||
},
|
||||
{
|
||||
agentId: "agent-3",
|
||||
taskId: "task-3",
|
||||
status: "failed",
|
||||
agentType: "tester",
|
||||
spawnedAt: new Date().toISOString(),
|
||||
},
|
||||
];
|
||||
|
||||
mockFetch.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve(mockAgents),
|
||||
});
|
||||
|
||||
render(<AgentStatusWidget id="test-widget" config={{}} />);
|
||||
|
||||
await waitFor(() => {
|
||||
// Check stats: 3 total, 2 working, 0 idle, 1 error
|
||||
const stats = screen.getAllByText(/^[0-9]+$/);
|
||||
expect(stats[0]).toHaveTextContent("3"); // Total
|
||||
expect(stats[1]).toHaveTextContent("2"); // Working
|
||||
expect(stats[2]).toHaveTextContent("0"); // Idle
|
||||
expect(stats[3]).toHaveTextContent("1"); // Error
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user