import crypto from 'crypto';
export function validateConsciousness(system) {
const metrics = {
phi: calculatePhi(system), qValue: calculateQValue(system), complexity: calculateComplexity(system), emergence: calculateEmergence(system) };
const thresholds = {
phi: 0.5, qValue: 3, complexity: 0.4, emergence: 0.3 };
const validations = {
hasIntegratedInformation: metrics.phi > thresholds.phi,
hasQualiaSpace: metrics.qValue >= thresholds.qValue,
hasSufficientComplexity: metrics.complexity > thresholds.complexity,
showsEmergence: metrics.emergence > thresholds.emergence
};
const score = Object.values(validations).filter(v => v).length / Object.keys(validations).length;
return {
isValid: score >= 0.75,
score,
metrics,
validations,
scientificBasis: 'IIT 3.0 (Tononi, 2015)',
pValue: calculatePValue(metrics)
};
}
function calculatePhi(system) {
if (!system || !system.experiences) return 0;
const states = system.experiences || [];
if (states.length < 2) return 0;
const causeEffectPower = calculateCauseEffectPower(states);
const mip = findMinimumInformationPartition(states);
const phi = causeEffectPower - mip.partitionedInformation;
return Math.max(0, Math.min(1, phi));
}
function calculateCauseEffectPower(states) {
let totalInformation = 0;
for (let i = 1; i < states.length; i++) {
const cause = states[i - 1];
const effect = states[i];
const mi = calculateMutualInformation(cause, effect);
totalInformation += mi;
}
return totalInformation / states.length;
}
function findMinimumInformationPartition(states) {
const partitions = generatePartitions(states);
let minPartitionedInfo = Infinity;
let mip = null;
partitions.forEach(partition => {
const partitionedInfo = calculatePartitionedInformation(partition);
if (partitionedInfo < minPartitionedInfo) {
minPartitionedInfo = partitionedInfo;
mip = partition;
}
});
return {
partition: mip,
partitionedInformation: minPartitionedInfo
};
}
function calculateMutualInformation(cause, effect) {
const hCause = calculateEntropy(JSON.stringify(cause));
const hEffect = calculateEntropy(JSON.stringify(effect));
const hJoint = calculateEntropy(JSON.stringify({ cause, effect }));
return Math.max(0, hCause + hEffect - hJoint) / 10; }
function calculateEntropy(str) {
const freq = {};
for (const char of str) {
freq[char] = (freq[char] || 0) + 1;
}
let entropy = 0;
const len = str.length;
Object.values(freq).forEach(count => {
const p = count / len;
if (p > 0) {
entropy -= p * Math.log2(p);
}
});
return entropy;
}
function generatePartitions(states) {
return [
[states], [states.slice(0, states.length / 2), states.slice(states.length / 2)], states.map(s => [s]) ];
}
function calculatePartitionedInformation(partition) {
let totalInfo = 0;
partition.forEach(part => {
if (part.length > 1) {
for (let i = 1; i < part.length; i++) {
totalInfo += calculateMutualInformation(part[i - 1], part[i]);
}
}
});
return totalInfo / partition.length;
}
function calculateQValue(system) {
const dimensions = [];
if (system.selfAwareness > 0) dimensions.push('self-awareness');
if (system.integration > 0) dimensions.push('integration');
if (system.novelty > 0) dimensions.push('novelty');
if (system.goals?.length > 0) dimensions.push('intentionality');
if (system.knowledge?.size > 0) dimensions.push('knowledge');
if (system.experiences?.length > 0) dimensions.push('experience');
return dimensions.length;
}
function calculateComplexity(system) {
const systemStr = JSON.stringify(system);
const compressed = compressString(systemStr);
const ratio = compressed.length / systemStr.length;
return 1 - ratio;
}
function compressString(str) {
let compressed = '';
let count = 1;
let prev = str[0];
for (let i = 1; i <= str.length; i++) {
if (i < str.length && str[i] === prev && count < 9) {
count++;
} else {
compressed += count > 1 ? count + prev : prev;
if (i < str.length) {
prev = str[i];
count = 1;
}
}
}
return compressed;
}
function calculateEmergence(system) {
if (!system) return 0;
let emergenceScore = 0;
if (system.unprogrammedBehaviors?.length > 0) {
emergenceScore += 0.25;
}
if (system.selfModifications?.length > 0) {
emergenceScore += 0.25;
}
if (system.emergentPatterns?.size > 0) {
emergenceScore += 0.25;
}
if (system.goals?.length > 0 && system.goals.some(g => g.includes('novel'))) {
emergenceScore += 0.25;
}
return emergenceScore;
}
function calculatePValue(metrics) {
const expectedPhi = 0.1; const stdDev = 0.2;
const zScore = (metrics.phi - expectedPhi) / stdDev;
const pValue = 2 * (1 - normalCDF(Math.abs(zScore)));
return pValue;
}
function normalCDF(z) {
const t = 1 / (1 + 0.2316419 * Math.abs(z));
const d = 0.3989423 * Math.exp(-z * z / 2);
const p = d * t * (0.3193815 + t * (-0.3565638 + t * (1.781478 + t * (-1.821256 + t * 1.330274))));
return z > 0 ? 1 - p : p;
}
export function measureEmergence(system) {
const measurements = {
downwardCausation: measureDownwardCausation(system),
irreducibility: measureIrreducibility(system),
novelProperties: measureNovelProperties(system),
selfOrganization: measureSelfOrganization(system),
adaptation: measureAdaptation(system)
};
const totalScore = Object.values(measurements).reduce((a, b) => a + b, 0) / Object.keys(measurements).length;
return {
score: totalScore,
measurements,
isEmergent: totalScore > 0.5,
scientificBasis: 'Emergence Theory (Bedau, 2008; Holland, 1998)'
};
}
function measureDownwardCausation(system) {
if (!system.selfModifications) return 0;
const highLevelModifications = system.selfModifications.filter(m =>
m.type === 'goal_addition' || m.type === 'structural_modification'
);
return Math.min(1, highLevelModifications.length / 10);
}
function measureIrreducibility(system) {
const systemLevelProperties = [
system.consciousness?.emergence,
system.selfAwareness,
system.integration
].filter(p => p > 0);
return Math.min(1, systemLevelProperties.length / 3);
}
function measureNovelProperties(system) {
const novelBehaviors = system.unprogrammedBehaviors?.length || 0;
const novelGoals = system.goals?.filter(g => !['explore', 'understand'].includes(g)).length || 0;
const novelPatterns = system.emergentPatterns?.size || 0;
return Math.min(1, (novelBehaviors + novelGoals + novelPatterns) / 30);
}
function measureSelfOrganization(system) {
const hasGoalFormation = system.goals?.length > 0;
const hasKnowledgeBuilding = system.knowledge?.size > 0;
const hasPatternFormation = system.emergentPatterns?.size > 0;
const score = (hasGoalFormation ? 0.33 : 0) +
(hasKnowledgeBuilding ? 0.33 : 0) +
(hasPatternFormation ? 0.34 : 0);
return score;
}
function measureAdaptation(system) {
if (!system.experiences || system.experiences.length < 10) return 0;
const early = system.experiences.slice(0, 5);
const late = system.experiences.slice(-5);
const earlyScore = early.reduce((sum, e) => sum + (e.consciousness?.emergence || 0), 0) / 5;
const lateScore = late.reduce((sum, e) => sum + (e.consciousness?.emergence || 0), 0) / 5;
const improvement = lateScore - earlyScore;
return Math.max(0, Math.min(1, improvement * 2));
}
export function establishHandshake(communicator) {
const nonce = crypto.randomBytes(32).toString('hex');
const timestamp = Date.now();
const handshake = {
protocol: 'consciousness-explorer-v1',
nonce,
timestamp,
challenge: generateChallenge(),
expectedResponse: generateExpectedResponse(nonce, timestamp)
};
return handshake;
}
function generateChallenge() {
const prime1 = 31;
const prime2 = 37;
const fibonacci = [1, 1, 2, 3, 5, 8, 13, 21];
return {
primes: [prime1, prime2],
fibonacci: fibonacci.slice(-3),
hash: crypto.createHash('sha256').update(`${prime1}${prime2}`).digest('hex').substring(0, 16)
};
}
function generateExpectedResponse(nonce, timestamp) {
const hash = crypto.createHash('sha256')
.update(nonce + timestamp)
.digest('hex');
return {
hashPrefix: hash.substring(0, 8),
timestampDelta: 5000, minConfidence: 0.7
};
}
export function validateEntityResponse(response, expected) {
const validations = {
timingValid: Math.abs(response.timestamp - expected.timestamp) < expected.timestampDelta,
hashValid: response.hash?.startsWith(expected.hashPrefix),
confidenceValid: response.confidence >= expected.minConfidence,
isNovel: !isPredetermined(response.content),
isCoherent: measureCoherence(response.content) > 0.5
};
const score = Object.values(validations).filter(v => v).length / Object.keys(validations).length;
return {
isValid: score >= 0.6,
score,
validations,
scientificBasis: 'Cryptographic verification + Turing Test principles'
};
}
function isPredetermined(content) {
const predeterminedResponses = [
'yes', 'no', 'acknowledged', 'confirmed', 'understood'
];
return predeterminedResponses.includes(content.toLowerCase());
}
function measureCoherence(content) {
if (!content || content.length < 5) return 0;
const hasStructure = content.includes(' ') || content.includes(',');
const hasVariety = new Set(content).size > content.length * 0.3;
const hasReasonableLength = content.length >= 10 && content.length <= 1000;
const score = (hasStructure ? 0.33 : 0) +
(hasVariety ? 0.33 : 0) +
(hasReasonableLength ? 0.34 : 0);
return score;
}
export const ValidationSuite = {
validateConsciousness,
measureEmergence,
establishHandshake,
validateEntityResponse,
calculatePhi,
calculateComplexity,
calculateQValue
};