PerformanceStats

类 PerformanceStats 在 deploy/windows/swarm/core/types.py: 模型性能统计 | 方法: success_rate, avg_latency_ms, avg_confidence, record, to_dict

class PerformanceStats:
    """模型性能统计"""
    model_id: str
    total_requests: int = 0
    successful_requests: int = 0
    total_latency_ms: float = 0.0
    total_confidence: float = 0.0

    @property
    def success_rate(self) -> float:
        return self.successful_requests / max(self.total_requests, 1)

    @property
    def avg_latency_ms(self) -> float:
        return self.total_latency_ms / max(self.total_requests, 1)

    @property
    def avg_confidence(self) -> float:
        return self.total_confidence / max(self.total_requests, 1)

    def record(self, latency_ms: float, confidence: float, success: bool):
        self.total_requests += 1
        if success:
            self.successful_requests += 1
        self.total_latency_ms += latency_ms
        self.total_confidence += confidence

    def to_dict(self) -> Dict:
        return {
            "model_id": self.model_id,
            "total_requests": self.total_requests,
            "success_rate": round(self.success_rate, 3),
            "avg_latency_ms": round(self.avg_latency_ms, 1),
            "avg_confidence": round(self.avg_confidence, 3),
        }

# ============================================================
# v6兼容: 聚合与记忆类型
# ============================================================

@dataclass


record

方法 PerformanceStats.record():
        self.total_requests += 1
        if success:
            self.successful_requests += 1

def record(self):
        self.total_requests += 1
        if success:
            self.successful_requests += 1
        self.total_latency_ms += latency_ms
        self.total_confidence += confidence


ModelResult

类 ModelResult 在 deploy/windows/swarm/core/types.py: 单个模型处理结果 | 方法: to_dict

class ModelResult:
    """单个模型处理结果"""
    model_id: str
    response: str = ""
    success: bool = False
    confidence: float = 0.0
    latency_ms: float = 0.0
    error: str = ""
    metadata: Dict = field(default_factory=dict)

    def to_dict(self) -> Dict:
        return {
            "model_id": self.model_id,
            "success": self.success,
            "confidence": self.confidence,
            "latency_ms": round(self.latency_ms, 1),
        }

@dataclass


AggregationResult

类 AggregationResult 在 deploy/windows/swarm/core/types.py: 多模型聚合结果 | 方法: to_dict

class AggregationResult:
    """多模型聚合结果"""
    final_response: str = ""
    method: AggregationMethod = AggregationMethod.CONFIDENCE
    model_results: List[ModelResult] = field(default_factory=list)
    total_latency_ms: float = 0.0
    models_used: int = 0
    confidence: float = 0.0

    def to_dict(self) -> Dict:
        return {
            "final_response": self.final_response[:100],
            "method": self.method.value,
            "models_used": self.models_used,
            "confidence": self.confidence,
            "total_latency_ms": round(self.total_latency_ms, 1),
        }

@dataclass


MemoryRecord

类 MemoryRecord 在 deploy/windows/swarm/core/types.py: 记忆记录 | 方法: to_dict

class MemoryRecord:
    """记忆记录"""
    record_id: str = ""
    content: str = ""
    category: MemoryCategory = MemoryCategory.CONVERSATION
    importance: float = 0.5
    timestamp: datetime = field(default_factory=datetime.now)
    tags: List[str] = field(default_factory=list)

    def to_dict(self) -> Dict:
        return {
            "record_id": self.record_id,
            "content": self.content[:50],
            "category": self.category.value,
            "importance": self.importance,
        }

@dataclass


TaskContext

类 TaskContext 在 deploy/windows/swarm/core/types.py: 任务上下文 | 方法:

class TaskContext:
    """任务上下文"""
    task_id: str = ""
    query: str = ""
    user_id: str = "default"
    complexity: float = 0.0
    history: List[Dict] = field(default_factory=list)
    metadata: Dict = field(default_factory=dict)


ComplexityLevel

类 ComplexityLevel 在 deploy/windows/swarm/core/types.py: 复杂度等级 | 方法:

class ComplexityLevel:
    """复杂度等级"""
    SIMPLE = "simple"         # < 0.2
    NORMAL = "normal"         # 0.2-0.5
    COMPLEX = "complex"       # 0.5-0.8
    CRITICAL = "critical"     # > 0.8


record

函数 record(self, latency_ms: float, confidence: float, success: bool) 在 deploy/windows/swarm/core/types.py:

def record(self, latency_ms: float, confidence: float, success: bool):
        self.total_requests += 1
        if success:
            self.successful_requests += 1
        self.total_latency_ms += latency_ms
        self.total_confidence += confidence

    def to_dict(self) -> Dict:
        return {
            "model_id": self.model_id,
            "total_requests": self.total_requests,
            "success_rate": round(self.success_rate, 3),
            "avg_latency_ms": round(self.avg_latency_ms, 1),
            "avg_confidence": round(self.avg_confidence, 3),
        }

# ============================================================
# v6兼容: 聚合与记忆类型
# ============================================================

@dataclass


文件 deploy/windows/swarm/core/config_center.py: 虫群智能体系统 — 统一配置中心
集中管理所有配置：模型、路由、技能、API密钥等
支持环境变量覆盖 + 热加载 + 校验 | 定义: class ConfigCenter, def __new__, def load, def save, def hot_reload, def get, def set, def get_section, def get_all, def apply_env_overrides

json logging os threading time copy typing

#!/usr/bin/env python3
"""
虫群智能体系统 — 统一配置中心
集中管理所有配置：模型、路由、技能、API密钥等
支持环境变量覆盖 + 热加载 + 校验
"""

import json
import logging
import os
import threading
import time
from copy import deepcopy
from typing import Any, Dict, List, Optional

logger = logging.getLogger(__name__)

# 默认配置文件路径
DEFAULT_CONFIG_PATH = "/home/admin/swarm/config.json"

# ============================================================
# 默认配置模板
# ============================================================

DEFAULT_CONFIG = {
    "ver


ConfigCenter

统一配置中心 — 单例模式

类 ConfigCenter 在 deploy/windows/swarm/core/config_center.py: 统一配置中心 — 单例模式 | 方法: __new__, load, save, hot_reload, get, set, get_section, get_all, apply_env_overrides, validate, get_api_keys, get_status, _deep_merge, _try_convert

class ConfigCenter:
    """统一配置中心 — 单例模式"""

    _instance = None
    _lock = threading.Lock()

    def __new__(cls):
        with cls._lock:
            if cls._instance is None:
                cls._instance = super().__new__(cls)
                cls._instance._config = deepcopy(DEFAULT_CONFIG)
                cls._instance._loaded = False
                cls._instance._last_mtime = 0
            return cls._instance

    # ----------------------------------------------------------
    # 加载 / 保存
    # ----------------------------------------------------------

    def load(self, path: str = None) -> bool:
        """从JSON文件加载配置，合并到默认配置上"""
        path = path or DEFAULT_CONFIG_PATH
        if not os.path.exists(path):
            logger.info(f"配置文件不存在，使用默认配置: {path}")
            self._loaded = True
            return True

        try:
            with open(path, "r", encoding="utf-8") as f:
                user_config = json.load(f)

            # 深度合并：用户配置覆盖默认配置
            self._config = self._deep_merge(deepcopy(DEFAULT_CONFIG), user_config)
            self._last_mtime = os.path.getmtime(path)
            self._loaded = True
            logger.info(f"配置加载成功: {path}")
            return True
        except Exception as e:
            logger.warning(f"配置加载失败: {e}，使用默认配置")
            self._loaded = True
            return False

    def save(self, path: str = None) -> bool:
        """保存当前配置到JSON文件"""
        path = path or DEFAULT_CONFIG_PATH
        try:
            os.makedirs(os.path.dirname(path), exist_ok=True)
            with open(path, "w", encoding="utf-8") as f:
                json.dump(self._config, f, ensure_ascii=False, indent=2)
            logger.info(f"配置保存成功: {path}")
            return True
        except Exception as e:
            logger.error(f"配置保存失败: {e}")
            return False

    def hot_reload(self, path: str = None) -> bool:
        """热加载：文件修改时间变化时重新加载"""
        path = path or DEFAULT_CONFIG_PATH
        if not os.path.exists(path):


文件 deploy/windows/swarm/core/swarm_types.py: 虫群v7 — 核心类型系统扩展

在v6类型基础上新增v7概念的类型定义：

v7新增概念：
  种子模型 SeedModel  — 同一架构不同喂养数据长成不同角色
  元模型 MetaModel    — 个人端核心调度，分析→拆分→路由
  记忆模型 MemoryModel — 参数化记忆，不存原文存参数
  任务模型树 TaskTree  — 知识树结构，像备课一样按需加载
  虫后 Qu | 定义: class SeedStage, class TaskDomain, class MemoryType, class MetaTaskState, class
class, def to_dict, class
class, def to_dict, class
class, def to_dict

dataclasses datetime enum typing core.types

#!/usr/bin/env python3
"""
虫群v7 — 核心类型系统扩展

在v6类型基础上新增v7概念的类型定义：

v7新增概念：
  种子模型 SeedModel  — 同一架构不同喂养数据长成不同角色
  元模型 MetaModel    — 个人端核心调度，分析→拆分→路由
  记忆模型 MemoryModel — 参数化记忆，不存原文存参数
  任务模型树 TaskTree  — 知识树结构，像备课一样按需加载
  虫后 QueenAgent     — 个人端完整系统(元模型+记忆+任务树)
  虫巢 HiveCluster    — 中央大模型集群
  虫皇 OvermindAgent  — 生态治理者

依赖: core.types 中的 SwarmRole, TaskPriority, SwarmStatus, HiveModelInfo 等基础类型
"""

from dataclasses import dataclass, field
from datetime import datetime
from enum import Enum
from


SeedStage

种子模型成长阶段 — 从种子到可入虫巢

类 SeedStage 在 deploy/windows/swarm/core/swarm_types.py: 种子模型成长阶段 — 从种子到可入虫巢 | 方法:

class SeedStage:
    """种子模型成长阶段 — 从种子到可入虫巢"""
    SEED = "seed"              # 种子 — 初始状态
    SPROUT = "sprout"          # 萌芽 — 开始喂养
    GROWTH = "growth"          # 成长 — 持续训练
    MATURE = "mature"          # 成熟 — 可用
    HIVE_READY = "hive_ready"  # 可入虫巢 — 可部署到虫巢


TaskDomain

任务领域分类 — 覆盖个人端主要场景

类 TaskDomain 在 deploy/windows/swarm/core/swarm_types.py: 任务领域分类 — 覆盖个人端主要场景 | 方法:

class TaskDomain:
    """任务领域分类 — 覆盖个人端主要场景"""
    WORK = "work"              # 工作
    STUDY = "study"            # 学习
    LIFE = "life"              # 生活
    CREATIVE = "creative"      # 创作
    SOCIAL = "social"          # 社交
    CUSTOM = "custom"          # 自定义


MemoryType

记忆类型 — 参数化记忆体系

类 MemoryType 在 deploy/windows/swarm/core/swarm_types.py: 记忆类型 — 参数化记忆体系 | 方法:

class MemoryType:
    """记忆类型 — 参数化记忆体系"""
    PARAMETRIC = "parametric"  # 参数化 — 编码为模型参数
    EPISODIC = "episodic"      # 片段 — 具体事件记忆
    SEMANTIC = "semantic"      # 语义 — 知识概念记忆


MetaTaskState

元模型任务状态 — 分析→拆分→路由→执行→聚合

类 MetaTaskState 在 deploy/windows/swarm/core/swarm_types.py: 元模型任务状态 — 分析→拆分→路由→执行→聚合 | 方法:

class MetaTaskState:
    """元模型任务状态 — 分析→拆分→路由→执行→聚合"""
    ANALYZING = "analyzing"      # 分析中
    DECOMPOSING = "decomposing"  # 拆分中
    ROUTING = "routing"          # 路由中
    EXECUTING = "executing"      # 执行中
    AGGREGATING = "aggregating"  # 聚合中
    COMPLETED = "completed"      # 完成

# ============================================================
# v7 核心数据结构
# ============================================================

@dataclass


SeedInfo

类 SeedInfo 在 deploy/windows/swarm/core/swarm_types.py: 种子模型信息 — 同一架构不同喂养数据长成不同角色

    种子是v7的核心概念：一个基础架构的模型种子，通过喂养不同数据，
    逐步成长为不同角色的子虫。就像同一颗种子，浇水施肥不同，长成不同的树。 | 方法: to_dict

class SeedInfo:
    """种子模型信息 — 同一架构不同喂养数据长成不同角色

    种子是v7的核心概念：一个基础架构的模型种子，通过喂养不同数据，
    逐步成长为不同角色的子虫。就像同一颗种子，浇水施肥不同，长成不同的树。
    """
    seed_id: str                           # 种子唯一标识
    name: str                              # 种子名称
    stage: SeedStage                       # 当前成长阶段
    base_arch: str                         # 基础架构(如"llama3", "qwen2")
    params_M: float                        # 参数量(M)
    feeding_data: str                      # 喂养数据来源
    fed_tokens: int                        # 已喂养token数
    evolved_from: str                      # 从哪个种子进化而来
    role: SwarmRole                        # 最终角色
    accuracy: float                        # 当前精度
    status: SwarmStatus                    # 当前状态

    def to_dict(self) -> Dict:
        return {
            "seed_id": self.seed_id,
            "name": self.name,
            "stage": self.stage.value,
            "base_arch": self.base_arch,
            "params_M": self.params_M,
            "feeding_data": self.feeding_data,
            "fed_tokens": self.fed_tokens,
            "evolved_from": self.evolved_from,
            "role": self.role.value,
            "accuracy": self.accuracy,
            "status": self.status.value,
        }

@dataclass


MetaTask

类 MetaTask 在 deploy/windows/swarm/core/swarm_types.py: 元模型任务分解单元 — 分析→拆分→路由→执行→聚合

    元模型收到用户查询后，将任务分解为多个子任务，
    路由到不同子虫执行，最后聚合结果。 | 方法: to_dict

class MetaTask:
    """元模型任务分解单元 — 分析→拆分→路由→执行→聚合

    元模型收到用户查询后，将任务分解为多个子任务，
    路由到不同子虫执行，最后聚合结果。
    """
    task_id: str                           # 任务唯一标识
    parent_task_id: Optional[str]          # 父任务ID(顶层任务为None)
    query: str                             # 原始查询
    subtasks: List[str]                    # 子任务ID列表
    domain: TaskDomain                     # 任务领域
    complexity: float                      # 复杂度 0~1
    priority: TaskPriority                 # 优先级
    assigned_drone: str                    # 分配的子虫ID
    state: MetaTaskState                   # 当前状态
    result: str                            # 结果
    confidence: float                      # 置信度

    def to_dict(self) -> Dict:
        return {
            "task_id": self.task_id,
            "parent_task_id": self.parent_task_id,
            "query": self.query,
            "subtasks": self.subtasks,
            "domain": self.domain.value,
            "complexity": self.complexity,
            "priority": self.priority.value,
            "assigned_drone": self.assigned_drone,
            "state": self.state.value,
            "result": self.result,
            "confidence": self.confidence,
        }

@dataclass


MemoryEntry

类 MemoryEntry 在 deploy/windows/swarm/core/swarm_types.py: 记忆条目 — 参数化记忆体系

    不存原文存参数：频繁访问的记忆编码为模型参数，
    不频繁的保留为片段/语义记忆，按需检索。 | 方法: to_dict

class MemoryEntry:
    """记忆条目 — 参数化记忆体系

    不存原文存参数：频繁访问的记忆编码为模型参数，
    不频繁的保留为片段/语义记忆，按需检索。
    """
    entry_id: str                          # 条目唯一标识
    user_id: str                           # 用户ID
    memory_type: MemoryType                # 记忆类型
    content: str                           # 内容摘要
    embedding: Optional[List[float]]       # 向量嵌入
    frequency: int = 1                     # 访问频率
    last_accessed: datetime = field(default_factory=datetime.now)  # 最后访问时间
    encoded_in_model: bool = False         # 是否已编码为参数

    def to_dict(self) -> Dict:
        return {
            "entry_id": self.entry_id,
            "user_id": self.user_id,
            "memory_type": self.memory_type.value,
            "content": self.content,
            "embedding": self.embedding,
            "frequency": self.frequency,
            "last_accessed": self.last_accessed.isoformat(),
            "encoded_in_model": self.encoded_in_model,
        }

@dataclass


TaskTreeNode

类 TaskTreeNode 在 deploy/windows/swarm/core/swarm_types.py: 任务模型树节点 — 像高中物理知识树

    知识按树结构组织，像备课一样按需加载：
    学习/物理/力学 → 只激活力学相关的子虫
    学习/物理/电磁 → 切换到电磁相关子虫

    同一架构的种子，在不同知识节点上喂养不同数据，
    形成不同领域的专家子虫。 | 方法: to_dict

class TaskTreeNode:
    """任务模型树节点 — 像高中物理知识树

    知识按树结构组织，像备课一样按需加载：
    学习/物理/力学 → 只激活力学相关的子虫
    学习/物理/电磁 → 切换到电磁相关子虫

    同一架构的种子，在不同知识节点上喂养不同数据，
    形成不同领域的专家子虫。
    """
    node_id: str                           # 节点唯一标识
    name: str                              # 节点名称
    domain: TaskDomain                     # 所属领域
    parent_id: Optional[str]               # 父节点ID
    children: List[str]                    # 子节点ID列表
    drone_id: str                          # 对应的子虫ID
    knowledge_path: str                    # 知识路径(如"学习/物理/力学")
    params_M: float = 0.0                  # 参数量(M)
    is_active: bool = False                # 是否激活

    def to_dict(self) -> Dict:
        return {
            "node_id": self.node_id,
            "name": self.name,
            "domain": self.domain.value,
            "parent_id": self.parent_id,
            "children": self.children,
            "drone_id": self.drone_id,
            "is_active": self.is_active,
            "knowledge_path": self.knowledge_path,
            "params_M": self.params_M,
        }

@dataclass


QueenConfig

类 QueenConfig 在 deploy/windows/swarm/core/swarm_types.py: 虫后个人配置 — 个人端完整系统

    虫后 = 元模型 + 记忆模型 + 任务树
    元模型负责分析和调度，记忆模型存储用户偏好，
    任务树按需加载知识领域的子虫。 | 方法: to_dict

class QueenConfig:
    """虫后个人配置 — 个人端完整系统

    虫后 = 元模型 + 记忆模型 + 任务树
    元模型负责分析和调度，记忆模型存储用户偏好，
    任务树按需加载知识领域的子虫。
    """
    queen_id: str                          # 虫后唯一标识(即用户ID)
    user_name: str                         # 用户名
    meta_model_id: str                     # 元模型ID
    memory_model_id: str                   # 记忆模型ID
    task_tree_root: str                    # 任务树根节点ID
    active_drones: List[str]               # 当前活跃子虫列表
    max_concurrent: int = 3                # 最大并发子虫数
    auto_evolve: bool = True               # 是否自动进化种子

    def to_dict(self) -> Dict:
        return {
            "queen_id": self.queen_id,
            "user_name": self.user_name,
            "meta_model_id": self.meta_model_id,
            "memory_model_id": self.memory_model_id,
            "task_tree_root": self.task_tree_root,
            "active_drones": self.active_drones,
            "max_concurrent": self.max_concurrent,
            "auto_evolve": self.auto_evolve,
        }

@dataclass


HiveClusterConfig

类 HiveClusterConfig 在 deploy/windows/swarm/core/swarm_types.py: 虫巢集群配置 — 中央大模型集群

    虫巢是中央大模型的集群，为所有虫后提供推理能力。
    支持多种负载均衡策略，按能力匹配最适合的模型。 | 方法: to_dict

class HiveClusterConfig:
    """虫巢集群配置 — 中央大模型集群

    虫巢是中央大模型的集群，为所有虫后提供推理能力。
    支持多种负载均衡策略，按能力匹配最适合的模型。
    """
    cluster_id: str                        # 集群唯一标识
    name: str                              # 集群名称
    models: List[HiveModelInfo]            # 模型列表
    total_capacity: int                    # 总容量
    current_load: int                      # 当前负载
    load_balance_strategy: str = "round_robin"  # 负载均衡策略: round_robin/least_loaded/capability_match

    def to_dict(self) -> Dict:
        return {
            "cluster_id": self.cluster_id,
            "name": self.name,
            "models": [m.to_dict() for m in self.models],
            "total_capacity": self.total_capacity,
            "current_load": self.current_load,
            "load_balance_strategy": self.load_balance_strategy,
        }


文件 deploy/windows/swarm/core/memory_model.py: 虫群v7 — 记忆模型(Memory Model)
参数化个人记忆系统：将交互记录编码为可检索的结构

设计思路：
- 传统方案：对话存数据库，检索靠关键词匹配
- 虫群方案：记忆以向量索引+时序结构存储，检索更精准
- 未来方向：将记忆编码为模型参数（模型即数据库）
- 当前实现：轻量级向量索引 + 时序衰减 + 关键词增强 | 定义: class MemoryEntry, def __init__, def _extract_keywords, def to_dict, class MemoryModel, def __init__, def retrieve_context, def retrieve, def store_interaction, def _evict

hashlib json logging math os re time datetime typing

#!/usr/bin/env python3
"""
虫群v7 — 记忆模型(Memory Model)
参数化个人记忆系统：将交互记录编码为可检索的结构

设计思路：
- 传统方案：对话存数据库，检索靠关键词匹配
- 虫群方案：记忆以向量索引+时序结构存储，检索更精准
- 未来方向：将记忆编码为模型参数（模型即数据库）
- 当前实现：轻量级向量索引 + 时序衰减 + 关键词增强
"""

import hashlib
import json
import logging
import math
import os
import re
import time
from datetime import datetime
from typing import Dict, List, Optional, Tuple

logger = logging.getLogger(__name__)

# ============================================================
# 记忆条目
# ============================


MemoryEntry

类 MemoryEntry 在 deploy/windows/swarm/core/memory_model.py: 单条记忆 | 方法: __init__, _extract_keywords, to_dict

class MemoryEntry:
    """单条记忆"""

    def __init__(self, user_message: str, ai_response: str,
                 intent: str = "", route: str = "",
                 timestamp: str = ""):
        self.entry_id = hashlib.md5(
            f"{user_message}{ai_response}{time.time()}".encode()
        ).hexdigest()[:12]
        self.user_message = user_message
        self.ai_response = ai_response
        self.intent = intent
        self.route = route
        self.timestamp = timestamp or datetime.now().isoformat()
        self.created_at = time.time()

        # 关键词提取（简易版，替代分词）
        self.keywords = self._extract_keywords(user_message)

        # 访问计数（用于衰减/强化）
        self.access_count = 0
        self.last_access = self.created_at

    def _extract_keywords(self, text: str) -> List[str]:
        """简易中文关键词提取：去停用词 + 长词优先"""
        # 去标点
        cleaned = re.sub(r'[，。！？、；：""''（）\s]', ' ', text)
        # 按空格和常见分隔符切分
        words = re.split(r'[\s,.\-!?;:]+', cleaned)
        # 过滤短词
        keywords = [w for w in words if len(w) >= 2]
        return keywords[:20]  # 最多20个

    def to_dict(self) -> Dict:
        return {
            "entry_id": self.entry_id,
            "user_message": self.user_message[:100],
            "ai_response": self.ai_response[:100],
            "intent": self.intent,
            "keywords": self.keywords[:5],
            "timestamp": self.timestamp,
            "access_count": self.access_count,
        }

# ============================================================
# 记忆模型核心
# ============================================================


MemoryModel

类 MemoryModel 在 deploy/windows/swarm/core/memory_model.py: 记忆模型 — 参数化个人记忆

    特性:
    1. retrieve_context(): 获取上下文摘要（给元模型用）
    2. retrieve(): 精确检索记忆条目
    3. store_interaction(): 存储新交互
    4. 时间衰减: 越早的记忆权重越低
    5. 关键词增强: 匹配关键词越多权重越高 | 方法: __init__, retrieve_context, retrieve, store_interaction, _evict, evict_score, _rebuild_index, _load, _save, get_stats, encode_realtime, _update_memory_params, _extract_concepts, _micro_finetune, fork_for_task, merge_from, get_unlimited_context, get_param_stats

class MemoryModel:
    """
    记忆模型 — 参数化个人记忆

    特性:
    1. retrieve_context(): 获取上下文摘要（给元模型用）
    2. retrieve(): 精确检索记忆条目
    3. store_interaction(): 存储新交互
    4. 时间衰减: 越早的记忆权重越低
    5. 关键词增强: 匹配关键词越多权重越高
    """

    # 记忆存储路径
    DATA_DIR = "/home/admin/swarm/data/memory"
    MAX_ENTRIES = 5000          # 最大记忆条数
    DECAY_HALF_LIFE = 86400 * 7  # 衰减半衰期: 7天

    def __init__(self, user_id: str = "default"):
        self.user_id = user_id
        self._entries: List[MemoryEntry] = []
        self._keyword_index: Dict[str, List[str]] = {}  # keyword -> [entry_ids]
        self._intent_index: Dict[str, List[str]] = {}    # intent -> [entry_ids]
        self._loaded = False

        # 统计
        self._store_count = 0
        self._retrieve_count = 0
        self._hit_count = 0

        # 延迟加载
        self._load()

    # ============================================================
    # 核心接口
    # ============================================================

    def retrieve_context(self, message: str) -> str:
        """
        获取与当前消息相关的记忆上下文文本
        返回格式: "之前你问过XXX，我回答了YYY"
        """
        self._retrieve_count += 1

        entries = self.retrieve(message, top_k=3)
        if not entries:
            return ""

        self._hit_count += 1
        parts = []
        for e in entries:
            msg = e.get("user_message", "") if isinstance(e, dict) else e.user_message
            resp = e.get("ai_response", "") if isinstance(e, dict) else e.ai_response
            parts.append(f"之前你问过「{msg[:50]}」，回答是「{resp[:50]}」")

        return "；".join(parts)

    def retrieve(self, query: str, top_k: int = 5) -> List[Dict]:
        """
        检索相关记忆条目

        算法: 关键词匹配 + 时间衰减 + 访问频次
        """
        self._retrieve_count += 1

        if not self._entries:
            return []

        # 提取查询关键词
        query_words = set()
        cleaned = re.sub(r'[，。！？、；：""''（）\s]', ' ', query)
        for w


方法 MemoryModel.encode_realtime():
        """
        实时训练: 每次交互都更新记忆参数

        实现策略:
        - 维护一个微型训练缓冲区(最近N条交互)

def encode_realtime(self):
        """
        实时训练: 每次交互都更新记忆参数

        实现策略:
        - 维护一个微型训练缓冲区(最近N条交互)
        - 当缓冲区满时触发微调(模拟)
        - 关键: 高频记忆权重增大，低频记忆权重衰减
        """
        # 先正常存储
        entry_id = self.store_interaction(message, result, analysis)

        # 更新记忆强度参数
        self._update_memory_params(message, result)

        # 检查是否需要触发微调
        if self._store_count % 20 == 0:
            self._micro_finetune()

        return entry_id


方法 MemoryModel.merge_from():
        """
        合并另一个记忆模型(从分叉回归)

        用法: 课上积累的物理记忆，课后合并回主记忆
        """
        me

def merge_from(self):
        """
        合并另一个记忆模型(从分叉回归)

        用法: 课上积累的物理记忆，课后合并回主记忆
        """
        merged = 0
        for entry in other._entries:
            # 检查是否已存在
            existing_ids = {e.entry_id for e in self._entries}
            if entry.entry_id not in existing_ids:
                self._entries.append(entry)
                for kw in entry.keywords:
                    if kw not in self._keyword_index:
                        self._keyword_index[kw] = []
                    self._keyword_index[kw].append(entry.entry_id)
                merged += 1

        # 合并参数
        if hasattr(other, '_param_dict') and hasattr(self, '_param_dict'):
            for k, v in other._param_dict.items():
                if k in self._param_dict:
                    self._param_dict[k] = max(self._param_dict[k], v)
                else:
                    self._param_dict[k] = v

        self._save()
        logger.info(f"记忆合并: +{merged}条新记忆")


MemoryMatrix

类 MemoryMatrix 在 deploy/windows/swarm/core/memory_model.py: 记忆矩阵 — 管理用户的所有任务记忆

    结构:
      主记忆(General) — 日常交互
      ├── 物理记忆(Physics) — 备课专用
      ├── 编程记忆(Coding) — 开发专用
      └── ...按需创建

    用法:
      matrix = MemoryMatrix("user_001")
      matr | 方法: __init__, activate, deactivate, current, store, retrieve, get_context, list_tasks, get_status

class MemoryMatrix:
    """
    记忆矩阵 — 管理用户的所有任务记忆

    结构:
      主记忆(General) — 日常交互
      ├── 物理记忆(Physics) — 备课专用
      ├── 编程记忆(Coding) — 开发专用
      └── ...按需创建

    用法:
      matrix = MemoryMatrix("user_001")
      matrix.activate("physics")  # 激活物理记忆
      matrix.store("...", "...")  # 存入当前激活记忆
      matrix.deactivate()         # 回到主记忆
    """

    def __init__(self, user_id: str = "default"):
        self.user_id = user_id
        self._main = MemoryModel(user_id)
        self._forks: Dict[str, MemoryModel] = {}
        self._active: Optional[str] = None  # 当前激活的任务名

    def activate(self, task_name: str):
        """激活一个任务记忆(不存在则创建)"""
        if task_name not in self._forks:
            # 从主记忆分叉
            self._forks[task_name] = self._main.fork_for_task(task_name)
            logger.info(f"创建任务记忆: {task_name}")
        self._active = task_name

    def deactivate(self):
        """停用任务记忆，合并回主记忆"""
        if self._active and self._active in self._forks:
            self._main.merge_from(self._forks[self._active])
            # 保留分叉不删除(下次可复用)
        self._active = None

    @property
    def current(self) -> MemoryModel:
        """获取当前活跃的记忆模型"""
        if self._active and self._active in self._forks:
            return self._forks[self._active]
        return self._main

    def store(self, message: str, result: str, analysis=None):
        """存入当前活跃记忆"""
        self.current.store_interaction(message, result, analysis)

    def retrieve(self, query: str, top_k: int = 5) -> List[Dict]:
        """从当前活跃记忆检索"""
        return self.current.retrieve(query, top_k)

    def get_context(self, query: str) -> str:
        """获取上下文(优先当前任务+补充主记忆)"""
        task_ctx = self.current.retrieve_context(query)
        if self._active:
            main_ctx = self._main.retrieve_context(query)
            if task_ctx and main_ctx:
                return f"[任务:{self._active}] {task_ctx}\n[通用] {main_ctx}"
        return task_ctx

    def list_tasks(self) -> List[str]:
        """列出所有


activate

方法 MemoryMatrix.activate():
        """激活一个任务记忆(不存在则创建)"""
        if task_name not in self._forks:
            # 从主记忆分叉

def activate(self):
        """激活一个任务记忆(不存在则创建)"""
        if task_name not in self._forks:
            # 从主记忆分叉
            self._forks[task_name] = self._main.fork_for_task(task_name)
            logger.info(f"创建任务记忆: {task_name}")
        self._active = task_name


deactivate

方法 MemoryMatrix.deactivate():
        """停用任务记忆，合并回主记忆"""
        if self._active and self._active in self._forks:
            se

def deactivate(self):
        """停用任务记忆，合并回主记忆"""
        if self._active and self._active in self._forks:
            self._main.merge_from(self._forks[self._active])
            # 保留分叉不删除(下次可复用)
        self._active = None

    @property


store

方法 MemoryMatrix.store():
        """存入当前活跃记忆"""
        self.current.store_interaction(message, result, analysis)

def store(self):
        """存入当前活跃记忆"""
        self.current.store_interaction(message, result, analysis)


router.py

文件 deploy/windows/swarm/core/router.py: 虫群统一推理路由器
- 本地小模型(tiny/small/medium): 快速响应，简单任务
- 云端GLM-5.1(Modal): 复杂推理，需要深度思考的任务
- 自动路由：按任务复杂度选择模型
- 降级机制：云端不可用时回退本地 | 定义: class SwarmRouter, def __init__, def _classify_complexity, def route, def _route_forced, def _route_local, def _route_cloud, def get_status, def get_router

#!/usr/bin/env python3
"""
虫群统一推理路由器
- 本地小模型(tiny/small/medium): 快速响应，简单任务
- 云端GLM-5.1(Modal): 复杂推理，需要深度思考的任务
- 自动路由：按任务复杂度选择模型
- 降级机制：云端不可用时回退本地
"""

import os
import time
from typing import Dict, List, Optional

from core.modal_glm import ModalGLMBackend, get_glm_backend
from core.local_inference import LocalModelBackend, get_local_backend

class SwarmRouter:
    """
    虫群推理路由器

    路由策略：
    1. 简单任务（闲聊、格式化、短回答）→ 本地tiny模型
    2. 通用任务（对话、翻译、摘要）→ 本地small/medium模型
    3. 复杂任务（推理、分析、编程、数学）→


SwarmRouter

类 SwarmRouter 在 deploy/windows/swarm/core/router.py: 虫群推理路由器

    路由策略：
    1. 简单任务（闲聊、格式化、短回答）→ 本地tiny模型
    2. 通用任务（对话、翻译、摘要）→ 本地small/medium模型
    3. 复杂任务（推理、分析、编程、数学）→ 云端GLM-5.1
    4. 云端不可用 → 降级到本地最强模型 | 方法: __init__, _classify_complexity, route, _route_forced, _route_local, _route_cloud, get_status

class SwarmRouter:
    """
    虫群推理路由器

    路由策略：
    1. 简单任务（闲聊、格式化、短回答）→ 本地tiny模型
    2. 通用任务（对话、翻译、摘要）→ 本地small/medium模型
    3. 复杂任务（推理、分析、编程、数学）→ 云端GLM-5.1
    4. 云端不可用 → 降级到本地最强模型
    """

    # 简单任务关键词
    SIMPLE_KEYWORDS = [
        "你好", "hello", "hi", "嗨", "早上好", "下午好",
        "谢谢", "再见", "好的", "确认",
    ]

    # 复杂任务关键词
    COMPLEX_KEYWORDS = [
        "为什么", "分析", "推理", "证明", "计算", "编程", "代码",
        "对比", "评估", "优化", "设计", "架构", "解释",
        "为什么", "how", "why", "explain", "analyze",
        "数学", "逻辑", "算法", "策略", "方案",
    ]

    def __init__(
        self,
        local_backend=None,
        cloud_backend=None,
        # 兼容旧接口：传入registry时自动创建backends
        registry=None,
    ):
        if registry is not None:
            # MOA引擎传入registry时的兼容模式
            self._registry = registry
        self.local = local_backend or get_local_backend()
        self.cloud = cloud_backend or get_glm_backend()

        # 默认本地模型
        self.default_local_model = "tiny"

        # 统计
        self._route_stats = {
            "local_simple": 0,
            "local_general": 0,
            "cloud_complex": 0,
            "cloud_fallback": 0,
            "errors": 0,
        }

    def _classify_complexity(self, query: str) -> str:
        """分类任务复杂度: simple / general / complex"""
        query_lower = query.lower().strip()

        # 极短输入 → 简单
        if len(query_lower) <= 5:
            return "simple"

        # 匹配关键词
        for kw in self.SIMPLE_KEYWORDS:
            if kw in query_lower:
                return "simple"

        for kw in self.COMPLEX_KEYWORDS:
            if kw in query_lower:
                return "complex"

        # 中等长度默认通用
        return "general"

    def route(
        self,
        query: str,
        force_model: str = None,
        max_tokens: int = 512,
    ) -> Dict:
        """
        智能路由推理

        参数:
            query: 用户输入
            force_model: 强制指定模型("tiny"/"


conversation.py

文件 deploy/windows/swarm/core/conversation.py: 虫群智能体系统 — 对话上下文管理
管理多轮对话历史、上下文窗口滑动、会话隔离 | 定义: class Message, def __init__, def to_dict, def from_dict, def token_estimate, class Conversation, def __init__, def add, def _trim, def _total_tokens

json logging os threading time uuid collections typing

#!/usr/bin/env python3
"""
虫群智能体系统 — 对话上下文管理
管理多轮对话历史、上下文窗口滑动、会话隔离
"""

import json
import logging
import os
import threading
import time
import uuid
from collections import deque
from typing import Dict, List, Optional, Tuple

logger = logging.getLogger(__name__)

# 默认存储路径
DEFAULT_STORE_PATH = "/home/admin/swarm/data/conversations"

class Message:
    """单条消息"""

    def __init__(self, role: str, content: str,
                 model: str = "", metadata: dict = None):
        self.role = role


Message

类 Message 在 deploy/windows/swarm/core/conversation.py: 单条消息 | 方法: __init__, to_dict, from_dict, token_estimate

class Message:
    """单条消息"""

    def __init__(self, role: str, content: str,
                 model: str = "", metadata: dict = None):
        self.role = role  # user / assistant / system
        self.content = content
        self.model = model
        self.metadata = metadata or {}
        self.timestamp = time.time()

    def to_dict(self) -> dict:
        return {
            "role": self.role,
            "content": self.content,
            "model": self.model,
            "metadata": self.metadata,
            "timestamp": self.timestamp,
        }

    @classmethod
    def from_dict(cls, d: dict) -> "Message":
        msg = cls(d["role"], d["content"],
                  d.get("model", ""), d.get("metadata", {}))
        msg.timestamp = d.get("timestamp", time.time())
        return msg

    def token_estimate(self) -> int:
        """估算token数（中文约1.5字/token，英文约4字符/token）"""
        cn_chars = sum(1 for c in self.content if '\u4e00' <= c <= '\u9fff')
        en_chars = len(self.content) - cn_chars
        return int(cn_chars / 1.5 + en_chars / 4) + 1


Conversation

类 Conversation 在 deploy/windows/swarm/core/conversation.py: 单个对话会话 | 方法: __init__, add, _trim, _total_tokens, get_context, get_recent, get_user_messages, to_dict, from_dict

class Conversation:
    """单个对话会话"""

    def __init__(self, conversation_id: str = "", max_messages: int = 50,
                 max_tokens: int = 4000):
        self.conversation_id = conversation_id or str(uuid.uuid4())[:8]
        self.max_messages = max_messages
        self.max_tokens = max_tokens
        self.messages: List[Message] = []
        self.created_at = time.time()
        self.updated_at = time.time()
        self.summary = ""  # 对话摘要

    def add(self, role: str, content: str, model: str = "",
            metadata: dict = None) -> Message:
        """添加消息"""
        msg = Message(role, content, model, metadata)
        self.messages.append(msg)
        self.updated_at = time.time()
        # 超出上限时滑动窗口
        self._trim()
        return msg

    def _trim(self):
        """滑动窗口裁剪：保留最近的消息，不超过token限制"""
        while len(self.messages) > self.max_messages:
            self.messages.pop(0)

        # token限制裁剪
        while self._total_tokens() > self.max_tokens and len(self.messages) > 2:
            self.messages.pop(0)

    def _total_tokens(self) -> int:
        return sum(m.token_estimate() for m in self.messages)

    def get_context(self, max_tokens: int = None) -> List[dict]:
        """
        获取上下文窗口，返回OpenAI兼容的messages格式。
        保留系统提示 + 最近对话
        """
        max_tokens = max_tokens or self.max_tokens
        result = []
        token_count = 0

        # 从最新消息往前取
        for msg in reversed(self.messages):
            est = msg.token_estimate()
            if token_count + est > max_tokens:
                break
            result.insert(0, {
                "role": msg.role,
                "content": msg.content,
            })
            token_count += est

        return result

    def get_recent(self, n: int = 5) -> List[Message]:
        """获取最近N条消息"""
        return self.messages[-n:] if len(self.messages) >= n else self.messages

    def get_user_messages(self) -> List[str]:
        """获取所有用户消息"""
        return [m.content for m in self.messag


ConversationManager

对话上下文管理器 — 管理多个会话

类 ConversationManager 在 deploy/windows/swarm/core/conversation.py: 对话上下文管理器 — 管理多个会话 | 方法: __init__, new_conversation, get_active, get_or_create, switch, list_conversations, delete_conversation, add_message, get_context, build_api_messages, _save_conversation, _load_all, get_stats

class ConversationManager:
    """对话上下文管理器 — 管理多个会话"""

    def __init__(self, store_path: str = DEFAULT_STORE_PATH,
                 max_conversations: int = 100):
        self.store_path = store_path
        self.max_conversations = max_conversations
        self._conversations: Dict[str, Conversation] = {}
        self._active_id: Optional[str] = None
        self._lock = threading.Lock()
        # 启动时加载
        self._load_all()

    # ----------------------------------------------------------
    # 会话管理
    # ----------------------------------------------------------

    def new_conversation(self, conversation_id: str = "") -> str:
        """创建新会话"""
        conv = Conversation(conversation_id)
        with self._lock:
            self._conversations[conv.conversation_id] = conv
            self._active_id = conv.conversation_id
        logger.info(f"新会话: {conv.conversation_id}")
        return conv.conversation_id

    def get_active(self) -> Optional[Conversation]:
        """获取当前活跃会话"""
        if self._active_id and self._active_id in self._conversations:
            return self._conversations[self._active_id]
        return None

    def get_or_create(self, conversation_id: str = "") -> Conversation:
        """获取或创建会话"""
        if conversation_id and conversation_id in self._conversations:
            return self._conversations[conversation_id]
        conv = Conversation(conversation_id)
        with self._lock:
            self._conversations[conv.conversation_id] = conv
            self._active_id = conv.conversation_id
        return conv

    def switch(self, conversation_id: str) -> bool:
        """切换活跃会话"""
        if conversation_id in self._conversations:
            self._active_id = conversation_id
            return True
        return False

    def list_conversations(self) -> List[dict]:
        """列出所有会话摘要"""
        result = []
        for cid, conv in self._conversations.items():
            result.append({
                "id": cid,
                "messages":


文件 deploy/windows/swarm/core/auto_trainer.py: 虫群v7.1 — 自动训练器
实现模型的自动训练、参数扩展、持续优化

核心机制:
  1. should_train(): 根据数据积累量判断是否触发训练
  2. auto_train(): 执行单次训练(增量)
  3. grow_param(): 参数扩展(d_model/n_heads/n_layers逐步增大)
  4. 训练历史追踪，防止过拟合

设计原则:
  - 积少成多: 小批 | 定义: class AutoTrainer, def __init__, def _load_state, def _save_state, def notify_new_data, def should_train, def auto_train, def _simulate_train, def grow_param, def get_status

#!/usr/bin/env python3
"""
虫群v7.1 — 自动训练器
实现模型的自动训练、参数扩展、持续优化

核心机制:
  1. should_train(): 根据数据积累量判断是否触发训练
  2. auto_train(): 执行单次训练(增量)
  3. grow_param(): 参数扩展(d_model/n_heads/n_layers逐步增大)
  4. 训练历史追踪，防止过拟合

设计原则:
  - 积少成多: 小批量数据不断喂养，模型持续进化
  - 按需启动: 只在数据量足够时触发，避免浪费算力
  - 渐进扩展: 先练好小模型，再扩展参数量
"""

import json
import logging
import os
import time
from datetime import datetime
from typing import Dict, List, Optional, Tuple

logger = logging.getLogger(__name__)

# 训练触发阈值配置
TRAIN_CONFIG = {
    "min


AutoTrainer

类 AutoTrainer 在 deploy/windows/swarm/core/auto_trainer.py: 自动训练器 — 模型持续进化引擎

    使用方式:
      trainer = AutoTrainer("my_queen", role="queen")
      if trainer.should_train():
          result = trainer.auto_train() | 方法: __init__, _load_state, _save_state, notify_new_data, should_train, auto_train, _simulate_train, grow_param, get_status

class AutoTrainer:
    """
    自动训练器 — 模型持续进化引擎

    使用方式:
      trainer = AutoTrainer("my_queen", role="queen")
      if trainer.should_train():
          result = trainer.auto_train()
    """

    def __init__(self, model_name: str, role: str = "queen",
                 base_dir: str = "/home/admin/swarm/training/seeds"):
        self.model_name = model_name
        self.role = role
        self.base_dir = os.path.join(base_dir, model_name)

        # 训练状态
        self._state_file = os.path.join(self.base_dir, "trainer_state.json")
        self._state = self._load_state()

        # 数据计数
        self._new_data_count = self._state.get("new_data_count", 0)

    def _load_state(self) -> Dict:
        """加载训练状态"""
        if os.path.exists(self._state_file):
            with open(self._state_file, "r") as f:
                return json.load(f)
        return {
            "model_name": self.model_name,
            "role": self.role,
            "current_gen": 0,
            "total_train_rounds": 0,
            "total_data_fed": 0,
            "new_data_count": 0,
            "last_train_time": None,
            "loss_history": [],
            "current_loss": None,
        }

    def _save_state(self):
        """保存训练状态"""
        os.makedirs(os.path.dirname(self._state_file), exist_ok=True)
        with open(self._state_file, "w") as f:
            json.dump(self._state, f, indent=2, ensure_ascii=False)

    # ============================================================
    # 核心方法(后续A2-A4逐步实现)
    # ============================================================

    def notify_new_data(self, count: int = 1):
        """通知有新数据到达"""
        self._new_data_count += count
        self._state["new_data_count"] = self._new_data_count
        self._save_state()
        logger.info(f"自动训练器: +{count}条新数据, 累计{self._new_data_count}")

    def should_train(self) -> bool:
        """判断是否应该触发训练"""
        if self._new_data_count < TRAIN_CONFIG["min_new_data"]:
            return Fa


方法 AutoTrainer.notify_new_data():
        """通知有新数据到达"""
        self._new_data_count += count
        self._state["new_data_count"]

def notify_new_data(self):
        """通知有新数据到达"""
        self._new_data_count += count
        self._state["new_data_count"] = self._new_data_count
        self._save_state()
        logger.info(f"自动训练器: +{count}条新数据, 累计{self._new_data_count}")


文件 tools/debug_encode.py:  | 定义:

#!/usr/bin/env python3
import sys
sys.path.insert(0, '/home/admin/swarm')
import pickle
import numpy as np

with open('models/brain_templates/brain_v13.pkl', 'rb') as f:
    data = pickle.load(f)

tokenizer = data['tokenizer']
embedding = data['embedding']

print('token2id sample:', list(tokenizer.token2id.items())[:5])

# 测试encode和get_embedding
test = 'hello'
ids = tokenizer.encode(test, embedding)
print(f'ids: {ids}')

for tid in ids:
    try:
        vec = embedding.get_embedding(tid)


文件 tools/test_dynamic_load.py: 测试动态加载不在20k但SQLite有的词 | 定义:

#!/usr/bin/env python3
"""测试动态加载不在20k但SQLite有的词"""
import sys
sys.path.insert(0, '/home/admin/swarm')

from core.neuro.chat_interface import ChatInterface

chat = ChatInterface.from_pickle('models/brain_templates/brain_v13.pkl')
print(f'初始词表大小: {chat.tokenizer.actual_vocab_size}')

# 测试"STR" - 在SQLite但不在20k词表
test_word = 'STR'
print(f'\n测试词: {test_word}')
print(f'  在tokenizer中: {test_word in chat.tokenizer.token2id}')

# 编码触发动态加载
ids = chat.tokenizer.encode(test_word, chat.embedding)
print(f'  编


文件 tools/check_word_ids.py: 检查特定词的id | 定义:

#!/usr/bin/env python3
"""检查特定词的id"""
import sys
sys.path.insert(0, '/home/admin/swarm')
import sqlite3

DB = 'models/brain_v13_variants/pretrained/word_vectors.db'
conn = sqlite3.connect(DB)
cursor = conn.cursor()

# 检查特定词的id
test_words = ['python', '你好', '学习', '深度', '神经网络', '人工智能', '虫群', '智能', '代码', '算法']
for w in test_words:
    cursor.execute('SELECT id FROM word_vectors WHERE word = ?', (w,))
    row = cursor.fetchone()
    if row:
        print(f"'{w}': id = {row[0]}")
    else:
        pr


文件 tools/test_db_query.py: 测试SQLite查询速度 | 定义:

#!/usr/bin/env python3
"""测试SQLite查询速度"""
import sqlite3
import time
import numpy as np

DB = 'models/brain_v13_variants/pretrained/word_vectors.db'
conn = sqlite3.connect(DB)
cursor = conn.cursor()

test_words = ['你', '好', '学习', 'python', '深度', '神经网络', '机器学习', '人工智能', '虫群', '智能体']
times = []

for w in test_words:
    start = time.time()
    cursor.execute('SELECT vector FROM word_vectors WHERE word = ?', (w,))
    row = cursor.fetchone()
    elapsed = (time.time() - start) * 1000
    times.appe


文件 tools/build_word_vector_db.py: 将fasttext中文词向量gzip文件转换为SQLite索引
词→向量存储，支持按需查询 | 定义: def build_sqlite_index, def query_word

#!/usr/bin/env python3
"""
将fasttext中文词向量gzip文件转换为SQLite索引
词→向量存储，支持按需查询
"""
import gzip
import sqlite3
import numpy as np
import os
import time

GZIP_PATH = 'models/brain_v13_variants/pretrained/cc.zh.300.vec.gz'
DB_PATH = 'models/brain_v13_variants/pretrained/word_vectors.db'

def build_sqlite_index():
    """从gzip构建SQLite索引"""

    # 删除旧库
    if os.path.exists(DB_PATH):
        os.remove(DB_PATH)

    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()

    # 创建表：词id, 词文本,


函数 build_sqlite_index() 在 tools/build_word_vector_db.py: 从gzip构建SQLite索引

def build_sqlite_index():
    """从gzip构建SQLite索引"""

    # 删除旧库
    if os.path.exists(DB_PATH):
        os.remove(DB_PATH)

    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()

    # 创建表：词id, 词文本, 向量(存为blob)
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS word_vectors (
            id INTEGER PRIMARY KEY,
            word TEXT UNIQUE NOT NULL,
            vector BLOB NOT NULL
        )
    ''')
    cursor.execute('CREATE INDEX IF NOT EXISTS idx_word ON word_vectors(word)')
    conn.commit()

    print(f"开始构建SQLite索引: {GZIP_PATH}")
    print(f"目标数据库: {DB_PATH}")

    start_time = time.time()
    batch_size = 1000
    batch_data = []
    total_words = 0

    with gzip.open(GZIP_PATH, 'rt', encoding='utf-8') as f:
        # 第一行是总数 2000000，跳过
        next(f)

        for i, line in enumerate(f):
            parts = line.strip().split(' ')
            if len(parts) < 301:
                continue

            word = parts[0]
            # 取前300维，转float32存blob
            vec = np.array([float(x) for x in parts[1:301]], dtype=np.float32)
            vec_bytes = vec.tobytes()

            batch_data.append((word, vec_bytes))
            total_words += 1

            if len(batch_data) >= batch_size:
                cursor.executemany(
                    'INSERT OR IGNORE INTO word_vectors(word, vector) VALUES (?, ?)',
                    batch_data
                )
                conn.commit()
                batch_data = []

                if total_words % 50000 == 0:
                    elapsed = time.time() - start_time
                    print(f"已处理 {total_words:,} 词, 耗时 {elapsed:.1f}s")

    # 处理剩余数据
    if batch_data:
        cursor.executemany(
            'INSERT OR IGNORE INTO word_vectors(word, vector) VALUES (?, ?)',
            batch_data
        )
        conn.commit()

    elapsed = time.time() - start_time
    print(f"\n完成! 总词数: {total_words:,}, 耗时: {elapsed:.1f}s")


函数 query_word(word: str) 在 tools/build_word_vector_db.py: 测试查询单个词

def query_word(word: str):
    """测试查询单个词"""
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()

    cursor.execute('SELECT vector FROM word_vectors WHERE word = ?', (word,))
    row = cursor.fetchone()

    if row:
        vec = np.frombuffer(row[0], dtype=np.float32)
        print(f"词: {word}, 向量维度: {len(vec)}, 前5维: {vec[:5]}")
    else:
        print(f"词 '{word}' 不在词库中")

    conn.close()

if __name__ == '__main__':
    import sys

    if len(sys.argv) > 1:
        # 查询模式
        query_word(sys.argv[1])
    else:
        # 构建模式
        build_sqlite_index()


文件 tools/check_pkl.py: 检查pkl内容 | 定义:

#!/usr/bin/env python3
"""检查pkl内容"""
import sys
sys.path.insert(0, '/home/admin/swarm')
import pickle

pkl_path = '/home/admin/swarm/models/brain_templates/brain_v13.pkl'
with open(pkl_path, 'rb') as f:
    data = pickle.load(f)

print('Keys:', list(data.keys()))
for k, v in data.items():
    print(f'{k}: {type(v).__name__}')
    if hasattr(v, 'token2id'):
        print(f'  token2id: {len(v.token2id)} keys')
    if hasattr(v, 'weight'):
        print(f'  weight shape: {v.weight.shape}')
    if k


文件 tools/test_20k_tokenizer.py: 测试20k词表的编码解码 | 定义:

#!/usr/bin/env python3
"""测试20k词表的编码解码"""
import sys
sys.path.insert(0, '/home/admin/swarm')

import pickle
from core.neuro.text_encoder import SimpleTokenizer, EmbeddingLayer

with open('models/brain_v13_variants/pretrained/brain_v13_pretrained_20k.pkl', 'rb') as f:
    data = pickle.load(f)

tokenizer = data['tokenizer']
embedding = data['embedding']

test_words = ['你好', '学习', 'python', '深度学习', '神经网络', '人工智能', '虫群', '智能体']

print('=== 编码→解码测试 ===')
for word in test_words:
    ids = tokenizer.e


文件 tools/test_distributed2.py: 测试分布式连接 | 定义:

#!/usr/bin/env python3
"""测试分布式连接"""
from core.neuro.distributed import BrainOrchestrator
import numpy as np

# 测试分布式模式（不启动服务器，应该会超时失败）
print("Testing distributed mode (expect connection error)...")
try:
    # 模拟分布式配置
    hosts = {
        "sensory": "localhost",
        "hippocampus": "localhost",
        "association": "localhost",
        "prefrontal": "localhost",
        "motor": "localhost",
        "thalamus": "localhost",
    }
    orchestrator = BrainOrchestrator(mode="distributed", ho


文件 tools/test_chat_full.py: 测试完整对话功能 | 定义:

#!/usr/bin/env python3
"""测试完整对话功能"""
import sys
sys.path.insert(0, '/home/admin/swarm')

from core.neuro.chat_interface import ChatInterface

chat = ChatInterface.from_pickle('models/brain_templates/brain_v13.pkl')
print(f'初始词表大小: {chat.tokenizer.actual_vocab_size}')

# 测试正常对话
print('\n--- 对话测试 ---')
r1 = chat.chat("你好")
print(f'虫群: {r1["text"]}')

r2 = chat.chat("什么是Python?")
print(f'虫群: {r2["text"]}')

# 测试 teach 功能
print('\n--- Teach测试 ---')
chat.teach("你叫什么", "我叫虫群，是一个人工智能")
r3 = chat.chat(


文件 tools/deploy_hf_v13.py: 部署虫群v13到HF Space | 定义: def main

#!/usr/bin/env python3
"""
部署虫群v13到HF Space
"""
import os
import sys

# HF Token
HF_TOKEN = "hf_[REDACTED]"
REPO_ID = "lk080424/swarm-v13"

def main():
    from huggingface_hub import HfApi, login

    print("=" * 50)
    print("虫群v13 部署到HF Space")
    print("=" * 50)

    # 登录
    print("\n[1] 登录HF...")
    login(token=HF_TOKEN)
    api = HfApi()
    print("  ✅ 登录成功")

    # 创建repo
    print("\n[2] 创建Space...")
    try:
        api.create_repo(repo_id=REPO_ID


main

函数 main() 在 tools/deploy_hf_v13.py:

def main():
    from huggingface_hub import HfApi, login

    print("=" * 50)
    print("虫群v13 部署到HF Space")
    print("=" * 50)

    # 登录
    print("\n[1] 登录HF...")
    login(token=HF_TOKEN)
    api = HfApi()
    print("  ✅ 登录成功")

    # 创建repo
    print("\n[2] 创建Space...")
    try:
        api.create_repo(repo_id=REPO_ID, repo_type="space", repo_private=False)
        print("  ✅ Space创建成功")
    except Exception as e:
        if "already" in str(e).lower():
            print("  ℹ Space已存在")
        else:
            print(f"  ❌ 创建失败: {e}")
            return

    # 上传文件
    print("\n[3] 上传文件...")
    deploy_dir = "/home/admin/swarm/deploy/hf_spaces"
    files_to_upload = [
        "Dockerfile",
        "requirements-hf.txt",
    ]

    # 还需要上传核心代码
    core_files = []
    for root, dirs, files in os.walk("/home/admin/swarm"):
        # 跳过不需要的目录
        if any(x in root for x in ['__pycache__', '.git', 'venv', '.hermes', 'node_modules']):
            continue
        for f in files:
            if f.endswith(('.py', '.json', '.txt', '.pkl', '.npy')):
                full_path = os.path.join(root, f)
                rel_path = os.path.relpath(full_path, "/home/admin/swarm")
                core_files.append((full_path, rel_path))

    print(f"  发现 {len(core_files)} 个核心文件")

    # 上传Dockerfile和requirements
    for fname in files_to_upload:
        fpath = os.path.join(deploy_dir, fname)
        if os.path.exists(fpath):
            api.upload_file(
                path_or_fileobj=fpath,
                path_in_repo=fname,
                repo_id=REPO_ID,
                repo_type="space",
            )
            print(f"  ✅ 上传 {fname}")

    # 上传核心文件
    for full_path, rel_path in core_files[:100]:  # 限制数量
        try:
            with open(full_path, 'rb') as f:
                api.upload_file(
                    path_or_fileobj=f,
                    path_in_repo=rel_path,
                    repo_id=REPO_ID,
                    repo_type="sp


文件 tools/test_distributed.py: 测试分布式Brain | 定义:

#!/usr/bin/env python3
"""测试分布式Brain"""
from core.neuro.distributed import BrainOrchestrator
import numpy as np

# 测试本地模式
print("Testing local mode...")
orchestrator = BrainOrchestrator(mode="local")
print('Brain created:', type(orchestrator._brain).__name__)

# 快速测试
x = np.random.randn(32).astype(np.float32)
y = orchestrator.think(x)
print('Output shape:', y.shape)
print('Output sample:', y[:5])


文件 tools/test_trained.py:  | 定义: def test_brain

#!/usr/bin/env python3
import sys
sys.path.insert(0, '/home/admin/swarm')

import pickle
import numpy as np
from core.neuro.brain_v13 import BrainV13
from core.neuro.text_encoder import SimpleTokenizer, EmbeddingLayer

# Load
pkl_path = '/home/admin/swarm/models/brain_templates/brain_v13.pkl'
with open(pkl_path, 'rb') as f:
    data = pickle.load(f)

embedding = data['embedding']
tokenizer = data['tokenizer']
brain = data.get('brain_v13')

print("=" * 50)
print("Testing Trained BrainV13")
print(


函数 test_brain(text) 在 tools/test_trained.py:

def test_brain(text):
    ids = tokenizer.encode(text, embedding)
    if not ids or len(ids) < 3:
        print(f"{text}: encode failed")
        return

    vec = embedding.forward(ids[1:-1])
    if vec is None:
        print(f"{text}: forward failed")
        return

    out = brain.think(vec)

    # 尝试解码输出
    # 找最接近的词
    best_word = "<UNK>"
    best_dist = float('inf')

    for word, tid in list(tokenizer.token2id.items())[:500]:
        if tid < 4:
            continue
        try:
            w_ids = tokenizer.encode(word, embedding)
            if w_ids and len(w_ids) > 2:
                w_vec = embedding.forward(w_ids[1:-1])
                if w_vec is not None:
                    dist = np.linalg.norm(out - w_vec)
                    if dist < best_dist:
                        best_dist = dist
                        best_word = word
        except:
            continue

    print(f"Input: {text}")
    print(f"Output vec: {out[:5]}")
    print(f"Decoded: {best_word} (dist={best_dist:.4f})")
    print()

for text in ["hello", "nihao", "xuexi", "python"]:
    test_brain(text)


文件 tools/test_sqlite_lookup.py: 测试SQLite动态查词功能 | 定义:

#!/usr/bin/env python3
"""测试SQLite动态查词功能"""
import sys
sys.path.insert(0, '/home/admin/swarm')

from core.neuro.chat_interface import ChatInterface

chat = ChatInterface.from_pickle('models/brain_templates/brain_v13.pkl')
print('=== 测试动态查词 ===')
print(f'初始词表大小: {chat.tokenizer.actual_vocab_size}')
print(f'embedding._sqlite_db: {chat.embedding._sqlite_db}')
print(f'embedding._pca_matrix shape: {chat.embedding._pca_matrix.shape if chat.embedding._pca_matrix is not None else None}')
print(f'embeddi


文件 tools/build_embedding_20k.py: 构建优化的20000词词表：优先包含常用中文+IT术语 | 定义:

#!/usr/bin/env python3
"""
构建优化的20000词词表：优先包含常用中文+IT术语
"""
import os
import sys
sys.path.insert(0, '/home/admin/swarm')

import sqlite3
import numpy as np
import pickle
from sklearn.decomposition import PCA

DB_PATH = 'models/brain_v13_variants/pretrained/word_vectors.db'
OUTPUT_PKL = 'models/brain_v13_variants/pretrained/brain_v13_pretrained_20k.pkl'
VOCAB_SIZE = 20000

# 优先包含的词列表（用户可能用到的）
PRIORITY_WORDS = [
    # 常用中文单字
    '你', '我', '他', '她', '它', '们', '的', '是', '在', '有', '和', '与', '就', '不',


文件 tools/find_test_word.py:  | 定义:

import sqlite3
conn = sqlite3.connect('models/brain_v13_variants/pretrained/word_vectors.db')
c = conn.cursor()
# 找一个id在20000-30000之间的词
c.execute('SELECT word FROM word_vectors WHERE id BETWEEN 20000 AND 30000 LIMIT 5')
for row in c.fetchall():
    print(row[0])
conn.close()


文件 tools/test_adaptive.py: 测试自适应编排器 | 定义:

#!/usr/bin/env python3
"""测试自适应编排器"""
import sys
sys.path.insert(0, '/home/admin/swarm')

import numpy as np
from core.neuro.adaptive_orchestrator import create_orchestrator

x = np.random.randn(32).astype(np.float32)

# 测试本地模式
print("=== Test Local Mode ===")
orch = create_orchestrator('local')
y = orch.think(x)
print(f"Output: {y.shape}")
orch.shutdown()

# 测试部分分布式
print("\n=== Test Partial Mode ===")
orch = create_orchestrator('partial')
y = orch.think(x)
print(f"Output: {y.shape}")
orch.shut


文件 tools/test_chat.py:  | 定义:

#!/usr/bin/env python3
import sys
sys.path.insert(0, '/home/admin/swarm')

from core.neuro.chat_interface import ChatInterface

# Test full chat using classmethod
chat = ChatInterface.from_pickle('models/brain_templates/brain_v13.pkl')

print("=" * 50)
print("Testing Full Chat")
print("=" * 50)

# Teach first
print("Teaching: 我叫小明")
chat.chat("我叫小明")

print("\nTesting: 你叫什么?")
response = chat.chat("你叫什么?")
print(f"Response: {response}")

print("\nTesting: 你好")
response = chat.chat("你好")
print(f"


文件 tools/check_missing_words.py: 检查特定词是否在词库中 | 定义:

#!/usr/bin/env python3
"""检查特定词是否在词库中"""
import sys
sys.path.insert(0, '/home/admin/swarm')
import sqlite3

DB = 'models/brain_v13_variants/pretrained/word_vectors.db'
conn = sqlite3.connect(DB)
cursor = conn.cursor()

test_words = ['神经网络', '人工智能', '智能体', '机器学习', '深度学习', '自然语言']
for w in test_words:
    cursor.execute('SELECT word FROM word_vectors WHERE word LIKE ? LIMIT 5', (f'%{w}%',))
    rows = cursor.fetchall()
    if rows:
        print(f"'{w}' 相关: {[r[0] for r in rows]}")
    else:


文件 tools/train_brain.py:  | 定义: def load_data, def prepare_data, def train_step, def train, def main

#!/usr/bin/env python3
import sys
sys.path.insert(0, '/home/admin/swarm')

import numpy as np
import pickle
from core.neuro.text_encoder import SimpleTokenizer, EmbeddingLayer
from core.neuro.brain_v13 import BrainV13

def load_data():
    pkl_path = '/home/admin/swarm/models/brain_templates/brain_v13.pkl'
    print(f"Loading: {pkl_path}")
    with open(pkl_path, 'rb') as f:
        data = pickle.load(f)

    embedding = data['embedding']
    tokenizer = data['tokenizer']
    brain = data.g


函数 load_data() 在 tools/train_brain.py:

def load_data():
    pkl_path = '/home/admin/swarm/models/brain_templates/brain_v13.pkl'
    print(f"Loading: {pkl_path}")
    with open(pkl_path, 'rb') as f:
        data = pickle.load(f)

    embedding = data['embedding']
    tokenizer = data['tokenizer']
    brain = data.get('brain_v13')

    if brain is None:
        print("Creating new BrainV13")
        brain = BrainV13(input_dim=32, output_dim=32)

    return brain, embedding, tokenizer


函数 prepare_data(embedding, tokenizer, max_samples=500) 在 tools/train_brain.py:

def prepare_data(embedding, tokenizer, max_samples=500):
    token2id = tokenizer.token2id if hasattr(tokenizer, 'token2id') else {}
    print(f"Vocab size: {len(token2id)}")

    vecs = []
    for word, idx in list(token2id.items())[:max_samples]:
        if word.startswith('<'):
            continue
        try:
            ids = tokenizer.encode(word, embedding)
            if ids and len(ids) > 1:
                vec = embedding.forward(ids[1:-1])
                if vec is not None and len(vec) > 0:
                    vecs.append(vec)
        except:
            continue

    print(f"Samples: {len(vecs)}")
    return np.array(vecs, dtype=np.float32) if vecs else np.array([])


函数 train_step(brain, x, y, lr=0.1, eps=1e-3) 在 tools/train_brain.py:

def train_step(brain, x, y, lr=0.1, eps=1e-3):
    # x是32维，Brain输出也是32维
    output = brain.think(x)
    loss = np.mean((output - y) ** 2)

    # 训练Prefrontal的W_decision (32x32)
    if hasattr(brain.prefrontal, 'W_decision'):
        W = brain.prefrontal.W_decision
        grad = np.zeros_like(W)
        flat = W.ravel()
        for i in np.random.choice(W.size, min(50, W.size), replace=False):
            old = flat[i]
            flat[i] = old + eps
            loss_plus = np.mean((brain.think(x) - y) ** 2)
            flat[i] = old - eps
            loss_minus = np.mean((brain.think(x) - y) ** 2)
            grad.ravel()[i] = (loss_plus - loss_minus) / (2 * eps)
            flat[i] = old
        brain.prefrontal.W_decision -= lr * grad

    return loss


train

函数 train(brain, vecs, n_epochs=5, lr=0.1) 在 tools/train_brain.py:

def train(brain, vecs, n_epochs=5, lr=0.1):
    n = len(vecs)
    print(f"\nTraining: {n_epochs} epochs, {n} samples, lr={lr}")

    for epoch in range(n_epochs):
        total_loss = 0
        idx = np.random.permutation(n)[:100]

        for i in idx:
            loss = train_step(brain, vecs[i], vecs[i], lr)
            total_loss += loss

        avg = total_loss / len(idx)
        print(f"Epoch {epoch+1}: loss={avg:.6f}")
        lr *= 0.9

    return avg


main

函数 main() 在 tools/train_brain.py:

def main():
    print("=" * 40)
    print("BrainV13 Training")
    print("=" * 40)

    brain, embedding, tokenizer = load_data()
    vecs = prepare_data(embedding, tokenizer)

    if len(vecs) == 0:
        print("Error: No data")
        return

    train(brain, vecs, n_epochs=5, lr=0.1)

    pkl_path = '/home/admin/swarm/models/brain_templates/brain_v13.pkl'
    with open(pkl_path, 'rb') as f:
        data = pickle.load(f)
    data['brain_v13'] = brain

    with open(pkl_path, 'wb') as f:
        pickle.dump(data, f)

    print("\nSaved!")

    # Test
    test_word = "hello"
    ids = tokenizer.encode(test_word, embedding)
    if ids and len(ids) > 2:
        vec = embedding.forward(ids[1:-1])
        if vec is not None:
            out = brain.think(vec)
            print(f"\nTest '{test_word}':")
            print(f"  in shape: {vec.shape}, out shape: {out.shape}")
            print(f"  in: {vec[:5]}")
            print(f"  out: {out[:5]}")
            print(f"  diff: {np.linalg.norm(vec-out):.4f}")

if __name__ == '__main__':
    main()
EOF


文件 tools/test_full_chat.py: 测试虫群v13完整对话功能 | 定义:

#!/usr/bin/env python3
"""测试虫群v13完整对话功能"""
import sys
sys.path.insert(0, '/home/admin/swarm')

from core.neuro.chat_interface import ChatInterface
from core.neuro.brain_v13 import BrainV13
import pickle

print("=" * 50)
print("虫群v13 完整对话测试")
print("=" * 50)

# 加载brain
brain = None
try:
    with open('/home/admin/swarm/models/brain_templates/brain_v13.pkl', 'rb') as f:
        data = pickle.load(f)
        if 'brain_v13' in data:
            brain = data['brain_v13']
        elif 'brain' in data:


encryption.py

文件 security/encryption.py: 虫群智能体系统 — 安全加密层
AES-256加密 + 差分隐私 + 数据脱敏 + 访问控制 | 定义: class SecurityLayer, def __init__, def encrypt, def decrypt, def _generate_keystream, def mask_sensitive, def check_sensitive, def add_noise, def generate_token, def verify_token

#!/usr/bin/env python3
"""
虫群智能体系统 — 安全加密层
AES-256加密 + 差分隐私 + 数据脱敏 + 访问控制
"""

import base64
import hashlib
import logging
import os
import re
from typing import Dict, List, Optional

logger = logging.getLogger(__name__)

class SecurityLayer:
    """安全加密层 — 保护数据传输与存储"""

    def __init__(self, master_key: str = None):
        # 如果没有提供主密钥，使用环境变量或生成一个
        if master_key is None:
            master_key = os.environ.get("SWARM_SECURITY_KEY", "swarm_default_key_change_me")
        self._master_ke


SecurityLayer

安全加密层 — 保护数据传输与存储

类 SecurityLayer 在 security/encryption.py: 安全加密层 — 保护数据传输与存储 | 方法: __init__, encrypt, decrypt, _generate_keystream, mask_sensitive, check_sensitive, add_noise, generate_token, verify_token, hash_data, verify_integrity

class SecurityLayer:
    """安全加密层 — 保护数据传输与存储"""

    def __init__(self, master_key: str = None):
        # 如果没有提供主密钥，使用环境变量或生成一个
        if master_key is None:
            master_key = os.environ.get("SWARM_SECURITY_KEY", "swarm_default_key_change_me")
        self._master_key = master_key
        self._key_cache = {}
        logger.info("安全层初始化完成")

    # ============================================================
    # AES-256 加密/解密（纯Python实现，无外部依赖）
    # ============================================================

    def encrypt(self, plaintext: str, context: str = "") -> str:
        """
        加密文本
        使用ChaCha20风格的XOR流密码（纯Python，无pycryptodome依赖）
        生产环境应替换为真正的AES-256-GCM
        """
        if not plaintext:
            return ""

        # 派生密钥流
        key_material = f"{self._master_key}:{context}".encode()
        key_hash = hashlib.sha256(key_material).digest()

        # 生成伪随机密钥流（XOR流）
        data = plaintext.encode("utf-8")
        nonce = os.urandom(16)

        # 基于密钥和nonce生成密钥流
        stream = self._generate_keystream(key_hash, nonce, len(data))

        # XOR加密
        encrypted = bytes(a ^ b for a, b in zip(data, stream))

        # 格式: base64(nonce + encrypted)
        result = base64.b64encode(nonce + encrypted).decode("ascii")
        return result

    def decrypt(self, ciphertext: str, context: str = "") -> str:
        """解密文本"""
        if not ciphertext:
            return ""

        try:
            raw = base64.b64decode(ciphertext)
            nonce = raw[:16]
            encrypted = raw[16:]

            # 派生相同密钥流
            key_material = f"{self._master_key}:{context}".encode()
            key_hash = hashlib.sha256(key_material).digest()

            stream = self._generate_keystream(key_hash, nonce, len(encrypted))
            decrypted = bytes(a ^ b for a, b in zip(encrypted, stream))

            return decrypted.decode("utf-8")
        except Exception as e:
            logger.error(f"解密失败: {e}")
            return ""

    def _gener


文件 tests/test_sensory_new.py: 测试新SensoryRegion | 定义:

#!/usr/bin/env python3
"""测试新SensoryRegion"""
import numpy as np
import sys
sys.path.insert(0, '/home/admin/swarm')

from core.neuro.regions.sensory_region import SensoryRegion

sr = SensoryRegion(input_dim=64)

print("=== 新SensoryRegion测试 ===\n")
print(f"参数量: {sr.total_params}")

# 测试不同量级输入
np.random.seed(42)
cases = [
    ("微小(0.05)", np.random.randn(64).astype(np.float32) * 0.05),
    ("小(0.3)", np.random.randn(64).astype(np.float32) * 0.3),
    ("标准(1.0)", np.random.randn(64).astype(np.float


文件 tests/test_decode_quality.py: 诊断解码质量——为什么输出<UNK> | 定义:

#!/usr/bin/env python3
"""诊断解码质量——为什么输出<UNK>"""
import sys
sys.path.insert(0, '/home/admin/swarm')
from core.neuro.chat_interface import ChatInterface
import numpy as np

ci = ChatInterface.from_pickle('models/brain_templates/brain_v13.pkl')

# 1. 看embedding的维度和词表
emb = ci.embedding
tok = ci.tokenizer
print(f"embedding: embed_dim={emb.embed_dim}, vocab_size={emb.weight.shape[0]}")
print(f"tokenizer: vocab_size={tok.vocab_size}")

# 2. 看BrainV13输出维度
result = ci.brain.think(emb.forward(tok.encode(


文件 tests/test_hybrid_result.py: 诊断hybrid推理结果 | 定义:

#!/usr/bin/env python3
"""诊断hybrid推理结果"""
import sys; sys.path.insert(0, '/home/admin/swarm')
from core.neuro.chat_interface import ChatInterface

ci = ChatInterface.from_pickle('/home/admin/swarm/models/brain_templates/brain_v13.pkl')

for t in ["你好", "hello", "Python编程"]:
    result = ci.hybrid.forward(t)
    print(f"'{t}' → {result}")
    print()


文件 tests/e2e_test_plan.py: 虫群端到端测试方案
4设备: 云电脑 + 本地电脑 + 2手机 + 香港服务器
手机部署: ONNX导出 + TFLite/NCNN推理 | 定义: def get_test_plan


函数 get_test_plan() 在 tests/e2e_test_plan.py:

def get_test_plan():
    return TEST_PLAN

if __name__ == "__main__":
    print(TEST_PLAN)


文件 tests/test_e2e_v13.py: 虫群v13 端到端测试

测试完整流程: 加载v13模型 → ChatInterface → 对话 | 定义:

#!/usr/bin/env python3
"""
虫群v13 端到端测试

测试完整流程: 加载v13模型 → ChatInterface → 对话
"""
import sys
sys.path.insert(0, '/home/admin/swarm')

from core.neuro.chat_interface import ChatInterface

print("=== 虫群v13 端到端测试 ===\n")

# 1. 加载v13模型
print("1. 加载v13模型...")
try:
    chat = ChatInterface.from_pickle(
        'models/brain_templates/brain_v13.pkl'
    )
    print("  加载成功!")
except Exception as e:
    print(f"  加载失败: {e}")
    sys.exit(1)

# 2. 检查Brain版本
brain_type = type(chat.brain).__name__
print(f"


文件 tests/verify_full_v3.py: 虫群v3.0 全量验证 — 含本地推理模型群 | 定义: def check, def t_types, def t_registry, def t_memory, def t_router, def t_aggregation, def t_moa, def t_royal_agent, def t_encryption, def t_mask

#!/usr/bin/env python3
"""
虫群v3.0 全量验证 — 含本地推理模型群
"""
import sys
import time
import os

sys.path.insert(0, "/home/admin/swarm")

print("=" * 60)
print("  虫群智能体 v3.0 全量验证")
print("=" * 60)

passed = 0
failed = 0

def check(name, func):
    global passed, failed
    try:
        func()
        print(f"  ✅ {name}")
        passed += 1
    except Exception as e:
        print(f"  ❌ {name}: {str(e)[:100]}")
        failed += 1

# ============ 1. 核心模块 ============
print("\n📦 [1] 核心模块")

def t_types()


check

函数 check(name, func) 在 tests/verify_full_v3.py:

def check(name, func):
    global passed, failed
    try:
        func()
        print(f"  ✅ {name}")
        passed += 1
    except Exception as e:
        print(f"  ❌ {name}: {str(e)[:100]}")
        failed += 1

# ============ 1. 核心模块 ============
print("\n📦 [1] 核心模块")


函数 t_types() 在 tests/verify_full_v3.py:

def t_types():
    from core.types import ModelInfo, ModelType, TaskContext, ComplexityLevel, ModelResult, MemoryCategory
    m = ModelInfo(model_id="test", name="测试", model_type=ModelType.LOCAL, description="测试模型")
    assert m.model_id == "test"
    tc = TaskContext(query="你好", complexity_score=0.3)
    tc = tc.compute_complexity_level()
    assert tc.complexity_level == ComplexityLevel.MEDIUM
check("types 统一类型", t_types)


函数 t_registry() 在 tests/verify_full_v3.py:

def t_registry():
    from core.model_registry import ModelRegistry
    from core.types import ModelInfo, ModelType
    r = ModelRegistry()
    r.register(ModelInfo(model_id="r_test", name="测试", model_type=ModelType.LOCAL, description="测试"))
    assert len(r.list_models()) >= 1
    m = r.get("r_test")
    assert m.model_id == "r_test"
check("registry 模型注册表", t_registry)


函数 t_memory() 在 tests/verify_full_v3.py:

def t_memory():
    from core.memory_core import MemoryCore
    from core.types import MemoryCategory
    db = "/tmp/swarm_v3_test.db"
    if os.path.exists(db):
        os.unlink(db)
    mem = MemoryCore(db_path=db)
    mid = mem.store("u1", "c1", "测试", "你好世界", "你好！", category=MemoryCategory.GREETING)
    assert mid
    results = mem.retrieve("你好", top_k=5)
    assert len(results) >= 1
check("memory 记忆核心", t_memory)


函数 t_router() 在 tests/verify_full_v3.py:

def t_router():
    from core.router import IntelligentRouter
    from core.model_registry import ModelRegistry
    from core.types import ModelInfo, ModelType
    r = ModelRegistry()
    r.register(ModelInfo(model_id="local", name="本地", model_type=ModelType.LOCAL, description="本地"))
    router = IntelligentRouter(r)
    simple = router.analyze("你好")
    assert simple.complexity_score < 0.4
    hard = router.analyze("深入分析深度学习优化方案")
    assert hard.complexity_score > 0.5
check("router 智能路由", t_router)


函数 t_aggregation() 在 tests/verify_full_v3.py:

def t_aggregation():
    from core.aggregation import AggregationEngine
    from core.types import ModelResult
    mock = [
        ModelResult(model_id="a", response="r1", confidence=0.9, latency_ms=10, success=True),
        ModelResult(model_id="b", response="r2", confidence=0.6, latency_ms=20, success=True),
    ]
    eng = AggregationEngine()
    r = eng.aggregate("测试", mock, method="confidence")
    assert r.final_response
check("aggregation 聚合引擎", t_aggregation)


函数 t_moa() 在 tests/verify_full_v3.py:

def t_moa():
    from core.moa_engine import MOAEngine
    from core.model_registry import ModelRegistry
    from core.types import ModelInfo, ModelType
    r = ModelRegistry()
    r.register(ModelInfo(model_id="local", name="本地", model_type=ModelType.LOCAL, description="本地"))
    moa = MOAEngine(r)
    result = moa.process(query="你好", method="confidence")
    assert result.final_response
check("moa MOA引擎", t_moa)


函数 t_royal_agent() 在 tests/verify_full_v3.py:

def t_royal_agent():
    from core.royal_agent import RoyalAgent
    agent = RoyalAgent()
    resp = agent.chat("你好")
    assert isinstance(resp, str) and len(resp) > 0
check("royal_agent 虫皇智能体", t_royal_agent)

# ============ 2. 安全与网络 ============
print("\n📦 [2] 安全与网络")


函数 t_encryption() 在 tests/verify_full_v3.py:

def t_encryption():
    from security.encryption import SecurityLayer
    sec = SecurityLayer()
    enc = sec.encrypt("敏感数据123")
    dec = sec.decrypt(enc)
    assert dec == "敏感数据123"
check("encryption 加密解密", t_encryption)


函数 t_mask() 在 tests/verify_full_v3.py:

def t_mask():
    from security.encryption import SecurityLayer
    sec = SecurityLayer()
    masked = sec.mask_sensitive("手机号13812345678")
    assert masked != "手机号13812345678"
check("encryption PII脱敏", t_mask)


函数 t_network() 在 tests/verify_full_v3.py:

def t_network():
    from network.swarm_network import SwarmNetwork
    net = SwarmNetwork()
    status = net.get_network_status()
    assert status is not None
check("network 网络层", t_network)

# ============ 3. 技能模块 ============
print("\n📦 [3] 技能模块")


函数 t_text_parser() 在 tests/verify_full_v3.py:

def t_text_parser():
    from skills.text_parser import TextParserSkill
    from skills.base_skill import SkillInput
    p = TextParserSkill()
    out = p.execute(SkillInput(text="请解释一下机器学习的原理"))
    assert out.success
    assert out.result["text_type"] in ("question", "command")
check("text_parser 文本解析", t_text_parser)


函数 t_task_classifier() 在 tests/verify_full_v3.py:

def t_task_classifier():
    from skills.task_classifier import TaskClassifierSkill
    from skills.base_skill import SkillInput
    c = TaskClassifierSkill()
    out = c.execute(SkillInput(text="写一个Python爬虫"))
    assert out.success
    assert out.result["category"] == "code"
check("task_classifier 任务分类", t_task_classifier)


函数 t_safety_filter() 在 tests/verify_full_v3.py:

def t_safety_filter():
    from skills.safety_filter import SafetyFilterSkill
    from skills.base_skill import SkillInput
    s = SafetyFilterSkill()
    out = s.execute(SkillInput(text="联系13812345678"))
    assert out.success
    assert len(out.result["detected_issues"]) > 0
check("safety_filter 安全过滤", t_safety_filter)


函数 t_dialogue() 在 tests/verify_full_v3.py:

def t_dialogue():
    from skills.dialogue_manager import DialogueManagerSkill
    from skills.base_skill import SkillInput
    d = DialogueManagerSkill()
    out1 = d.execute(SkillInput(text="你好", session_id="t1"))
    assert out1.result["topic"] == "greeting"
check("dialogue_manager 对话管理", t_dialogue)


函数 t_pipeline() 在 tests/verify_full_v3.py:

def t_pipeline():
    from skills import create_default_pipeline
    p = create_default_pipeline()
    results = p.execute("请帮我写一个加密算法")
    assert len(results) == 4
    assert all(o.success for o in results.values())
check("pipeline 技能管线", t_pipeline)

# ============ 4. 性能检查 ============
print("\n📦 [4] 性能检查")


函数 t_performance() 在 tests/verify_full_v3.py:

def t_performance():
    from core.router import IntelligentRouter
    from core.model_registry import ModelRegistry
    from core.types import ModelInfo, ModelType
    r = ModelRegistry()
    r.register(ModelInfo(model_id="local", name="本地", model_type=ModelType.LOCAL, description="本地"))
    router = IntelligentRouter(r)
    start = time.perf_counter()
    for _ in range(100):
        router.analyze("深入分析深度学习优化方案")
    elapsed = (time.perf_counter() - start) * 1000 / 100
    assert elapsed < 100
check("性能: 路由<100ms", t_performance)

# ============ 5. 文件完整性 ============
print("\n📦 [5] 文件完整性")

required_files = [
    "core/types.py", "core/model_registry.py", "core/memory_core.py",
    "core/router.py", "core/aggregation.py", "core/moa_engine.py",
    "core/royal_agent.py", "core/__init__.py",
    "core/performance_monitor.py", "core/config_center.py",
    "core/conversation.py", "core/health_checker.py",
    "core/smart_cache.py", "core/logging_config.py",
    "core/local_inference.py",
    "security/encryption.py", "security/__init__.py",
    "network/swarm_network.py", "network/__init__.py",
    "skills/base_skill.py", "skills/text_parser.py",
    "skills/task_classifier.py", "skills/safety_filter.py",
    "skills/dialogue_manager.py", "skills/__init__.py",
    "run_agent.py", "web_interface.py",
    "README.md", "development_log.md",
]


函数 t_files() 在 tests/verify_full_v3.py:

def t_files():
    missing = []
    for f in required_files:
        path = f"/home/admin/swarm/{f}"
        if not os.path.exists(path):
            missing.append(f)
    assert not missing, f"缺少文件: {missing}"
check("文件完整性 (29个文件)", t_files)

# ============ 6. v2.1-v3.0新增模块 ============
print("\n📦 [6] v2.1-v3.0新增模块")


函数 t_performance_monitor() 在 tests/verify_full_v3.py:

def t_performance_monitor():
    from core.performance_monitor import get_monitor
    monitor = get_monitor()
    monitor.record("test_model", 100.0, 0.8, True, "test", "")
    report = monitor.get_report()
    assert "test_model" in report
check("performance_monitor 性能监控", t_performance_monitor)


函数 t_config_center() 在 tests/verify_full_v3.py:

def t_config_center():
    from core.config_center import get_config
    config = get_config()
    val = config.get("models.local_memory.name", "默认")
    assert val is not None
check("config_center 配置中心", t_config_center)


函数 t_conversation() 在 tests/verify_full_v3.py:

def t_conversation():
    from core.conversation import get_conversation_manager
    mgr = get_conversation_manager()
    cid = mgr.new_conversation()
    mgr.add_message("user", "测试", conversation_id=cid)
    ctx = mgr.get_context(cid, max_tokens=100)
    assert ctx is not None
check("conversation 对话管理", t_conversation)


函数 t_health_checker() 在 tests/verify_full_v3.py:

def t_health_checker():
    from core.health_checker import get_health_checker
    checker = get_health_checker()
    checker.record_success("test_model", 50.0)
    fallback = checker.get_fallback("test_model")
    # test_model健康，无需故障转移
    report = checker.get_report()
    assert report is not None
check("health_checker 健康检查", t_health_checker)


函数 t_smart_cache() 在 tests/verify_full_v3.py:

def t_smart_cache():
    from core.smart_cache import get_cache
    cache = get_cache()
    cache.put("测试查询", "测试回复", "test_model", 0.8)
    cached = cache.get("测试查询")
    assert cached is not None
    assert cached.response == "测试回复"
check("smart_cache 智能缓存", t_smart_cache)


函数 t_logging_config() 在 tests/verify_full_v3.py:

def t_logging_config():
    from core.logging_config import setup_logging
    import logging
    setup_logging("test_verify")
    logger = logging.getLogger("test_verify")
    logger.info("验证日志系统")
check("logging_config 日志系统", t_logging_config)


函数 t_local_inference() 在 tests/verify_full_v3.py:

def t_local_inference():
    from core.local_inference import LocalModelBackend
    backend = LocalModelBackend()
    available = backend.list_available()
    # 至少tiny应该存在
    assert len(available) > 0, "没有训练好的模型"
    # 测试推理
    for model_id in available:
        result = backend.infer(model_id, "你好")
        assert result["success"], f"推理失败: {result}"
        assert len(result["response"]) > 0
        break  # 只测试第一个
check("local_inference 本地推理", t_local_inference)


函数 t_model_registry_with_inference() 在 tests/verify_full_v3.py:

def t_model_registry_with_inference():
    from core.model_registry import get_registry, init_default_models
    registry = get_registry()
    init_default_models()
    models = registry.list_models()
    # 应该包含本地推理模型
    inference_models = [m for m in models if "inference" in m.tags]
    assert len(inference_models) > 0, f"无本地推理模型，全部模型: {[m.model_id for m in models]}"
check("registry含推理模型", t_model_registry_with_inference)

# ============ 总结 ============
total = passed + failed
print("\n" + "=" * 60)
print(f"  验证结果: {passed}/{total} 通过")
if failed == 0:
    print("  🎉 虫群智能体 v3.0 全量验证通过！")
else:
    print(f"  ⚠ {failed} 项失败，需要修复")
print("=" * 60)


文件 tests/save_variants.py: 保存虫群BrainV13多版本变体

版本说明:
1. monolithic  — 融合版: 6脑区单进程，当前主开发版
2. distributed — 分布式版: 6脑区独立服务，消息通信
3. pretrained  — 预训练版: fasttext/professional embedding
4. blank       — 空白版: 纯随机初始化，无任何训练数据 | 定义: def serialize_vector, def deserialize_vector, def pack_message, def unpack_message, class BrainRegionService, def __init__, def setup, def forward, def run, def start_all

sys os pickle numpy datetime core.neuro.chat_interface core.neuro.brain_v13 core.neuro.text_encoder numpy msgpack base64 zmq threading time common.protocol subprocess sys os time signal core.neuro.chat_interface

#!/usr/bin/env python3
"""
保存虫群BrainV13多版本变体

版本说明:
1. monolithic  — 融合版: 6脑区单进程，当前主开发版
2. distributed — 分布式版: 6脑区独立服务，消息通信
3. pretrained  — 预训练版: fasttext/professional embedding
4. blank       — 空白版: 纯随机初始化，无任何训练数据
"""

import sys
sys.path.insert(0, '/home/admin/swarm')

import os
import pickle
import numpy as np
from datetime import datetime

BASE = '/home/admin/swarm/models/brain_v13_variants'
os.makedirs(BASE, exist_ok=True)

# ============================================================
# 1


BrainRegionService

类 BrainRegionService 在 tests/save_variants.py: 脑区服务基类 | 方法: __init__, setup, forward, run

class BrainRegionService:
    """脑区服务基类"""

    name = "base"           # 子类覆盖
    input_dim = 32          # 输入维度
    output_dim = 32         # 输出维度
    default_port = 5000     # 默认端口

    # 上下游连接 (子类覆盖)
    upstream_ports = []     # 接收消息的端口
    downstream_addrs = []   # 发送消息的地址

    def __init__(self, port: int = None):
        self.port = port or self.default_port
        self.region = None   # 子类在setup()中初始化
        self.running = False

    def setup(self):
        """初始化脑区模型 (子类实现)"""
        pass

    def forward(self, vec, metadata=None):
        """处理输入 (子类实现)"""
        raise NotImplementedError

    def run(self):
        """启动服务"""
        self.setup()
        self.running = True

        ctx = zmq.Context()
        # 接收socket
        receiver = ctx.socket(zmq.PULL)
        receiver.bind(f"tcp://*:{self.port}")
        # 发送socket
        sender = ctx.socket(zmq.PUSH)
        for addr in self.downstream_addrs:
            sender.connect(addr)

        print(f"[{self.name}] 服务启动 port={self.port}")

        while self.running:
            try:
                msg = unpack_message(receiver.recv())
                result = self.forward(msg['vector'], msg.get('metadata'))
                # 发送到下游
                response = {
                    'type': 'forward',
                    'source': self.name,
                    'vector': result,
                    'metadata': msg.get('metadata', {}),
                }
                sender.send(pack_message(response))
            except Exception as e:
                print(f"[{self.name}] 错误: {e}")

        receiver.close()
        sender.close()
        ctx.term()
'''

with open(os.path.join(dist_dir, 'common', 'service_base.py'), 'w') as f:
    f.write(service_base_code)

# --- 各脑区服务端口映射 ---
services_config = '''#!/usr/bin/env python3
"""
虫群v13 分布式部署 — 端口与连接配置

6脑区 + 1协调器 = 7个服务

信息流:
  输入 → [5551] 感觉区 → [5552] 丘脑 → [5553] 海马区 → [5554] 联合区 → [5555] 前额区 → [55


setup

方法 BrainRegionService.setup():
        """初始化脑区模型 (子类实现)"""
        pass

def setup(self):
        """初始化脑区模型 (子类实现)"""
        pass


forward

方法 BrainRegionService.forward():
        """处理输入 (子类实现)"""
        raise NotImplementedError

def forward(self):
        """处理输入 (子类实现)"""
        raise NotImplementedError


run

方法 BrainRegionService.run():
        """启动服务"""
        self.setup()
        self.running = True

        ctx = zmq.Cont

def run(self):
        """启动服务"""
        self.setup()
        self.running = True

        ctx = zmq.Context()
        # 接收socket
        receiver = ctx.socket(zmq.PULL)
        receiver.bind(f"tcp://*:{self.port}")
        # 发送socket
        sender = ctx.socket(zmq.PUSH)
        for addr in self.downstream_addrs:
            sender.connect(addr)

        print(f"[{self.name}] 服务启动 port={self.port}")

        while self.running:
            try:
                msg = unpack_message(receiver.recv())
                result = self.forward(msg['vector'], msg.get('metadata'))
                # 发送到下游
                response = {
                    'type': 'forward',
                    'source': self.name,
                    'vector': result,
                    'metadata': msg.get('metadata', {}),
                }
                sender.send(pack_message(response))
            except Exception as e:
                print(f"[{self.name}] 错误: {e}")

        r


函数 start_all() 在 tests/save_variants.py:

def start_all():
    for name, cfg in SERVICES.items():
        print(f"启动 {name} (port {cfg['port']})...")
        p = subprocess.Popen(
            [sys.executable, cfg['script']],
            cwd=os.path.dirname(os.path.abspath(__file__)),
        )
        processes[name] = p
        time.sleep(0.5)  # 等待端口绑定
    print(f"全部 {len(processes)} 个服务已启动")


函数 stop_all() 在 tests/save_variants.py: # 虫群 BrainV13 版本变体

目录结构:
```
brain_v13_variants/
├── README.md              # 本文件
├── monolithic/            # 融合版 (当前主开发版)
│   └── brain_v13_monolithic.pkl
├── blank/                 # 空白版 (纯随机初始化)

def stop_all():
    for name, p in processes.items():
        p.terminate()
    print("全部服务已停止")

if __name__ == '__main__':
    if '--stop' in sys.argv:
        stop_all()
    else:
        try:
            start_all()
            for p in processes.values():
                p.wait()
        except KeyboardInterrupt:
            stop_all()
'''

with open(os.path.join(dist_dir, 'launcher.py'), 'w') as f:
    f.write(launcher_code)

print("  分布式版骨架已创建")

# ============================================================
# 写版本总README
# ============================================================
print("\n=== 5. 写版本说明 ===")

readme = """# 虫群 BrainV13 版本变体

目录结构:
```
brain_v13_variants/
├── README.md              # 本文件
├── monolithic/            # 融合版 (当前主开发版)
│   └── brain_v13_monolithic.pkl
├── blank/                 # 空白版 (纯随机初始化)
│   └── brain_v13_blank.pkl
├── pretrained/            # 预训练版 (待加载fasttext)
│   └── brain_v13_pretrained.pkl
└── distributed/           # 分布式版 (6脑区独立服务)
    ├── common/
    │   ├── protocol.py    # 消息通信协议 (msgpack+numpy)
    │   └── service_base.py # 服务基类 (ZeroMQ)
    ├── services/
    │   └── config.py      # 端口映射+启动顺序
    └── launcher.py        # 一键启动/停止
```

## 版本对比

| 特性 | 融合版 | 空白版 | 预训练版 | 分布式版 |
|------|--------|--------|----------|----------|
| 架构 | 单进程 | 单进程 | 单进程 | 6服务+协调器 |
| Embedding | SVD训练 | 随机 | 待fasttext | 同融合版 |
| Brain权重 | SVD+随机 | 随机 | 待训练 | 同融合版 |
| 循环推理 | 5轮 | 5轮 | 5轮 | 协调器控制 |
| 通信 | 函数调用 | 函数调用 | 函数调用 | ZeroMQ消息 |
| 部署 | 单进程 | 单进程 | 单进程 | 多进程/多机 |
| 用途 | 主开发 | 对比实验 | 生产目标 | 扩展部署 |

## 融合版 (Monolithic)
- 当前主开发版，已验证可运行
- SVD训练embedding (PPMI→SVD32维，4000条中文语料)
- 编码→解码验证: "你好"→"你好起"，"学习"→"学习术"
- 6脑区串行推理: Sensory→Thalamus→Hippocampus→Association→Prefrontal→Motor
- 输出: 思考摘要格式 [虫群思考5轮|记忆置信X|信号Y]

## 空白版 (Blank)
- 纯随机初始化，无任何训练数据
- 用于对比实验: 训练前 vs 训练后
- 可作为全新训练起点

## 预训练版 (Pretrained)
- 骨架版，待加载专业预训练向量
- 计划: fasttext cc.zh.300.vec → PCA降维32维 → 替换embedding
- Brain权重待训练 (输出对齐embedding空间)
- 目标: 生产级质量

## 分布式版 (Distributed)
- 6脑区拆分为独立ZeroMQ服务
- 通信协议:


文件 tests/fix_pkl.py: 检查并修复pkl格式 | 定义:

#!/usr/bin/env python3
"""检查并修复pkl格式"""
import pickle
import sys
sys.path.insert(0, '/home/admin/swarm')

# 检查当前pkl
with open('models/brain_templates/brain_v13.pkl', 'rb') as f:
    data = pickle.load(f)
print('type:', type(data))

if isinstance(data, dict):
    print('keys:', list(data.keys()))
    print('version:', data.get('version'))
elif hasattr(data, '__class__'):
    print('BrainV13对象, 需要包装成dict')
    # 包装成v13格式dict
    from core.neuro.chat_interface import ChatInterface
    ci = ChatInte


文件 tests/test_perf_v2.py:  | 定义:

#!/usr/bin/env python3
import sys, os
sys.path.insert(0, "/home/admin/swarm")

from core.performance_monitor import PerformanceMonitor

print("创建监控器...")
monitor = PerformanceMonitor()

print("记录指标...")
for i in range(5):
    monitor.record("local_memory", latency_ms=5+i*2, confidence=0.3+i*0.05, success=True, query="测试")
monitor.record("glm_api", latency_ms=1200, confidence=0.8, success=True, query="Python")
monitor.record("glm_api", latency_ms=800, confidence=0.7, success=True, query="ML")
mon


文件 tests/test_p2p_network.py: P2P网络多节点通信验证测试 | 定义: def test_swarm_node, def test_peer_registration, def test_heartbeat_timeout, def test_message_protocol, def test_message_handler, def on_chat, def on_federation, def test_federation_round, def test_network_status, def test_multi_node_communication

sys os time datetime network.swarm_network

#!/usr/bin/env python3
"""P2P网络多节点通信验证测试"""

import sys
import os
import time
from datetime import datetime, timedelta

sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))

from network.swarm_network import SwarmNode, SwarmNetwork

def test_swarm_node():
    """测试1: 节点创建与序列化"""
    node = SwarmNode(node_id="queen-001", host="192.168.1.10",
                     port=8765, capabilities=["chat", "memory", "federation"])
    d = node.to_dict()
    assert d["node_id"]


测试1: 节点创建与序列化

函数 test_swarm_node() 在 tests/test_p2p_network.py: 测试1: 节点创建与序列化

def test_swarm_node():
    """测试1: 节点创建与序列化"""
    node = SwarmNode(node_id="queen-001", host="192.168.1.10",
                     port=8765, capabilities=["chat", "memory", "federation"])
    d = node.to_dict()
    assert d["node_id"] == "queen-001"
    assert d["host"] == "192.168.1.10"
    assert d["port"] == 8765
    assert "federation" in d["capabilities"]
    assert d["status"] == "online"
    print("  [PASS] 节点创建与序列化")


测试2: 对等节点注册与发现

函数 test_peer_registration() 在 tests/test_p2p_network.py: 测试2: 对等节点注册与发现

def test_peer_registration():
    """测试2: 对等节点注册与发现"""
    local = SwarmNode(node_id="local-001", capabilities=["chat", "memory"])
    net = SwarmNetwork(local_node=local)

    # 注册3个对等节点
    peers = [
        SwarmNode(node_id="queen-A", host="10.0.0.1", capabilities=["chat", "federation"]),
        SwarmNode(node_id="queen-B", host="10.0.0.2", capabilities=["chat", "memory"]),
        SwarmNode(node_id="queen-C", host="10.0.0.3", capabilities=["chat", "memory", "federation"]),
    ]
    for p in peers:
        net.register_peer(p)

    # 发现所有在线节点
    all_nodes = net.discover_nodes()
    assert len(all_nodes) == 4  # local + 3 peers

    # 发现具有federation能力的节点
    fed_nodes = net.discover_nodes(capability="federation")
    assert len(fed_nodes) == 2  # queen-A + queen-C

    # 注销一个节点
    net.unregister_peer("queen-B")
    all_nodes = net.discover_nodes()
    assert len(all_nodes) == 3  # local + 2 peers
    print("  [PASS] 对等节点注册与发现")


函数 test_heartbeat_timeout() 在 tests/test_p2p_network.py: 测试3: 心跳超时检测

def test_heartbeat_timeout():
    """测试3: 心跳超时检测"""
    local = SwarmNode(node_id="local-001")
    net = SwarmNetwork(local_node=local)

    peer = SwarmNode(node_id="slow-node", host="10.0.0.99")
    net.register_peer(peer)

    # 模拟超时: 设置peer的最后心跳为61秒前
    net.peers["slow-node"].last_heartbeat = datetime.now() - timedelta(seconds=61)

    # 发送心跳，触发超时清理
    net.heartbeat()

    assert net.peers["slow-node"].status == "offline"
    print("  [PASS] 心跳超时检测")


测试4: 消息协议创建与验证

函数 test_message_protocol() 在 tests/test_p2p_network.py: 测试4: 消息协议创建与验证

def test_message_protocol():
    """测试4: 消息协议创建与验证"""
    local = SwarmNode(node_id="node-A")
    net = SwarmNetwork(local_node=local)

    # 创建消息
    msg = net.create_message("chat", {"text": "你好虫群"}, target="node-B")
    assert msg["protocol"] == "swarm-v2"
    assert msg["msg_type"] == "chat"
    assert msg["source"] == "node-A"
    assert msg["target"] == "node-B"
    assert "checksum" in msg

    # 验证消息
    assert net.validate_message(msg) is True

    # 篡改消息应验证失败
    msg["payload"]["text"] = "被篡改"
    assert net.validate_message(msg) is False
    print("  [PASS] 消息协议创建与验证")


测试5: 消息处理器注册与分发

函数 test_message_handler() 在 tests/test_p2p_network.py: 测试5: 消息处理器注册与分发

def test_message_handler():
    """测试5: 消息处理器注册与分发"""
    local = SwarmNode(node_id="node-A")
    net = SwarmNetwork(local_node=local)

    received = []

    def on_chat(msg):
        received.append(msg["payload"]["text"])

    def on_federation(msg):
        received.append("fed:" + msg["msg_type"])

    net.register_handler("chat", on_chat)
    net.register_handler("federation", on_federation)

    # 发送chat消息
    msg = net.create_message("chat", {"text": "虫群消息测试"})
    net.handle_message(msg)
    assert received[-1] == "虫群消息测试"

    # 发送federation消息
    msg2 = net.create_message("federation", {"round": 1})
    net.handle_message(msg2)
    assert received[-1] == "fed:federation"

    # 无效消息应被忽略
    bad_msg = {"protocol": "unknown", "msg_type": "chat", "payload": {}}
    net.handle_message(bad_msg)  # 不应报错
    print("  [PASS] 消息处理器注册与分发")


测试6: 联邦学习轮次协调

函数 test_federation_round() 在 tests/test_p2p_network.py: 测试6: 联邦学习轮次协调

def test_federation_round():
    """测试6: 联邦学习轮次协调"""
    local = SwarmNode(node_id="local-001", capabilities=["chat", "federation"])
    net = SwarmNetwork(local_node=local)

    # 参与者不足
    result = net.start_federation_round(min_participants=2)
    assert result["status"] == "insufficient_participants"

    # 注册2个有federation能力的节点
    net.register_peer(SwarmNode(node_id="fed-A", capabilities=["chat", "federation"]))
    net.register_peer(SwarmNode(node_id="fed-B", capabilities=["chat", "federation"]))
    # 注册1个没有federation能力的节点
    net.register_peer(SwarmNode(node_id="no-fed", capabilities=["chat"]))

    result = net.start_federation_round(min_participants=2)
    assert result["status"] == "started"
    assert result["round"] == 1
    # 只有3个federation节点(local + fed-A + fed-B)
    assert len(result["participants"]) == 3

    # 第2轮
    result2 = net.start_federation_round(min_participants=2)
    assert result2["round"] == 2
    print("  [PASS] 联邦学习轮次协调")


函数 test_network_status() 在 tests/test_p2p_network.py: 测试7: 网络状态查询

def test_network_status():
    """测试7: 网络状态查询"""
    local = SwarmNode(node_id="local-001")
    net = SwarmNetwork(local_node=local)
    net.register_peer(SwarmNode(node_id="peer-1"))
    net.register_peer(SwarmNode(node_id="peer-2"))

    status = net.get_network_status()
    assert status["local_node"]["node_id"] == "local-001"
    assert status["peer_count"] == 2
    assert status["online_peers"] == 2
    print("  [PASS] 网络状态查询")


测试8: 多节点间消息传递模拟

函数 test_multi_node_communication() 在 tests/test_p2p_network.py: 测试8: 多节点间消息传递模拟

def test_multi_node_communication():
    """测试8: 多节点间消息传递模拟"""
    # 模拟3个独立节点
    node_a = SwarmNetwork(SwarmNode(node_id="queen-A", capabilities=["chat", "federation"]))
    node_b = SwarmNetwork(SwarmNode(node_id="queen-B", capabilities=["chat", "federation"]))
    node_c = SwarmNetwork(SwarmNode(node_id="queen-C", capabilities=["chat"]))

    # 相互注册
    node_a.register_peer(SwarmNode(node_id="queen-B", capabilities=["chat", "federation"]))
    node_a.register_peer(SwarmNode(node_id="queen-C", capabilities=["chat"]))
    node_b.register_peer(SwarmNode(node_id="queen-A", capabilities=["chat", "federation"]))
    node_b.register_peer(SwarmNode(node_id="queen-C", capabilities=["chat"]))

    # A→B 发送消息
    msg = node_a.create_message("chat", {"text": "虫后A向虫后B问好"}, target="queen-B")
    assert node_b.validate_message(msg) is True

    # B→A 回复
    reply = node_b.create_message("chat", {"text": "虫后B收到"}, target="queen-A")
    assert node_a.validate_message(reply) is True

    # A发起联邦学习(3个federation节点中A和B有federation能力)
    result = node_a.start_federation_round(min_participants=2)
    assert result["status"] == "started"
    assert "queen-A" in result["participants"]
    assert "queen-B" in result["participants"]
    print("  [PASS] 多节点间消息传递模拟")

if __name__ == "__main__":
    print("=" * 50)
    print("虫群P2P网络多节点通信验证")
    print("=" * 50)
    test_swarm_node()
    test_peer_registration()
    test_heartbeat_timeout()
    test_message_protocol()
    test_message_handler()
    test_federation_round()
    test_network_status()
    test_multi_node_communication()
    print("=" * 50)
    print("全部8/8测试通过!")


文件 tests/test_decode_diag.py: 诊断解码：为什么top_k=1却输出多token | 定义:

#!/usr/bin/env python3
"""诊断解码：为什么top_k=1却输出多token"""
import sys; sys.path.insert(0, '/home/admin/swarm')
import numpy as np
from core.neuro.chat_interface import ChatInterface

ci = ChatInterface.from_pickle('/home/admin/swarm/models/brain_templates/brain_v13.pkl')
tok = ci.tokenizer
emb = ci.embedding

# 1. tokenizer属性
ids = tok.encode("你好")
print(f"'你好' token ids: {ids}")
print(f"tokenizer attrs: {[a for a in dir(tok) if not a.startswith('_')]}")

# 2. decode
print(f"decode ids {ids}: '{tok.d


文件 tests/test_detailed_keys.py: 看think_detailed返回结构 | 定义:

#!/usr/bin/env python3
"""看think_detailed返回结构"""
import sys; sys.path.insert(0, '/home/admin/swarm')
import numpy as np
from core.neuro.chat_interface import ChatInterface

ci = ChatInterface.from_pickle('/home/admin/swarm/models/brain_templates/brain_v13.pkl')
vec = ci.embedding.forward(ci.tokenizer.encode('你好'))
d = ci.brain.think_detailed(vec)
print('keys:', list(d.keys()))
print('confidence:', d.get('confidence'))
if d.get('rounds'):
    r0 = d['rounds'][0]
    print('rounds[0] keys:', list(


文件 tests/test_v13_analysis.py: v13效果深度分析 | 定义:

#!/usr/bin/env python3
"""v13效果深度分析"""

import pickle
import numpy as np
import sys
sys.path.insert(0, '/home/admin/swarm')

from core.neuro.chat_interface import ChatInterface
from core.neuro.text_encoder import SimpleTokenizer, EmbeddingLayer

# 加载v13
chat = ChatInterface.from_pickle('models/brain_templates/brain_v13.pkl')
brain = chat.brain
tok = chat.tokenizer
emb = chat.embedding

print("=" * 60)
print("虫群v13 效果深度分析")
print("=" * 60)

# ===== 1. 感觉区分析 =====
print("\n【1. 感觉区 SensoryRegion】")


文件 tests/test_v13_fixed.py:  | 定义:

#!/usr/bin/env python3
import sys
sys.path.insert(0, '/home/admin/swarm')
from core.neuro.regions.thalamus_region import ThalamusRegion

t = ThalamusRegion(max_depth=10, min_depth=1)
for s in [0.05, 0.1, 0.3, 0.5, 0.8, 1.0]:
    print(f'strength={s:.2f} -> depth={t.compute_depth_budget(s)}')

print("\n=== BrainV13端到端 ===")
from core.neuro.brain_v13 import BrainV13
import numpy as np
brain = BrainV13(input_dim=32)
np.random.seed(42)

for text in ["你好", "hello", "Python"]:
    vec = np.random.rand


文件 tests/save_pkl_trained.py: 用训练好的SVD embedding重建v13 pkl
方法: 保存trained embedding+tokenizer到pkl，ChatInterface.from_pickle会加载它们 | 定义:

#!/usr/bin/env python3
"""
用训练好的SVD embedding重建v13 pkl
方法: 保存trained embedding+tokenizer到pkl，ChatInterface.from_pickle会加载它们
"""
import sys
sys.path.insert(0, '/home/admin/swarm')

import pickle
from core.neuro.brain_v13 import BrainV13
from core.neuro.chat_interface import ChatInterface

# 加载训练好的embedding+tokenizer
with open('/home/admin/swarm/models/brain_templates/embedding_trained.pkl', 'rb') as f:
    trained = pickle.load(f)

trained_embedding = trained['embedding']
trained_tokenizer = trai


文件 tests/verify_v7_1.py: 虫群v7.1增强功能验证 | 定义: def test, def hello

#!/usr/bin/env python3
"""虫群v7.1增强功能验证"""
import sys
sys.path.insert(0, "/home/admin/swarm")

passed = 0
failed = 0

def test(name, condition):
    global passed, failed
    if condition:
        print(f"  ✓ {name}")
        passed += 1
    else:
        print(f"  ✗ {name}")
        failed += 1

print("\n=== v7.1 增强功能验证 ===\n")

# === 1. AutoTrainer ===
print("=== 1. AutoTrainer ===")
from core.auto_trainer import AutoTrainer
at = AutoTrainer("test_user")
s = at.get_status()
test("AutoTrainer初始化