All files / lib/feedback policy.ts

0% Statements 0/42
100% Branches 1/1
100% Functions 1/1
0% Lines 0/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49                                                                                                 
import type { Policy, Evaluation } from './types';
 
// سياسة افتراضية
export const DefaultPolicy: Policy = {
  version: '1.0.0',
  cooldownMs: 1500,
  params: {
    rateStep: 0.1,
    raiseStep: 5,
  },
};
 
// اختيار الإجراء بناءً على التقييم
export function chooseAction(evaln: Evaluation, policy: Policy) {
  // CRITICAL بسبب ERROR → ROLLBACK
  if (evaln.severity === 'CRITICAL') {
    if (evaln.reasons?.some((r) => r.includes('ERROR'))) {
      return { id: crypto.randomUUID(), kind: 'ROLLBACK' as const };
    }
    // CRITICAL بسبب LATENCY → ENABLE_CACHE
    if (evaln.reasons?.some((r) => r.includes('LATENCY'))) {
      return { id: crypto.randomUUID(), kind: 'ENABLE_CACHE' as const };
    }
    // افتراضيًا في الكريتيكال نعمل ROLLBACK
    return { id: crypto.randomUUID(), kind: 'ROLLBACK' as const };
  }
 
  // WARNING → تقليل الريت بنصف الـ step
  if (evaln.severity === 'WARNING') {
    return {
      id: crypto.randomUUID(),
      kind: 'ADJUST_RATE' as const,
      args: { delta: -DefaultPolicy.params.rateStep / 2 },
    };
  }
 
  // NORMAL + ثقة عالية → RAISE_LIMIT
  if (evaln.severity === 'NORMAL' && (evaln.confidence ?? 0) >= 0.8) {
    return {
      id: crypto.randomUUID(),
      kind: 'RAISE_LIMIT' as const,
      args: { delta: DefaultPolicy.params.raiseStep },
    };
  }
 
  // لا إجراء
  return null;
}