Chancellor / protocolEvolutionService.ts
VoidBreach's picture
Upload 62 files
35b8b9c verified
Raw History Blame Contribute Delete
2.86 kB
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;
},
};