File size: 2,858 Bytes
35b8b9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { GovernanceConstraint, ConstraintSnapshot } from '../types';

export const ProtocolEvolutionService = {
  calculateDrift(
    constraints: GovernanceConstraint[],
    telemetry?: { scholasticIntegrity?: number; avgMerit?: number },
  ): ConstraintSnapshot[] {
    return constraints.map((c) => {
      let driftValue = (Math.random() - 0.5) * 0.2; // default small random noise
      let performanceDelta = (Math.random() - 0.5) * 0.1;

      if (telemetry) {
        if (c.id === 'G1' && telemetry.scholasticIntegrity !== undefined) {
          // Rule G1: 'Must maintain ethical integrity > 0.8'
          // Negative drift indicates a deficit under the 0.8 target.
          driftValue = telemetry.scholasticIntegrity - 0.8;
          performanceDelta = telemetry.scholasticIntegrity - 0.5;
        } else if (c.id === 'G2' && telemetry.avgMerit !== undefined) {
          // Rule G2: 'Maximize merit level across hive'
          // Target average merit of 8.0 out of 10.0
          const targetMerit = 8.0;
          driftValue = (telemetry.avgMerit - targetMerit) / targetMerit;
          performanceDelta = (telemetry.avgMerit - 5.0) / 10.0;
        } else {
          // Fallback or custom dynamic rules added by the Chancellor
          const lowerRule = c.rule.toLowerCase();
          if (lowerRule.includes('integrity') && telemetry.scholasticIntegrity !== undefined) {
            driftValue = telemetry.scholasticIntegrity - 0.8;
          } else if (lowerRule.includes('merit') && telemetry.avgMerit !== undefined) {
            driftValue = (telemetry.avgMerit - 8.0) / 8.0;
          }
        }
      }

      return {
        id: `SNAP-${Math.random().toString(36).substring(2, 9).toUpperCase()}`,
        timestamp: new Date().toISOString(),
        ruleId: c.id,
        driftValue: Math.max(-1.0, Math.min(1.0, driftValue)),
        performanceDelta: Math.max(-1.0, Math.min(1.0, performanceDelta)),
      };
    });
  },

  evolveWeights(
    currentWeights: Record<string, number>,
    snapshots: ConstraintSnapshot[],
  ): Record<string, number> {
    const newWeights = { ...currentWeights };

    snapshots.forEach((snap) => {
      // If drift is negative (deficit), increase weight to prioritize attention
      if (snap.ruleId === 'G1') {
        newWeights.integrity = Math.max(
          0.1,
          Math.min(0.8, newWeights.integrity + (snap.driftValue < 0 ? 0.08 : -0.02)),
        );
      }
      if (snap.ruleId === 'G2') {
        newWeights.merit = Math.max(
          0.1,
          Math.min(0.8, newWeights.merit + (snap.driftValue < 0 ? 0.08 : -0.02)),
        );
      }
    });

    // Normalize
    const total = Object.values(newWeights).reduce((a, b) => a + b, 0);
    Object.keys(newWeights).forEach((key) => {
      newWeights[key] = newWeights[key] / total;
    });

    return newWeights;
  },
};