"""
Operon: Biologically Inspired Architectures for Agentic Control
===============================================================

Operon brings biological control structures to AI agents using
Applied Category Theory to define rigorous "wiring diagrams".

Core Components:
    - BioAgent: The basic signal-processing agent abstraction
    - Signal: Input messages to agents
    - ActionProtein: Output actions from agents
    - ApprovalToken: Proof-carrying approval for two-key execution
    - IntegrityLabel/Capability: Minimal IFC + effect tags
    - WiringDiagram: Typed wiring diagram checker (WAgent)
    - reviewer_gate / specialist_swarm / advise_topology / skill_organism: pattern-first API

State Management:
    - ATP_Store: Multi-currency metabolic budget (ATP, GTP, NADH)
    - HistoneStore: Epigenetic memory with markers and decay
    - Genome: Immutable configuration with gene expression
    - Telomere: Lifecycle and senescence management

Network Topologies:
    - CoherentFeedForwardLoop: Dual-check guardrails with circuit breaker
    - NegativeFeedbackLoop: Homeostasis and error correction
    - QuorumSensing: Multi-agent consensus voting
    - Cascade: Signal amplification pipeline
    - Oscillator: Periodic task scheduling

Organelles:
    - Membrane: Input filtering and immune defense
    - Mitochondria: Deterministic computation
    - Chaperone: Output validation
    - Ribosome: Prompt synthesis
    - Lysosome: Cleanup and recycling
"""

# =============================================================================
# Core
# =============================================================================
from .core.types import (
    Signal,
    SignalType,
    SignalStrength,
    ActionProtein,
    ActionType,
    IntegrityLabel,
    DataType,
    Capability,
    ApprovalToken,
    FoldedProtein,
    CellState,
)
from .core.wagent import (
    WiringError,
    PortType,
    ModuleSpec,
    Wire,
)
from .core.denature import (
    DenatureFilter,
    SummarizeFilter,
    StripMarkupFilter,
    NormalizeFilter,
)
from .core.coalgebra import (
    Coalgebra,
    StateMachine,
    FunctionalCoalgebra,
    ParallelCoalgebra,
    SequentialCoalgebra,
    TransitionRecord,
    BisimulationResult,
)
from .core.optics import (
    Optic,
    OpticError,
    LensOptic,
    PrismOptic,
    TraversalOptic,
)
from .core.epistemic import (
    TopologyClass,
    EpistemicAnalysis,
    ObservationProfile,
    EpistemicPartition,
    TopologyClassification,
    TopologyRecommendation,
    analyze as epistemic_analyze,
    classify_topology,
)
from .core.wiring_runtime import (
    TypedValue,
    ModuleExecution,
    ExecutionReport,
)

# =============================================================================
# State Management
# =============================================================================
from .state.metabolism import (
    ATP_Store,
    MetabolicState,
    EnergyType,
    EnergyTransaction,
)
from .state.histone import (
    HistoneStore,
    MarkerType,
    MarkerStrength,
    EpigeneticMarker,
    RetrievalResult,
)
from .state.genome import (
    Genome,
    Gene,
    GeneType,
    ExpressionLevel,
    Mutation,
)
from .state.telomere import (
    Telomere,
    TelomereStatus,
    LifecyclePhase,
    SenescenceReason,
)
from .state.development import (
    DevelopmentController,
    DevelopmentConfig,
    DevelopmentalStage,
    DevelopmentStatus,
    CriticalPeriod,
    StageTransition,
)
from .state.dna_repair import (
    DNARepair,
    StateCheckpoint,
    CorruptionType,
    DamageSeverity,
    DamageReport,
    RepairStrategy,
)

# =============================================================================
# Topologies
# =============================================================================
from .topology.loops import (
    CoherentFeedForwardLoop,
    NegativeFeedbackLoop,
    GateLogic,
    CircuitState,
    LoopResult,
)
from .topology.quorum import (
    QuorumSensing,
    EmergencyQuorum,
    VotingStrategy,
    VoteType,
    Vote,
    QuorumResult,
)
from .topology.cascade import (
    Cascade,
    AgentCascade,
    MAPKCascade,
    CascadeStage,
    CascadeResult,
    StageResult,
    StageStatus,
    CascadeMode,
)
from .topology.oscillator import (
    Oscillator,
    CircadianOscillator,
    HeartbeatOscillator,
    CellCycleOscillator,
    OscillatorPhase,
    OscillatorState,
    OscillatorStatus,
    CycleResult,
    WaveformType,
)

# =============================================================================
# Organelles
# =============================================================================
from .organelles.membrane import (
    Membrane,
    ThreatLevel,
    ThreatSignature,
    FilterResult,
)
from .organelles.mitochondria import (
    Mitochondria,
    MetabolicPathway,
    ATP,
    MetabolicResult,
    Tool,
    SimpleTool,
)
from .organelles.chaperone import (
    Chaperone,
    FoldingStrategy,
    FoldingAttempt,
    EnhancedFoldedProtein,
)
from .organelles.ribosome import (
    Ribosome,
    mRNA,
    tRNA,
    Protein,
    Codon,
    CodonType,
)
from .organelles.lysosome import (
    Lysosome,
    Waste,
    WasteType,
    DigestResult,
)
from .organelles.nucleus import (
    Nucleus,
    Transcription,
)
from .organelles.plasmid import (
    Plasmid,
    PlasmidRegistry,
    PlasmidError,
    AcquisitionResult,
)

# =============================================================================
# Memory
# =============================================================================
from .memory import (
    MemoryTier,
    MemoryEntry,
    EpisodicMemory,
    BiTemporalFact,
    BiTemporalQuery,
    BiTemporalMemory,
    FactSnapshot,
    CorrectionResult,
)

# =============================================================================
# Providers
# =============================================================================
from .providers import (
    LLMProvider,
    LLMResponse,
    ProviderConfig,
    ToolSchema,
    ToolCall,
    ToolResult,
    MockProvider,
    OpenAIProvider,
    OpenAICompatibleProvider,
    AnthropicProvider,
    GeminiProvider,
    NucleusError,
    ProviderUnavailableError,
    QuotaExhaustedError,
    TranscriptionFailedError,
)

# =============================================================================
# Healing (Self-Repair Mechanisms)
# =============================================================================
from .healing import (
    ChaperoneLoop,
    HealingResult,
    HealingOutcome,
    RefoldingAttempt,
    RegenerativeSwarm,
    SwarmResult,
    SimpleWorker,
    WorkerMemory,
    WorkerStatus,
    AutophagyDaemon,
    PruneResult,
    ContextMetrics,
    ContextHealthStatus,
)

# =============================================================================
# Health (Epistemic Monitoring)
# =============================================================================
from .health import (
    EpiplexityMonitor,
    EpiplexityState,
    EpiplexityResult,
    HealthStatus,
    EmbeddingProvider,
    MockEmbeddingProvider,
)

# =============================================================================
# Surveillance - Innate Immunity
# =============================================================================
from .surveillance import (
    InnateImmunity,
    InnateCheckResult,
    TLRPattern,
    PAMPCategory,
    InflammationLevel,
    InflammationState,
    InflammationResponse,
    JSONValidator,
    LengthValidator,
    CharacterSetValidator,
)

# =============================================================================
# Coordination - Morphogen Gradients
# =============================================================================
from .coordination import (
    MorphogenType,
    MorphogenValue,
    MorphogenGradient,
    GradientUpdate,
    GradientOrchestrator,
    PhenotypeConfig,
    MorphogenSource,
    DiffusionParams,
    DiffusionField,
    SocialLearning,
    PeerExchange,
    TrustRegistry,
    AdoptionResult,
    AdoptionOutcome,
    ScaffoldingResult,
    AutoinducerSignal,
    SignalEnvironment,
    QuorumSensingBio,
)

# =============================================================================
# Multi-cellular Organization
# =============================================================================
from .multicell import (
    ExpressionProfile,
    CellType,
    DifferentiatedCell,
    TissueBoundary,
    TissueError,
    Tissue,
)
from .state.metabolism import MetabolicAccessPolicy

# =============================================================================
# Pattern-First API
# =============================================================================
from .healing import (
    SleepConsolidation,
    ConsolidationResult,
    CounterfactualResult,
    counterfactual_replay,
)
from .patterns import (
    AdaptiveRunResult,
    AdaptiveSkillOrganism,
    CognitiveMode,
    ExperienceRecord,
    InterventionKind,
    PatternLibrary,
    PatternRunRecord,
    PatternTemplate,
    ReviewerGate,
    ReviewerGateConfig,
    ReviewerGateResult,
    reviewer_gate,
    SignalCategory,
    SkillOrganism,
    SkillRunResult,
    SkillRuntimeComponent,
    SkillStage,
    SkillStageResult,
    SubstrateView,
    SpecialistSwarm,
    SpecialistSwarmConfig,
    SpecialistSwarmResult,
    specialist_swarm,
    TaskFingerprint,
    TelemetryEvent,
    TelemetryProbe,
    TopologyAdvice,
    WatcherComponent,
    WatcherConfig,
    WatcherIntervention,
    WatcherSignal,
    adaptive_skill_organism,
    advise_topology,
    assemble_pattern,
    CLIResult,
    cli_handler,
    cli_organism,
    consolidate,
    managed_organism,
    ManagedOrganism,
    ManagedRunResult,
    resolve_cognitive_mode,
    skill_organism,
)

# =============================================================================
# Public API
# =============================================================================
__all__ = [
    # Core
    "Signal",
    "SignalType",
    "SignalStrength",
    "ActionProtein",
    "ActionType",
    "IntegrityLabel",
    "DataType",
    "Capability",
    "ApprovalToken",
    "FoldedProtein",
    "CellState",
    "WiringError",
    "PortType",
    "ModuleSpec",
    "Wire",
    "DenatureFilter",
    "SummarizeFilter",
    "StripMarkupFilter",
    "NormalizeFilter",
    # Coalgebra (Paper §3.5)
    "Coalgebra",
    "StateMachine",
    "FunctionalCoalgebra",
    "ParallelCoalgebra",
    "SequentialCoalgebra",
    "TransitionRecord",
    "BisimulationResult",
    # Optics (Paper §3.4)
    "Optic",
    "OpticError",
    "LensOptic",
    "PrismOptic",
    "TraversalOptic",
    # Epistemic Topology (Paper §6.5.4)
    "TopologyClass",
    "EpistemicAnalysis",
    "ObservationProfile",
    "EpistemicPartition",
    "TopologyClassification",
    "TopologyRecommendation",
    "epistemic_analyze",
    "classify_topology",
    "TypedValue",
    "ModuleExecution",
    "ExecutionReport",
    # Healing
    "SleepConsolidation",
    "ConsolidationResult",
    "CounterfactualResult",
    "counterfactual_replay",
    # Pattern-First API
    "AdaptiveRunResult",
    "AdaptiveSkillOrganism",
    "CognitiveMode",
    "ExperienceRecord",
    "InterventionKind",
    "PatternLibrary",
    "PatternRunRecord",
    "PatternTemplate",
    "ReviewerGate",
    "ReviewerGateConfig",
    "ReviewerGateResult",
    "reviewer_gate",
    "SignalCategory",
    "SkillOrganism",
    "SkillRunResult",
    "SkillRuntimeComponent",
    "SkillStage",
    "SkillStageResult",
    "SubstrateView",
    "SpecialistSwarm",
    "SpecialistSwarmConfig",
    "SpecialistSwarmResult",
    "specialist_swarm",
    "TaskFingerprint",
    "TelemetryEvent",
    "TelemetryProbe",
    "TopologyAdvice",
    "WatcherComponent",
    "WatcherConfig",
    "WatcherIntervention",
    "WatcherSignal",
    "adaptive_skill_organism",
    "advise_topology",
    "assemble_pattern",
    "CLIResult",
    "cli_handler",
    "cli_organism",
    "consolidate",
    "managed_organism",
    "ManagedOrganism",
    "ManagedRunResult",
    "resolve_cognitive_mode",
    "skill_organism",

    # State - Metabolism
    "ATP_Store",
    "MetabolicState",
    "EnergyType",
    "EnergyTransaction",

    # State - Histone
    "HistoneStore",
    "MarkerType",
    "MarkerStrength",
    "EpigeneticMarker",
    "RetrievalResult",

    # State - Genome
    "Genome",
    "Gene",
    "GeneType",
    "ExpressionLevel",
    "Mutation",

    # State - Telomere
    "Telomere",
    "TelomereStatus",
    "LifecyclePhase",
    "SenescenceReason",

    # State - Development
    "DevelopmentController",
    "DevelopmentConfig",
    "DevelopmentalStage",
    "DevelopmentStatus",
    "CriticalPeriod",
    "StageTransition",

    # State - DNA Repair
    "DNARepair",
    "StateCheckpoint",
    "CorruptionType",
    "DamageSeverity",
    "DamageReport",
    "RepairStrategy",

    # Topology - Loops
    "CoherentFeedForwardLoop",
    "NegativeFeedbackLoop",
    "GateLogic",
    "CircuitState",
    "LoopResult",

    # Topology - Quorum
    "QuorumSensing",
    "EmergencyQuorum",
    "VotingStrategy",
    "VoteType",
    "Vote",
    "QuorumResult",

    # Topology - Cascade
    "Cascade",
    "AgentCascade",
    "MAPKCascade",
    "CascadeStage",
    "CascadeResult",
    "StageResult",
    "StageStatus",
    "CascadeMode",

    # Topology - Oscillator
    "Oscillator",
    "CircadianOscillator",
    "HeartbeatOscillator",
    "CellCycleOscillator",
    "OscillatorPhase",
    "OscillatorState",
    "OscillatorStatus",
    "CycleResult",
    "WaveformType",

    # Membrane
    "Membrane",
    "ThreatLevel",
    "ThreatSignature",
    "FilterResult",

    # Mitochondria
    "Mitochondria",
    "MetabolicPathway",
    "ATP",
    "MetabolicResult",
    "Tool",
    "SimpleTool",

    # Chaperone
    "Chaperone",
    "FoldingStrategy",
    "FoldingAttempt",
    "EnhancedFoldedProtein",

    # Ribosome
    "Ribosome",
    "mRNA",
    "tRNA",
    "Protein",
    "Codon",
    "CodonType",

    # Lysosome
    "Lysosome",
    "Waste",
    "WasteType",
    "DigestResult",

    # Nucleus
    "Nucleus",
    "Transcription",

    # Plasmid (Horizontal Gene Transfer)
    "Plasmid",
    "PlasmidRegistry",
    "PlasmidError",
    "AcquisitionResult",

    # Memory
    "MemoryTier",
    "MemoryEntry",
    "EpisodicMemory",
    "BiTemporalFact",
    "BiTemporalQuery",
    "BiTemporalMemory",
    "FactSnapshot",
    "CorrectionResult",

    # Providers
    "LLMProvider",
    "LLMResponse",
    "ProviderConfig",
    "ToolSchema",
    "ToolCall",
    "ToolResult",
    "MockProvider",
    "OpenAIProvider",
    "OpenAICompatibleProvider",
    "AnthropicProvider",
    "GeminiProvider",
    "NucleusError",
    "ProviderUnavailableError",
    "QuotaExhaustedError",
    "TranscriptionFailedError",

    # Healing (Self-Repair Mechanisms)
    "ChaperoneLoop",
    "HealingResult",
    "HealingOutcome",
    "RefoldingAttempt",
    "RegenerativeSwarm",
    "SwarmResult",
    "SimpleWorker",
    "WorkerMemory",
    "WorkerStatus",
    "AutophagyDaemon",
    "PruneResult",
    "ContextMetrics",
    "ContextHealthStatus",

    # Health (Epistemic Monitoring)
    "EpiplexityMonitor",
    "EpiplexityState",
    "EpiplexityResult",
    "HealthStatus",
    "EmbeddingProvider",
    "MockEmbeddingProvider",

    # Surveillance - Innate Immunity
    "InnateImmunity",
    "InnateCheckResult",
    "TLRPattern",
    "PAMPCategory",
    "InflammationLevel",
    "InflammationState",
    "InflammationResponse",
    "JSONValidator",
    "LengthValidator",
    "CharacterSetValidator",

    # Coordination - Morphogen Gradients
    "MorphogenType",
    "MorphogenValue",
    "MorphogenGradient",
    "GradientUpdate",
    "GradientOrchestrator",
    "PhenotypeConfig",
    # Diffusion (Paper §6.5.2 / §6.5.3)
    "MorphogenSource",
    "DiffusionParams",
    "DiffusionField",
    "SocialLearning",
    "PeerExchange",
    "TrustRegistry",
    "AdoptionResult",
    "AdoptionOutcome",
    "ScaffoldingResult",
    "AutoinducerSignal",
    "SignalEnvironment",
    "QuorumSensingBio",

    # Multi-cellular Organization
    "ExpressionProfile",
    "CellType",
    "DifferentiatedCell",
    "TissueBoundary",
    "TissueError",
    "Tissue",
    "MetabolicAccessPolicy",
]

# Single source of truth is pyproject.toml. importlib.metadata resolves the
# version by *distribution name*, independent of which path `import operon_ai`
# loaded — so a source checkout could otherwise report a different operon-ai
# installed elsewhere in the environment. Prefer the pyproject.toml adjacent to
# this package (source tree); fall back to installed dist metadata, then a
# sentinel. This keeps pyproject.toml authoritative and non-drifting.
def _resolve_version() -> str:
    from pathlib import Path

    pyproject = Path(__file__).resolve().parent.parent / "pyproject.toml"
    if pyproject.is_file():
        try:
            import tomllib  # stdlib >= 3.11 (project's minimum)

            with pyproject.open("rb") as fh:
                return tomllib.load(fh)["project"]["version"]
        except Exception:
            pass  # malformed/unexpected — fall through to dist metadata
    try:
        from importlib.metadata import version as _v

        return _v("operon-ai")
    except Exception:
        return "0.0.0+unknown"  # not installed and no source pyproject


__version__ = _resolve_version()
del _resolve_version
