Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ dependencies = [
"structlog>=24.4.0",
"defusedxml>=0.7.1",
"trafilatura>=1.12.0",
"sentence-transformers>=5.2.2",
"numpy>=2.4.2",
"scipy>=1.17.0",
"networkx>=3.6.1",
]

[project.optional-dependencies]
Expand Down Expand Up @@ -91,6 +95,11 @@ module = [
"googleapiclient.*",
"youtube_transcript_api.*",
"trafilatura.*",
"sentence_transformers.*",
"sklearn.*",
"scipy.*",
"numpy.*",
"networkx.*",
]
ignore_missing_imports = true

Expand Down
Empty file.
65 changes: 65 additions & 0 deletions src/intelstream/noosphere/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import enum
import math

PHI: float = (1 + math.sqrt(5)) / 2
GOLDEN_ANGLE: float = 2 * math.pi / (PHI**2)

FIBONACCI_SEQ: list[int] = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

EMBEDDING_MODEL_MULTILINGUAL: str = "paraphrase-multilingual-MiniLM-L12-v2"
EMBEDDING_MODEL_ENGLISH: str = "all-MiniLM-L6-v2"
EMBEDDING_DIM: int = 384

MIN_MSG_THRESHOLD: int = 20
OUTPUT_GAIN_RECOMPUTE_INTERVAL: float = 300.0
HARD_COOLDOWN_SECONDS: float = 30.0
DEFAULT_ANTHROPHONY_THRESHOLD: float = 0.15
DEFAULT_GAIN_RATIO: float = 4.0

ARCHIVE_BASE_HALF_LIFE_HOURS: float = 168.0
ARCHIVE_REFERENCE_EXTENSION: float = 1.5
ARCHIVE_FIDELITY_FLOOR: float = 0.01


class CrystalRoomMode(str, enum.Enum):
NUMBER_STATION = "number_station"
WHALE = "whale"
GHOST = "ghost"


class CrystalRoomState(str, enum.Enum):
OPEN = "open"
SEALED = "sealed"
BREATHING = "breathing"


class ComputationMode(str, enum.Enum):
SUBTRACTIVE = "subtractive"
BROADCAST = "broadcast"
RESONANT = "resonant"
STIGMERGIC = "stigmergic"
PARASITIC = "parasitic"
PARLIAMENTARY = "parliamentary"
INTEGRATIVE = "integrative"
CRYPTOBIOTIC = "cryptobiotic"
PROJECTIVE = "projective"
TOPOLOGICAL = "topological"


class PathologyType(str, enum.Enum):
CANCER = "non_terminating_pruning"
CYTOKINE_STORM = "receiver_saturation"
SEIZURE = "destructive_sync"
ANT_MILL = "positive_feedback_loop"
ADDICTION = "host_destructive_opt"
AUTOIMMUNE = "perpetual_non_consensus"
GROUPTHINK = "integration_no_diff"
COMA = "irreversible_suspension"
MISUNDERSTANDING = "irrecoverable_dim_loss"
SCHISM = "topological_damage"


class MessageClassification(str, enum.Enum):
ANTHROPHONY = "anthrophony"
BIOPHONY = "biophony"
GEOPHONY = "geophony"
130 changes: 130 additions & 0 deletions src/intelstream/noosphere/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
from datetime import datetime

from sqlalchemy import Float, ForeignKey, Integer, LargeBinary, String, Text, func
from sqlalchemy.orm import Mapped, mapped_column

from intelstream.database.models import Base


class NoosphereGuildState(Base):
__tablename__ = "noosphere_guild_state"
id: Mapped[int] = mapped_column(primary_key=True)
guild_id: Mapped[str] = mapped_column(String(32), unique=True, index=True)
is_active: Mapped[bool] = mapped_column(default=True)
mode: Mapped[str] = mapped_column(String(32), default="integrative")
phi_phase: Mapped[float] = mapped_column(default=0.0)
cryptobiosis: Mapped[bool] = mapped_column(default=False)
cryptobiosis_snapshot: Mapped[str | None] = mapped_column(Text, nullable=True)
last_activity: Mapped[datetime] = mapped_column(default=func.now())
created_at: Mapped[datetime] = mapped_column(default=func.now())


class MessageEmbedding(Base):
__tablename__ = "noosphere_message_embeddings"
id: Mapped[int] = mapped_column(primary_key=True)
guild_id: Mapped[str] = mapped_column(String(32), index=True)
channel_id: Mapped[str] = mapped_column(String(32), index=True)
message_id: Mapped[str] = mapped_column(String(32), unique=True, index=True)
author_id: Mapped[str] = mapped_column(String(32), index=True)
embedding: Mapped[bytes] = mapped_column(LargeBinary)
sentiment: Mapped[float] = mapped_column(Float, default=0.0)
topic_cluster: Mapped[int | None] = mapped_column(Integer, nullable=True)
created_at: Mapped[datetime] = mapped_column(default=func.now())


class EgregoreSnapshot(Base):
__tablename__ = "noosphere_egregore_snapshots"
id: Mapped[int] = mapped_column(primary_key=True)
guild_id: Mapped[str] = mapped_column(String(32), index=True)
channel_id: Mapped[str | None] = mapped_column(String(32), nullable=True, index=True)
semantic_coherence: Mapped[float] = mapped_column(Float)
topic_diversity: Mapped[float] = mapped_column(Float)
vocabulary_convergence: Mapped[float] = mapped_column(Float)
influence_score: Mapped[float] = mapped_column(Float)
created_at: Mapped[datetime] = mapped_column(default=func.now())


class UserBioelectricState(Base):
__tablename__ = "noosphere_user_states"
id: Mapped[int] = mapped_column(primary_key=True)
guild_id: Mapped[str] = mapped_column(String(32), index=True)
user_id: Mapped[str] = mapped_column(String(32), index=True)
activity_frequency: Mapped[float] = mapped_column(Float)
topic_vector: Mapped[bytes] = mapped_column(LargeBinary)
sentiment_avg: Mapped[float] = mapped_column(Float, default=0.0)
engagement_level: Mapped[float] = mapped_column(Float, default=0.0)
last_active: Mapped[datetime] = mapped_column(default=func.now())
created_at: Mapped[datetime] = mapped_column(default=func.now())


class SoundscapeSnapshot(Base):
__tablename__ = "noosphere_soundscape"
id: Mapped[int] = mapped_column(primary_key=True)
guild_id: Mapped[str] = mapped_column(String(32), index=True)
geophony: Mapped[float] = mapped_column(Float)
biophony: Mapped[float] = mapped_column(Float)
anthrophony: Mapped[float] = mapped_column(Float)
bot_anthrophony: Mapped[float] = mapped_column(Float)
health_score: Mapped[float] = mapped_column(Float)
created_at: Mapped[datetime] = mapped_column(default=func.now())


class AttractorSnapshot(Base):
__tablename__ = "noosphere_attractor"
id: Mapped[int] = mapped_column(primary_key=True)
guild_id: Mapped[str] = mapped_column(String(32), index=True)
fractal_dimension: Mapped[float | None] = mapped_column(Float, nullable=True)
lyapunov_exponent: Mapped[float | None] = mapped_column(Float, nullable=True)
curvature: Mapped[float | None] = mapped_column(Float, nullable=True)
created_at: Mapped[datetime] = mapped_column(default=func.now())


class ArchiveEntry(Base):
__tablename__ = "noosphere_archive"
id: Mapped[int] = mapped_column(primary_key=True)
guild_id: Mapped[str] = mapped_column(String(32), index=True)
content: Mapped[str] = mapped_column(Text)
content_hash: Mapped[str] = mapped_column(String(64))
fidelity: Mapped[float] = mapped_column(Float, default=1.0)
interaction_count: Mapped[int] = mapped_column(Integer, default=0)
last_interacted: Mapped[datetime] = mapped_column(default=func.now())
dependencies: Mapped[str | None] = mapped_column(Text, nullable=True)
created_at: Mapped[datetime] = mapped_column(default=func.now())
author_id: Mapped[str] = mapped_column(String(32))


class ArchiveLink(Base):
__tablename__ = "noosphere_archive_links"
id: Mapped[int] = mapped_column(primary_key=True)
source_id: Mapped[int] = mapped_column(ForeignKey("noosphere_archive.id"))
target_id: Mapped[int] = mapped_column(ForeignKey("noosphere_archive.id"))
link_type: Mapped[str] = mapped_column(String(32))
strength: Mapped[float] = mapped_column(Float, default=1.0)
created_at: Mapped[datetime] = mapped_column(default=func.now())


class CrystalRoom(Base):
__tablename__ = "noosphere_crystal_rooms"
id: Mapped[int] = mapped_column(primary_key=True)
guild_id: Mapped[str] = mapped_column(String(32), index=True)
channel_id: Mapped[str] = mapped_column(String(32), unique=True)
mode: Mapped[str] = mapped_column(String(32), default="number_station")
is_sealed: Mapped[bool] = mapped_column(default=False)
tier: Mapped[str] = mapped_column(String(16), default="harmonic")
member_ids: Mapped[str] = mapped_column(Text)
sealed_at: Mapped[datetime | None] = mapped_column(nullable=True)
created_at: Mapped[datetime] = mapped_column(default=func.now())


class GuildMetricsBaseline(Base):
__tablename__ = "noosphere_metrics_baseline"
id: Mapped[int] = mapped_column(primary_key=True)
guild_id: Mapped[str] = mapped_column(String(32), unique=True, index=True)
coherence_mean: Mapped[float] = mapped_column(Float, default=0.0)
coherence_var: Mapped[float] = mapped_column(Float, default=0.0)
convergence_mean: Mapped[float] = mapped_column(Float, default=0.0)
convergence_var: Mapped[float] = mapped_column(Float, default=0.0)
diversity_mean: Mapped[float] = mapped_column(Float, default=0.0)
diversity_var: Mapped[float] = mapped_column(Float, default=0.0)
sample_count: Mapped[int] = mapped_column(Integer, default=0)
updated_at: Mapped[datetime] = mapped_column(default=func.now())
Empty file.
48 changes: 48 additions & 0 deletions src/intelstream/noosphere/shared/archive_decay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from __future__ import annotations

import math

from intelstream.noosphere.constants import (
ARCHIVE_BASE_HALF_LIFE_HOURS,
ARCHIVE_FIDELITY_FLOOR,
ARCHIVE_REFERENCE_EXTENSION,
)


def relevance_score(
entry_age_hours: float,
reference_count: int,
base_half_life: float = ARCHIVE_BASE_HALF_LIFE_HOURS,
extension_factor: float = ARCHIVE_REFERENCE_EXTENSION,
floor: float = ARCHIVE_FIDELITY_FLOOR,
) -> float:
effective_half_life = base_half_life * (extension_factor**reference_count)
if effective_half_life <= 0:
return floor
decay = 0.5 ** (entry_age_hours / effective_half_life)
return float(max(floor, decay))


def compute_fidelity(
created_hours_ago: float,
interaction_timestamps_hours_ago: list[float],
reference_count: int,
base_half_life: float = ARCHIVE_BASE_HALF_LIFE_HOURS,
extension_factor: float = ARCHIVE_REFERENCE_EXTENSION,
boost: float = 0.3,
floor: float = ARCHIVE_FIDELITY_FLOOR,
) -> float:
effective_half_life = base_half_life * (extension_factor**reference_count)
if effective_half_life <= 0:
return floor

decay_rate = math.log(2) / effective_half_life
base_fidelity = math.exp(-decay_rate * created_hours_ago)

interaction_boost = 0.0
for hours_since in interaction_timestamps_hours_ago:
if hours_since >= 0:
interaction_boost += boost * math.exp(-decay_rate * hours_since)

total = base_fidelity + interaction_boost
return max(floor, min(1.0, total))
48 changes: 48 additions & 0 deletions src/intelstream/noosphere/shared/baseline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from __future__ import annotations

import math


class WelfordAccumulator:
"""Numerically stable incremental mean/variance using Welford's algorithm."""

def __init__(self, mean: float = 0.0, variance: float = 0.0, count: int = 0) -> None:
self.mean = mean
self._m2 = variance * count if count > 0 else 0.0
self.count = count

def update(self, value: float) -> None:
self.count += 1
delta = value - self.mean
self.mean += delta / self.count
delta2 = value - self.mean
self._m2 += delta * delta2

@property
def variance(self) -> float:
if self.count < 2:
return 0.0
return self._m2 / self.count

@property
def std(self) -> float:
return math.sqrt(max(self.variance, 1e-12))

def z_score(self, value: float) -> float:
if self.count < 2:
return 0.0
s = self.std
if s < 1e-6:
return 0.0
return (value - self.mean) / s

@staticmethod
def sigmoid(z: float) -> float:
if z > 10:
return 1.0
if z < -10:
return 0.0
return 1.0 / (1.0 + math.exp(-z))

def normalize(self, value: float) -> float:
return self.sigmoid(self.z_score(value))
46 changes: 46 additions & 0 deletions src/intelstream/noosphere/shared/data_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from __future__ import annotations

import math
from dataclasses import dataclass, field
from datetime import datetime # noqa: TC003

import numpy as np # noqa: TC002

from intelstream.noosphere.constants import MessageClassification # noqa: TC001


@dataclass
class ProcessedMessage:
guild_id: str
channel_id: str
user_id: str
message_id: str
content: str
timestamp: datetime
is_bot: bool
classification: MessageClassification
embedding: np.ndarray | None = field(default=None, repr=False)
topic_cluster: int | None = None


@dataclass
class CommunityStateVector:
guild_id: str
timestamp: datetime
semantic_coherence: float = 0.0
vocab_convergence: float = 0.0
topic_entropy: float = 0.0
activity_rate: float = 0.0
anthrophony_ratio: float = 0.0
biophony_ratio: float = 0.0
geophony_ratio: float = 0.0
semantic_momentum: float = 0.0
topic_churn: float = 0.0
reply_depth: float = 0.0
activity_entropy: float = 0.0
egregore_index: float = 0.0
sentiment_alignment: float = math.nan
interaction_modularity: float = math.nan
fractal_dimension: float = math.nan
lyapunov_exponent: float = math.nan
gromov_curvature: float = math.nan
Loading
Loading