/** * Admission decision only. All policy, context and reservation values must be * obtained from trusted server-side stores; this function does not verify a * user identity, reserve credits, or call INF0 or a model provider. */ export interface ModelAdmissionRequest { tenantId: string; projectId: string; jobId: string; modelId: string; dataClass: string; maxInputTokens: number; maxOutputTokens: number; maximumCredits: number; } export interface ModelExecutionPolicy { tenantId: string; projectId: string; allowedModelIds: readonly string[]; allowedDataClasses: readonly string[]; /** Destination is determined from the operator's verified model registry. */ destination: 'internal' | 'external'; externalTransferAllowed: boolean; maxInputTokens: number; maxOutputTokens: number; } export interface ModelBudgetReservation { tenantId: string; projectId: string; jobId: string; status: 'reserved' | 'settled' | 'released'; credits: number; } export type ModelAdmission = { allowed: true } | { allowed: false; reason: 'invalid_request' | 'scope_mismatch' | 'model_not_allowed' | 'data_class_not_allowed' | 'external_transfer_denied' | 'token_limit_exceeded' | 'reservation_required'; }; export function admitModelExecution( request: ModelAdmissionRequest, policy: ModelExecutionPolicy, reservation: ModelBudgetReservation | undefined, ): ModelAdmission { const names = [request.tenantId, request.projectId, request.jobId, request.modelId, request.dataClass, policy.tenantId, policy.projectId]; if (!names.every((s) => typeof s === 'string' && s.trim()) || !Array.isArray(policy.allowedModelIds) || !Array.isArray(policy.allowedDataClasses) || !['none', 'low', 'medium', 'high', 'restricted'].includes(request.dataClass) || !['internal', 'external'].includes(policy.destination) || typeof policy.externalTransferAllowed !== 'boolean' || ![request.maxInputTokens, request.maxOutputTokens, policy.maxInputTokens, policy.maxOutputTokens] .every((n) => Number.isSafeInteger(n) && n > 0) || !Number.isSafeInteger(request.maximumCredits) || request.maximumCredits < 0) { return { allowed: false, reason: 'invalid_request' }; } if (request.tenantId !== policy.tenantId || request.projectId !== policy.projectId) return { allowed: false, reason: 'scope_mismatch' }; if (!policy.allowedModelIds.includes(request.modelId)) return { allowed: false, reason: 'model_not_allowed' }; if (!policy.allowedDataClasses.includes(request.dataClass)) return { allowed: false, reason: 'data_class_not_allowed' }; if (policy.destination === 'external' && (!policy.externalTransferAllowed || ['high', 'restricted'].includes(request.dataClass))) { return { allowed: false, reason: 'external_transfer_denied' }; } if (request.maxInputTokens > policy.maxInputTokens || request.maxOutputTokens > policy.maxOutputTokens) { return { allowed: false, reason: 'token_limit_exceeded' }; } // maximumCredits === 0 means the operator runs this route without a wallet // (explicit pilot mode), so no reservation is required. Any positive amount // still requires an active, matching reservation. if (request.maximumCredits > 0 && ( !reservation || reservation.tenantId !== request.tenantId || reservation.projectId !== request.projectId || reservation.jobId !== request.jobId || reservation.status !== 'reserved' || !Number.isSafeInteger(reservation.credits) || reservation.credits < request.maximumCredits)) { return { allowed: false, reason: 'reservation_required' }; } return { allowed: true }; }