import { Controller, Get, Param, Post, Put, UseGuards, ForbiddenException, ServiceUnavailableException, } from '@nestjs/common'; import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'; import { JwtAuthGuard } from '../auth/jwt-auth.guard'; import { RolesGuard } from '../common/guards/roles.guard'; import { Roles } from '../common/decorators/roles.decorator'; import { OrchestrationService } from './orchestration.service'; // Explicit response allowlists: never serialize arbitrary JSON, signatures, URIs or BigInt. function pick(row: any, keys: string[]) { return Object.fromEntries( keys.filter((k) => row[k] !== undefined).map((k) => [k, row[k]]), ); } function project(row: any) { return { ...pick(row, [ 'id', 'key', 'title', 'department', 'state', 'version', 'operationMode', 'budgetLimitCents', ]), availableActions: [], runs: (row.runs ?? []).map((run: any) => ({ ...pick(run, ['id', 'commitSha', 'environment', 'status']), artifacts: (run.artifacts ?? []).map((a: any) => pick(a, ['id', 'kind', 'sha256']), ), })), decisions: (row.decisions ?? []).map((d: any) => pick(d, [ 'id', 'gate', 'decision', 'expiresAt', 'runId', 'artifactHash', 'commitSha', 'environment', 'createdAt', ]), ), events: (row.events ?? []).map((e: any) => pick(e, [ 'id', 'eventType', 'fromState', 'toState', 'sequence', 'createdAt', ]), ), budgetLedger: (row.budgetLedger ?? []).map((b: any) => pick(b, ['id', 'amountCents', 'kind', 'createdAt']), ), }; } @ApiTags('Admin - Orchestration v4') @ApiBearerAuth() @UseGuards(JwtAuthGuard, RolesGuard) @Roles('Admin', 'SUPER_ADMIN') @Controller('admin/orchestration') export class OrchestrationController { constructor(private readonly service: OrchestrationService) {} @Get('tasks') async all() { return (await this.service.findAll()).map(project); } @Get('tasks/:id') async one(@Param('id') id: string) { return project(await this.service.findOne(id)); } @Get('overview') async overview() { const tasks = await this.service.findAll(); return { items: [ { id: 'tasks', title: 'Persisted task inventory', count: tasks.length }, ], measuredAt: new Date().toISOString(), capabilities: { mutations: false, chat: false, runtime: false }, reason: 'security_hardening_pending', }; } @Get('releases') async releases() { const tasks = await this.service.findAll(); const details = await Promise.all( tasks.map((t: any) => this.service.findOne(t.id)), ); return { items: details.flatMap((t: any) => (t.runs ?? []).map((run: any) => pick(run, ['id', 'status', 'commitSha', 'environment']), ), ), measuredAt: new Date().toISOString(), }; } @Get('agents') agents() { throw new ServiceUnavailableException( 'Hermes runtime unavailable: trusted identity and runtime authentication not configured', ); } @Get('schedules') schedules() { throw new ServiceUnavailableException( 'Hermes schedules unavailable; runtime remains disabled', ); } @Get('model-policy') models() { throw new ServiceUnavailableException( 'No persisted model policy read contract available', ); } // The committed legacy core is not safe to expose. No env switch or role may bypass this boundary. @Post('tasks') create() { return this.deny(); } @Put('tasks/:id/transition') transition() { return this.deny(); } @Post('tasks/:id/gates/:gate/decide') decide() { return this.deny(); } @Post('tasks/:id/budget') budget() { return this.deny(); } @Put('kill-switch') kill() { return this.deny(); } @Post('chat/messages') chat() { return this.deny(); } @Post('tasks/:id/commands/:command') command() { return this.deny(); } @Post('tasks/:id/budget/reconcile') reconcile() { return this.deny(); } private deny(): never { throw new ForbiddenException( 'Orchestration mutations and chat disabled pending hardened core and trusted runtime integration', ); } }