238 lines
6.6 KiB
TypeScript
238 lines
6.6 KiB
TypeScript
import {
|
|
Controller,
|
|
Body,
|
|
Req,
|
|
Optional,
|
|
ValidationPipe,
|
|
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';
|
|
import {
|
|
PrepareWorkflowService,
|
|
WorkflowIdentity,
|
|
} from './prepare-workflow.service';
|
|
import {
|
|
CreateOrchestrationTaskDto,
|
|
TransitionTaskDto,
|
|
GateDecisionDto,
|
|
} from './dto/orchestration.dto';
|
|
const workflowValidation = new ValidationPipe({
|
|
transform: true,
|
|
whitelist: true,
|
|
forbidNonWhitelisted: true,
|
|
});
|
|
|
|
// 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,
|
|
@Optional() private readonly workflow?: PrepareWorkflowService,
|
|
) {}
|
|
private actor(req: any): WorkflowIdentity {
|
|
const id = req?.user?.id ?? req?.user?.sub;
|
|
// Match RolesGuard aliases without promoting an admin to a reviewer.
|
|
const normalized = typeof req?.user?.role === 'string'
|
|
? req.user.role.toLowerCase().replace(/[_-]/g, '')
|
|
: '';
|
|
const role = normalized === 'admin' ? 'Admin'
|
|
: ['superadmin', 'adminsuper'].includes(normalized) ? 'SUPER_ADMIN'
|
|
: undefined;
|
|
if (
|
|
typeof id !== 'string' ||
|
|
!id ||
|
|
!role
|
|
)
|
|
throw new ForbiddenException('Authenticated workflow identity required');
|
|
return { id, role };
|
|
}
|
|
private safe(row: any, req: any) {
|
|
const view = project(row);
|
|
if (!this.workflow) return view;
|
|
const w = this.workflow.project(row, this.actor(req));
|
|
return {
|
|
...view,
|
|
metadata: w.metadata,
|
|
availableActions: w.availableActions,
|
|
};
|
|
}
|
|
@Get('tasks') async all(@Req() req: any) {
|
|
return (await this.service.findAll()).map((row: any) =>
|
|
this.safe(row, req),
|
|
);
|
|
}
|
|
@Get('tasks/:id') async one(@Param('id') id: string, @Req() req: any) {
|
|
return this.safe(await this.service.findOne(id), req);
|
|
}
|
|
@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: !!this.workflow,
|
|
prepare: !!this.workflow,
|
|
release: false,
|
|
gate2Decisions: false,
|
|
chat: false,
|
|
runtime: false,
|
|
},
|
|
reason: this.workflow
|
|
? 'bounded_stage_prepare_only'
|
|
: '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',
|
|
);
|
|
}
|
|
// Only the separate bounded PREPARE service is exposed; legacy commands remain denied.
|
|
@Post('tasks') async create(
|
|
@Body(workflowValidation) dto: CreateOrchestrationTaskDto,
|
|
@Req() req: any,
|
|
) {
|
|
if (!this.workflow) return this.deny();
|
|
return this.safe(await this.workflow.create(dto, this.actor(req)), req);
|
|
}
|
|
@Put('tasks/:id/transition') async transition(
|
|
@Param('id') id: string,
|
|
@Body(workflowValidation) dto: TransitionTaskDto,
|
|
@Req() req: any,
|
|
) {
|
|
if (!this.workflow) return this.deny();
|
|
return this.safe(
|
|
await this.workflow.transition(id, dto, this.actor(req)),
|
|
req,
|
|
);
|
|
}
|
|
@Post('tasks/:id/gates/:gate/decide') async decide(
|
|
@Param('id') id: string,
|
|
@Param('gate') gate: string,
|
|
@Body() body: unknown,
|
|
@Req() req: any,
|
|
) {
|
|
if (!this.workflow || gate !== '1') return this.deny();
|
|
const dto = await workflowValidation.transform(body, {
|
|
type: 'body',
|
|
metatype: GateDecisionDto,
|
|
});
|
|
return this.safe(
|
|
await this.workflow.decide(id, 1, dto, this.actor(req)),
|
|
req,
|
|
);
|
|
}
|
|
@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',
|
|
);
|
|
}
|
|
}
|