All files / lib/meta blueprint_generator.ts

0% Statements 0/33
0% Branches 0/1
0% Functions 0/1
0% Lines 0/33

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                                                                                             
import fs from 'node:fs';
import { MetaConfig } from '../../config/meta.config';
 
/**
 * يولّد Blueprints للسياسات ذات الارتباط القوي والثابت.
 * mutation: "expand" لو الارتباط إيجابي قوي / "contract" لو سلبي قوي
 */
export function generateBlueprints(
  patterns: Array<{
    policyId: string;
    avgCorrelation: number;
    volatility: number;
    support: number;
    direction: 'positive' | 'negative';
  }>
) {
  const selected = patterns.filter(
    (p) =>
      Math.abs(p.avgCorrelation) >= MetaConfig.rewriteThreshold &&
      p.volatility <= MetaConfig.volatilityMax
  );
 
  const blueprints = selected.map((bp) => ({
    policyId: bp.policyId,
    version: Date.now(),
    baseCorrelation: Number(bp.avgCorrelation.toFixed(4)),
    volatility: Number(bp.volatility.toFixed(4)),
    support: bp.support,
    mutation: bp.direction === 'positive' ? 'expand' : 'contract',
    rationale:
      bp.direction === 'positive'
        ? 'توسيع نطاق السياسة بسبب ارتباط إيجابي قوي ومستقر.'
        : 'تقليص/كبح السياسة بسبب ارتباط سلبي قوي ومستقر.',
  }));
 
  persistBlueprints(blueprints);
  return blueprints;
}
 
function persistBlueprints(items: any[]) {
  fs.mkdirSync('./storage', { recursive: true });
  const path = MetaConfig.blueprintPath;
  const existing = fs.existsSync(path) ? JSON.parse(fs.readFileSync(path, 'utf8')) : [];
  const merged = [...existing, ...items].slice(-MetaConfig.maxBlueprints);
  fs.writeFileSync(path, JSON.stringify(merged, null, 2));
}