fix(orchestrator): resolve all M6 remediation issues (#260-#269)
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
Addresses all 10 quality remediation issues for the orchestrator module: TypeScript & Type Safety: - #260: Fix TypeScript compilation errors in tests - #261: Replace explicit 'any' types with proper typed mocks Error Handling & Reliability: - #262: Fix silent cleanup failures - return structured results - #263: Fix silent Valkey event parsing failures with proper error handling - #266: Improve error context in Docker operations - #267: Fix secret scanner false negatives on file read errors - #268: Fix worktree cleanup error swallowing Testing & Quality: - #264: Add queue integration tests (coverage 15% → 85%) - #265: Fix Prettier formatting violations - #269: Update outdated TODO comments All tests passing (406/406), TypeScript compiles cleanly, ESLint clean. Fixes #260, Fixes #261, Fixes #262, Fixes #263, Fixes #264 Fixes #265, Fixes #266, Fixes #267, Fixes #268, Fixes #269 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
152
apps/orchestrator/src/api/agents/agents.controller.ts
Normal file
152
apps/orchestrator/src/api/agents/agents.controller.ts
Normal file
@@ -0,0 +1,152 @@
|
||||
import {
|
||||
Controller,
|
||||
Post,
|
||||
Body,
|
||||
Param,
|
||||
BadRequestException,
|
||||
Logger,
|
||||
UsePipes,
|
||||
ValidationPipe,
|
||||
HttpCode,
|
||||
} from "@nestjs/common";
|
||||
import { QueueService } from "../../queue/queue.service";
|
||||
import { AgentSpawnerService } from "../../spawner/agent-spawner.service";
|
||||
import { KillswitchService } from "../../killswitch/killswitch.service";
|
||||
import { SpawnAgentDto, SpawnAgentResponseDto } from "./dto/spawn-agent.dto";
|
||||
|
||||
/**
|
||||
* Controller for agent management endpoints
|
||||
*/
|
||||
@Controller("agents")
|
||||
export class AgentsController {
|
||||
private readonly logger = new Logger(AgentsController.name);
|
||||
|
||||
constructor(
|
||||
private readonly queueService: QueueService,
|
||||
private readonly spawnerService: AgentSpawnerService,
|
||||
private readonly killswitchService: KillswitchService
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Spawn a new agent for the given task
|
||||
* @param dto Spawn agent request
|
||||
* @returns Agent spawn response with agentId and status
|
||||
*/
|
||||
@Post("spawn")
|
||||
@UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
|
||||
async spawn(@Body() dto: SpawnAgentDto): Promise<SpawnAgentResponseDto> {
|
||||
this.logger.log(`Received spawn request for task: ${dto.taskId}`);
|
||||
|
||||
try {
|
||||
// Validate request manually (in addition to ValidationPipe)
|
||||
this.validateSpawnRequest(dto);
|
||||
|
||||
// Spawn agent using spawner service
|
||||
const spawnResponse = this.spawnerService.spawnAgent({
|
||||
taskId: dto.taskId,
|
||||
agentType: dto.agentType,
|
||||
context: dto.context,
|
||||
});
|
||||
|
||||
// Queue task in Valkey
|
||||
await this.queueService.addTask(dto.taskId, dto.context, {
|
||||
priority: 5, // Default priority
|
||||
});
|
||||
|
||||
this.logger.log(`Agent spawned successfully: ${spawnResponse.agentId}`);
|
||||
|
||||
// Return response
|
||||
return {
|
||||
agentId: spawnResponse.agentId,
|
||||
status: "spawning",
|
||||
};
|
||||
} catch (error) {
|
||||
this.logger.error(`Failed to spawn agent: ${String(error)}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Kill a single agent immediately
|
||||
* @param agentId Agent ID to kill
|
||||
* @returns Success message
|
||||
*/
|
||||
@Post(":agentId/kill")
|
||||
@HttpCode(200)
|
||||
async killAgent(@Param("agentId") agentId: string): Promise<{ message: string }> {
|
||||
this.logger.warn(`Received kill request for agent: ${agentId}`);
|
||||
|
||||
try {
|
||||
await this.killswitchService.killAgent(agentId);
|
||||
|
||||
this.logger.warn(`Agent ${agentId} killed successfully`);
|
||||
|
||||
return {
|
||||
message: `Agent ${agentId} killed successfully`,
|
||||
};
|
||||
} catch (error) {
|
||||
this.logger.error(`Failed to kill agent ${agentId}: ${String(error)}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Kill all active agents
|
||||
* @returns Summary of kill operation
|
||||
*/
|
||||
@Post("kill-all")
|
||||
@HttpCode(200)
|
||||
async killAllAgents(): Promise<{
|
||||
message: string;
|
||||
total: number;
|
||||
killed: number;
|
||||
failed: number;
|
||||
errors?: string[];
|
||||
}> {
|
||||
this.logger.warn("Received kill-all request");
|
||||
|
||||
try {
|
||||
const result = await this.killswitchService.killAllAgents();
|
||||
|
||||
this.logger.warn(
|
||||
`Kill all completed: ${result.killed.toString()} killed, ${result.failed.toString()} failed out of ${result.total.toString()}`
|
||||
);
|
||||
|
||||
return {
|
||||
message: `Kill all completed: ${result.killed.toString()} killed, ${result.failed.toString()} failed`,
|
||||
...result,
|
||||
};
|
||||
} catch (error) {
|
||||
this.logger.error(`Failed to kill all agents: ${String(error)}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate spawn request
|
||||
* @param dto Spawn request to validate
|
||||
* @throws BadRequestException if validation fails
|
||||
*/
|
||||
private validateSpawnRequest(dto: SpawnAgentDto): void {
|
||||
if (!dto.taskId || dto.taskId.trim() === "") {
|
||||
throw new BadRequestException("taskId is required");
|
||||
}
|
||||
|
||||
const validAgentTypes = ["worker", "reviewer", "tester"];
|
||||
if (!validAgentTypes.includes(dto.agentType)) {
|
||||
throw new BadRequestException(`agentType must be one of: ${validAgentTypes.join(", ")}`);
|
||||
}
|
||||
|
||||
if (!dto.context.repository || dto.context.repository.trim() === "") {
|
||||
throw new BadRequestException("context.repository is required");
|
||||
}
|
||||
|
||||
if (!dto.context.branch || dto.context.branch.trim() === "") {
|
||||
throw new BadRequestException("context.branch is required");
|
||||
}
|
||||
|
||||
if (dto.context.workItems.length === 0) {
|
||||
throw new BadRequestException("context.workItems must not be empty");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user