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
// 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,
})
}