All files / lib/integration social_adaptation_engine.ts

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

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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
/**
 * Phase 7 — Social Adaptation Engine
 * ----------------------------------
 * يجمع تقارير العقد (Evolution_Report.json لكل عقدة)،
 * يحسب مؤشرات وعي جماعي:
 *  - collectiveStability (متوسط الاستقرار)
 *  - collaborationIndex (مدى التعاون الحقيقي بين العقد)
 *  - metaResonance (تشابه/تماهي الميتا-تفكير بين العقد — باستعمال شبه-تشابه كسيني)
 *  - nextEvolutionDirective (Expand / Contract / Freeze / Merge)
 *
 * يكتب Integration_Report.json + يدعم استقبال تقارير عبر Bus/CFP.
 */
 
import fs from 'node:fs';
import path from 'node:path';
import { Bus } from './integration_bus';
import { buildMessage, validateMessage, isReportBroadcast, CFPMessage } from './cfp_protocol';
 
// =================== Types ===================
 
export interface NodeReport {
  nodeId: string;
  // محتوى Evolution_Report.json (Phase 6 Runner)
  metrics: {
    adaptationRate: number; // 0..1
    stabilityIndex: number; // 0..1
    metaCognitionLevel: number; // 0..1
    nextRecommendedMutation: 'Expand' | 'Contract' | 'Freeze';
  };
  registrySnapshot?: Array<{ policyId: string; weight: number }>;
  // optional: ملف المصدر
  sourcePath?: string;
  trust?: number; // 0..1 default 0.7
}
 
export interface IntegrationReport {
  timestamp: string;
  nodes: Array<{ nodeId: string; trust: number }>;
  metrics: {
    collectiveStability: number;
    collaborationIndex: number;
    metaResonance: number;
    nextEvolutionDirective: 'Expand' | 'Contract' | 'Freeze' | 'Merge';
  };
  notes: {
    interpretation: string;
    guidance: string;
  };
}
 
// =================== Utils ===================
 
function clamp01(x: number) {
  return Math.max(0, Math.min(1, x));
}
function mean(xs: number[]) {
  return xs.length ? xs.reduce((a, b) => a + b, 0) / xs.length : 0;
}
 
function vectorizeRegistry(reg?: Array<{ policyId: string; weight: number }>): Map<string, number> {
  const m = new Map<string, number>();
  (reg ?? []).forEach((r) => m.set(r.policyId, r.weight));
  return m;
}
 
function cosineSimilarity(a: Map<string, number>, b: Map<string, number>) {
  // union keys
  const keys = new Set<string>([...a.keys(), ...b.keys()]);
  let dot = 0,
    na = 0,
    nb = 0;
  for (const k of keys) {
    const va = a.get(k) ?? 0;
    const vb = b.get(k) ?? 0;
    dot += va * vb;
    na += va * va;
    nb += vb * vb;
  }
  if (na === 0 || nb === 0) return 0;
  return clamp01(dot / (Math.sqrt(na) * Math.sqrt(nb)));
}
 
// =================== Core Computations ===================
 
export function computeCollectiveStability(nodes: NodeReport[]): number {
  const s = nodes.map((n) => clamp01(n.metrics.stabilityIndex));
  return clamp01(mean(s));
}
 
/**
 * collaborationIndex:
 * - مزيج من (تشابه السياسات) × (متوسط الثقة بين الأزواج).
 * - بنحسب تشابه كسيني للـ registrySnapshot بين كل زوج ونضرب في trust المتوسط.
 */
export function computeCollaborationIndex(nodes: NodeReport[]): number {
  if (nodes.length <= 1) return 0;
  let acc = 0,
    pairs = 0;
 
  for (let i = 0; i < nodes.length; i++) {
    for (let j = i + 1; j < nodes.length; j++) {
      const A = vectorizeRegistry(nodes[i].registrySnapshot);
      const B = vectorizeRegistry(nodes[j].registrySnapshot);
      const sim = cosineSimilarity(A, B); // 0..1
      const trustAvg = mean([nodes[i].trust ?? 0.7, nodes[j].trust ?? 0.7]); // 0..1
      acc += sim * trustAvg;
      pairs++;
    }
  }
  return pairs > 0 ? clamp01(acc / pairs) : 0;
}
 
/**
 * metaResonance:
 * - متوسط تشابه “التغيرات الموصى بها” (من nextRecommendedMutation + metaCognitionLevel).
 * - نقيسه كسكور بين الأزواج: لو القرار متوافق = 1، مختلف = 0.25؛
 *   ثم نُرجّح بالمتوسط الهندسي لمستويات الميتا.
 */
export function computeMetaResonance(nodes: NodeReport[]): number {
  if (nodes.length <= 1) return 0;
  let acc = 0,
    pairs = 0;
 
  const dirScore = (a: string, b: string) => {
    if (a === b) return 1;
    // اختلاف بسيط (Expand vs Freeze) أقل من (Expand vs Contract)
    const set = new Set([a, b]);
    if (set.has('Expand') && set.has('Contract')) return 0; // تضاد
    return 0.25; // اختلاف غير متضاد
  };
 
  for (let i = 0; i < nodes.length; i++) {
    for (let j = i + 1; j < nodes.length; j++) {
      const di = nodes[i].metrics.nextRecommendedMutation;
      const dj = nodes[j].metrics.nextRecommendedMutation;
      const base = dirScore(di, dj);
      const metaGMean = Math.sqrt(
        clamp01(nodes[i].metrics.metaCognitionLevel) * clamp01(nodes[j].metrics.metaCognitionLevel)
      );
      acc += base * metaGMean; // 0..1
      pairs++;
    }
  }
  return pairs > 0 ? clamp01(acc / pairs) : 0;
}
 
/**
 * القرار الجماعي النهائي:
 * - إن كان collectiveStability مرتفعًا (~>0.8) و metaResonance متوسط/مرتفع:
 *   - لو collaborationIndex أيضًا مرتفع (~>0.7) → "Merge" (تقارب قوي، اندماج سياسات)
 *   - وإلا → "Expand" (توسيع تدريجي)
 * - إن كان metaResonance منخفض (<0.35) → "Freeze" (لتقليل الضوضاء وجمع بيانات)
 * - إن كانت مؤشرات التعاون سلبية (<0.3) مع استقرار منخفض → "Contract"
 */
export function decideDirective(
  collectiveStability: number,
  collaborationIndex: number,
  metaResonance: number
): 'Expand' | 'Contract' | 'Freeze' | 'Merge' {
  const S = collectiveStability;
  const C = collaborationIndex;
  const M = metaResonance;
 
  if (S > 0.8 && M >= 0.5) {
    if (C >= 0.7) return 'Merge';
    return 'Expand';
  }
  if (M < 0.35) return 'Freeze';
  if (S < 0.45 && C < 0.3) return 'Contract';
  // حالة محايدة — توسّع حذر
  return 'Expand';
}
 
// =================== Report Builder ===================
 
export function buildIntegrationReport(nodes: NodeReport[]): IntegrationReport {
  const collectiveStability = clamp01(computeCollectiveStability(nodes));
  const collaborationIndex = clamp01(computeCollaborationIndex(nodes));
  const metaResonance = clamp01(computeMetaResonance(nodes));
  const nextEvolutionDirective = decideDirective(
    collectiveStability,
    collaborationIndex,
    metaResonance
  );
 
  const timestamp = new Date().toISOString();
  const report: IntegrationReport = {
    timestamp,
    nodes: nodes.map((n) => ({ nodeId: n.nodeId, trust: clamp01(n.trust ?? 0.7) })),
    metrics: {
      collectiveStability,
      collaborationIndex,
      metaResonance,
      nextEvolutionDirective,
    },
    notes: {
      interpretation:
        nextEvolutionDirective === 'Merge'
          ? 'تماهٍ جماعي قوي واستقرار مرتفع — اندماج سياسات مناسب.'
          : nextEvolutionDirective === 'Expand'
          ? 'المجتمع مستقر وجاهز لتوسيع تدريجي مشترك.'
          : nextEvolutionDirective === 'Contract'
          ? 'تباعد عالي مع استقرار ضعيف — الإنكماش لتقليل الضرر.'
          : 'تشويش/اختلاف ميتا مرتفع — التجميد المؤقت لجمع بيانات.',
      guidance:
        'نفّذ تغييرات صغيرة (3%–5%) لكل دورة، لا تُعدّل أكثر من Policy حرجة واحدة لكل عقدة، ' +
        'راقب metaResonance بعد كل تطبيق واقعي، وفعّل آلية مراجعة جماعية للأوزان المتقاربة.',
    },
  };
 
  return report;
}
 
// =================== Persistence & Bus Integration ===================
 
export interface SaveOptions {
  outPath?: string;
  ensureDir?: boolean;
}
 
export function saveIntegrationReport(rep: IntegrationReport, opts: SaveOptions = {}) {
  const out = path.resolve(opts.outPath ?? './storage/Integration_Report.json');
  if (opts.ensureDir !== false) {
    fs.mkdirSync(path.dirname(out), { recursive: true });
  }
  fs.writeFileSync(out, JSON.stringify(rep, null, 2));
  return out;
}
 
/**
 * تجميع التقارير من ملفات Evolution_Report.json متعدّدة.
 * تمرير paths لفايلات مختلفة (كل فايل = عقدة) + nodeId لكل واحد.
 */
export function loadNodeReportsFromFiles(
  entries: Array<{ nodeId: string; path: string; trust?: number }>
): NodeReport[] {
  const nodes: NodeReport[] = [];
  for (const e of entries) {
    try {
      const raw = fs.readFileSync(e.path, 'utf8');
      const json = JSON.parse(raw);
 
      const metrics = json?.metrics ?? {};
      const registrySnapshot = json?.registrySnapshot ?? [];
 
      nodes.push({
        nodeId: e.nodeId,
        metrics: {
          adaptationRate: Number(metrics.adaptationRate ?? 0),
          stabilityIndex: Number(metrics.stabilityIndex ?? 0),
          metaCognitionLevel: Number(metrics.metaCognitionLevel ?? 0),
          nextRecommendedMutation: metrics.nextRecommendedMutation ?? 'Freeze',
        },
        registrySnapshot,
        sourcePath: e.path,
        trust: e.trust ?? 0.7,
      });
    } catch {
      // تجاهل أي ملف غير صالح — ممكن لاحقاً نسجل Warning
    }
  }
  return nodes;
}
 
/**
 * تفعيل الاستقبال عبر Bus: أي رسالة CFP من نوع BROADCAST_REPORT
 * تحمل Evolution_Report.json (أو ملخصه) → نضيف/نحدّث NodeReport محلياً ونُصدر Integration_Report جديد.
 */
export class SocialAdaptationEngine {
  private nodes = new Map<string, NodeReport>();
  private lastReport?: IntegrationReport;
 
  constructor(private opts: { autoSave?: boolean; outPath?: string } = {}) {}
 
  ingestNodeReport(n: NodeReport) {
    const trust = clamp01(n.trust ?? 0.7);
    const merged: NodeReport = { ...n, trust };
    this.nodes.set(n.nodeId, merged);
  }
 
  ingestMany(list: NodeReport[]) {
    list.forEach((n) => this.ingestNodeReport(n));
  }
 
  computeAndStore(): IntegrationReport {
    const all = Array.from(this.nodes.values());
    const rep = buildIntegrationReport(all);
    this.lastReport = rep;
    if (this.opts.autoSave !== false) {
      saveIntegrationReport(rep, { outPath: this.opts.outPath, ensureDir: true });
    }
    return rep;
  }
 
  getLastReport() {
    return this.lastReport;
  }
 
  /**
   * يربط المحرك بالباص ليستقبل بث التقارير.
   * نتوقع payload بشكل:
   * { nodeId, report: EvolutionReportLike, trust?, registrySnapshot? }
   */
  bindToBus(bus = Bus) {
    bus.on<CFPMessage>('cfp:*', async (msg) => {
      // الرسائل هنا قد تكون CFPMessage بالفعل — لو اللي بعت بيستخدم Bus.emit("cfp:...", message)
      const maybe = msg as any;
      if (!maybe || typeof maybe !== 'object') return;
 
      const asCFP = maybe as CFPMessage;
      if (!isReportBroadcast(asCFP)) return;
 
      const v = validateMessage(asCFP);
      if (!v.valid) return;
 
      const p = asCFP.payload as any;
      if (!p?.nodeId || !p?.report?.metrics) return;
 
      const node: NodeReport = {
        nodeId: String(p.nodeId),
        metrics: {
          adaptationRate: Number(p.report.metrics.adaptationRate ?? 0),
          stabilityIndex: Number(p.report.metrics.stabilityIndex ?? 0),
          metaCognitionLevel: Number(p.report.metrics.metaCognitionLevel ?? 0),
          nextRecommendedMutation: p.report.metrics.nextRecommendedMutation ?? 'Freeze',
        },
        registrySnapshot: p.report.registrySnapshot ?? [],
        trust: clamp01(Number(asCFP.trust ?? p.trust ?? 0.7)),
      };
 
      this.ingestNodeReport(node);
      const rep = this.computeAndStore();
 
      // الرد على البث برسالة ملخصة (اختياري)
      const reply = buildMessage(
        'SYNC_SNAPSHOT',
        { integration: rep },
        {
          sourceId: 'integration-hub',
          targetId: asCFP.sourceId,
          trust: 0.9,
          meta: { replyTo: asCFP.id, tags: ['auto-reply', 'phase7'] },
        }
      );
 
      await bus.emit('cfp:SYNC_SNAPSHOT:reply', reply);
    });
  }
}