import { ForbiddenException, Injectable } from '@nestjs/common'; import { PrismaService } from '../prisma/prisma.service'; import { KillSwitchLevel } from './orchestration.domain'; export type GuardedAction = | 'CREATE_TASK' | 'CLAIM_TASK' | 'RUN_AGENT' | 'MUTATE_PRODUCTION' | 'OBSERVE'; @Injectable() export class KillSwitchService { constructor(private readonly prisma: PrismaService) {} async assertAllowed(action: GuardedAction): Promise { const state = await (this.prisma as any).orchestrationKillSwitch.findUnique( { where: { scope: 'GLOBAL' } }, ); if (!state?.active) return; const blocked: Record = { FREEZE_NEW_WORK: ['CREATE_TASK', 'CLAIM_TASK'], STOP_AGENTS: ['CREATE_TASK', 'CLAIM_TASK', 'RUN_AGENT'], DISABLE_MUTATIONS: ['CREATE_TASK', 'CLAIM_TASK', 'RUN_AGENT', 'MUTATE_PRODUCTION'], EMERGENCY_HALT: [ 'CREATE_TASK', 'CLAIM_TASK', 'RUN_AGENT', 'MUTATE_PRODUCTION', 'OBSERVE', ], }; if (!Object.values(KillSwitchLevel).includes(state.level) || blocked[state.level as KillSwitchLevel].includes(action)) throw new ForbiddenException( `Orchestration kill switch ${state.level} blocks ${action}`, ); } }