mycelix-bridge-common 0.1.0

Cross-cluster coordination types and bridge infrastructure for Mycelix
Documentation
// Copyright (C) 2024-2026 Tristan Stoltz / Luminous Dynamics
// SPDX-License-Identifier: AGPL-3.0-or-later
// Commercial licensing: see COMMERCIAL_LICENSE.md at repository root

//! # Consciousness Synchronization — Mycelix Mesh Consciousness Sync
//!
//! Synchronizes consciousness states across CognitiveLoopService nodes in the mesh
//! and computes collective/distributed consciousness emergence properties.

use serde::{Deserialize, Serialize};

/// Shared consciousness state for an agent in the mesh
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsciousnessState {
    /// Unique identifier of the agent
    pub agent_id: String,
    /// Current overall consciousness level (Psi/C_unified)
    pub consciousness_level: f64,
    /// Dominant emotional valence or category
    pub dominant_emotion: String,
    /// Top active goal labels currently prioritized
    pub active_goals: Vec<String>,
}

/// Collective properties emerged from synchronized consciousness states
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CollectiveConsciousness {
    /// Overall group-level Phi (emergent integrated information)
    pub collective_phi: f64,
    /// Active consensus attention/salience focus
    pub collective_salience: Option<String>,
    /// Combined active goals of the unified mesh
    pub group_goals: Vec<String>,
    /// Is distributed GWT ignition active for this collective?
    pub distributed_gwt_active: bool,
}

/// Computes collective consciousness emergence from a set of agent states.
///
/// Under Phase 4.2 guidelines:
/// - N agents synchronize if consciousness levels are within 0.1 of each other.
/// - Social coherence (average trust/alignment metric) must be > 0.6.
/// - Output properties include distributed GWT, consensus salience, and group-level Phi.
pub fn compute_collective_emergence(
    agents: &[ConsciousnessState],
    social_coherence: f64,
) -> Option<CollectiveConsciousness> {
    if agents.len() < 2 {
        return None;
    }

    // Check synchrony: pairwise difference in consciousness levels must be <= 0.1
    let mut synchronized = true;
    for i in 0..agents.len() {
        for j in (i + 1)..agents.len() {
            if (agents[i].consciousness_level - agents[j].consciousness_level).abs() > 0.1 {
                synchronized = false;
                break;
            }
        }
        if !synchronized {
            break;
        }
    }

    // Check threshold conditions: synchrony AND social coherence > 0.6
    if !synchronized || social_coherence <= 0.6 {
        return None;
    }

    // Compute collective Phi (mean level × social coherence)
    let mean_phi = agents.iter().map(|a| a.consciousness_level).sum::<f64>() / agents.len() as f64;
    let collective_phi = mean_phi * social_coherence;

    // Determine consensus salience / attention focus
    // (the goal or category that appears most frequently among agents)
    let mut goal_counts = std::collections::HashMap::new();
    for agent in agents {
        for goal in &agent.active_goals {
            *goal_counts.entry(goal.clone()).or_insert(0) += 1;
        }
    }
    let collective_salience = goal_counts
        .into_iter()
        .max_by_key(|&(_, count)| count)
        .map(|(goal, _)| goal);

    // Group goals: unique union of active goals across all synchronized agents
    let mut group_goals = Vec::new();
    for agent in agents {
        for goal in &agent.active_goals {
            if !group_goals.contains(goal) {
                group_goals.push(goal.clone());
            }
        }
    }

    // Distributed GWT active if collective Phi > 0.4
    let distributed_gwt_active = collective_phi > 0.4;

    Some(CollectiveConsciousness {
        collective_phi,
        collective_salience,
        group_goals,
        distributed_gwt_active,
    })
}