用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/ybbms777/openclaw-claude-code-integration --skill knowledge-federation命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
基于 SOC 职业分类
正在显示 SKILL.md
| name | knowledge-federation |
| description | 知识共享框架。多Agent跨项目学习机制,规则版本管理,社群排行榜,冲突协调(本地优先/社群优先/合并/版本管理),实现OpenClaw系统的分布式演化。 |
| model | minimax-portal/MiniMax-M2.7 |
| effort | high |
将单个Agent的学习经验扩展到整个OpenClaw社群,实现跨项目、跨Agent的规则共享、版本管理、冲突协调,形成一个自主进化的分布式学习网络。
每个Agent维护独立的本地规则库,持久化到 .local-rules/ 目录:
registry = LocalRuleRegistry(workspace_dir)
# 注册新规则
version = registry.register_rule(
rule_id="check_funds_transfer",
content={"condition": "amount > 1000", "require_approval": True},
effectiveness=85.0, # 初始效能评分
description="初始版本"
)
# 检索规则
rule = registry.get_rule("check_funds_transfer")
# 列出所有本地规则
all_rules = registry.list_rules()
存储结构:
.local-rules/{rule_id}_{version_id}.json每条规则都有完整的版本链,支持演化追踪:
@dataclass
class RuleVersion:
version_id: str # 版本ID(8字符UUID)
rule_id: str # 规则ID
parent_version: Optional[str] # 父版本 (用于追踪演化)
author_agent: str # 作者Agent ID
timestamp: str # 创建时间
content: Dict # 规则实际内容
effectiveness_score: float # 效能评分 (0-100)
status: str # draft / published / deprecated
tags: List[str] # 标签 (security, finance, common, etc.)
description: str # 版本说明
breaking_changes: List[str] # 破坏性改动说明
版本生命周期:
draft (草稿)
└→ 编辑、测试
└→ published (已发布)
├→ 保持活跃(高效能)
├→ deprecated (已废弃)
└→ replaced (被取代)
多Agent更新同一规则时的智能冲突处理:
# 检测冲突
conflict = ConflictResolver.detect_conflicts(local_rule, community_rule)
# 解决冲突(4种策略)
resolver = ConflictResolver()
resolved = resolver.resolve_conflict(conflict)
4种冲突解决策略:
| 策略 | 说明 | 使用场景 |
|---|---|---|
LOCAL_PRIORITY | 保留本地版本 | 项目特定规则,本地自定义 |
COMMUNITY_PRIORITY | 使用社群版本 | 社群规则更优且更新 |
MERGE | 合并两个版本 | 互补的改进可以合并 |
VERSION | 比较效能选择 | 让数据决策(高效能优先) |
冲突解决示例:
本地规则 (v1):
- 内容: {"timeout": 10s, "retry": 3}
- 效能: 75分
社群规则 (v2):
- 内容: {"timeout": 5s, "retry": 5, "cache": true}
- 效能: 88分
策略: VERSION 或 COMMUNITY_PRIORITY
↓
选择: v2 (效能88 > 75)
↓
结果: 新主规则已更新,v1保存为历史版本
实时追踪所有规则的效能排名、采纳人数、演化历史:
leaderboard = CommunityLeaderboard()
# 添加规则
leaderboard.add_rule(community_rule)
# 更新效能得分
leaderboard.update_effectiveness("rule_123", 82.5)
# 记录被采纳
leaderboard.record_adoption("rule_123")
# 获取排行前10
top_10 = leaderboard.get_top_rules(limit=10)
# 输出排行榜
board = leaderboard.get_leaderboard()
# [
# {"position": 1, "rule_id": "rule_123", "score": 88.5, "adoption_count": 15},
# {"position": 2, "rule_id": "rule_456", "score": 85.2, "adoption_count": 12},
# ...
# ]
排行榜特性:
核心编排层,整合上述所有功能:
fed = KnowledgeFederation(
workspace_dir="~/.openclaw/workspace",
central_api="http://localhost:8000" # 可选:中央知识库API
)
# 1. 发布规则到社群
version_id = fed.publish_rule(
rule_id="security_check",
content={"verify_signature": True},
effectiveness=88.5,
tags=["security", "critical"]
)
# 2. 订阅社群规则(可选过滤)
community_rules = fed.subscribe_community_rules(
filters={
"min_score": 75,
"tags": ["security"],
"project": "finance"
}
)
# 3. 集成社群规则(自动处理冲突)
result = fed.integrate_community_rule(community_rule)
# 4. 获取规则演化历史
genealogy = fed.get_rule_genealogy("rule_id")
# 返回按时间排序的版本链
# 5. 查看系统统计
stats = fed.get_statistics()
# {
# "agent_id": "agent_xyz",
# "project_id": "openclaw",
# "local_rules_count": 42,
# "community_rules_count": 156,
# "conflicts_detected": 3,
# "leaderboard_top_10": [...]
# }
from skills.knowledge_federation.scripts.knowledge_federation import KnowledgeFederation
# 初始化
fed = KnowledgeFederation()
# 场景1:发布高效规则到社群
new_rule = fed.publish_rule(
rule_id="avoid_typo_in_model_field",
content={
"checker": "regex_pattern",
"pattern": r"^(claude|gpt|mistral)",
"action": "warn_user"
},
effectiveness=92.5,
tags=["content-safety", "model-names"]
)
print(f"规则已发布: {new_rule}")
# 场景2:订阅并集成社群规则
community_rules = fed.subscribe_community_rules(
filters={"min_score": 80, "tags": ["security"]}
)
for rule in community_rules[:5]:
try:
integrated = fed.integrate_community_rule(rule)
print(f"✅ 已集成: {rule.rule_id} (效能: {integrated.effectiveness_score})")
except Exception as e:
print(f"⚠️ 集成失败: {e}")
# 场景3:分析规则演化
genealogy = fed.get_rule_genealogy("my_critical_rule")
print(f"规则演化链: {len(genealogy)} 个版本")
for i, version in enumerate(genealogy):
print(f" v{i}: @ ")
# 发布规则
python3 knowledge_federation.py publish my_rule \
--content '{"action":"check"}' \
--effectiveness 85.0 \
--tags security critical
# 订阅规则
python3 knowledge_federation.py subscribe \
--min-score 75 \
--tags security finance \
--workspace ~/.openclaw/workspace
# 查看统计
python3 knowledge_federation.py stats --workspace ~/.openclaw/workspace
from skills.rule_optimizer.scripts.rule_optimizer import RuleOptimizer
from skills.knowledge_federation.scripts.knowledge_federation import KnowledgeFederation
# 自动循环:优化 → 社群共享
def evolve_rules():
optimizer = RuleOptimizer()
fed = KnowledgeFederation()
# 1. 找出高效规则
for rule_id in [r.rule_id for r in fed.local_registry.list_rules()]:
metrics = optimizer.evaluate_rule_effectiveness(rule_id)
# 2. 效能优秀?发布到社群
if metrics.effectiveness_score > 85:
fed.publish_rule(
rule_id=rule_id,
content=metrics.rule_content,
effectiveness=metrics.effectiveness_score,
tags=metrics.tags
)
print(f"✅ 发布高效规则: {rule_id}")
# 3. 效能低?寻求社群改进
elif metrics.effectiveness_score < 50:
community_versions = fed.subscribe_community_rules({
"min_score": 75,
"tags": metrics.tags
})
if community_versions:
best = sorted(community_versions,
key=lambda r: r.leaderboard_score)[-1]
fed.integrate_community_rule(best)
print(f"⬆️ 更新规则 {rule_id} 为社群版本")
from skills.behavior_analyzer.scripts.behavior_analyzer import SessionBehaviorAnalyzer
from skills.knowledge_federation.scripts.knowledge_federation import KnowledgeFederation
# 异常行为 → 禁用社群规则,回到本地基线
analyzer = SessionBehaviorAnalyzer()
fed = KnowledgeFederation()
session_health = analyzer.analyze_session("session_123")
if session_health["health_score"] < 40:
# 会话异常,撤销社群规则,使用本地稳定版本
for rule in fed.local_registry.list_rules():
if rule.status == "published":
print(f"回退规则: {rule.rule_id} (会话异常)")
class RuleSource(Enum):
LOCAL = "local" # 本地生成
COMMUNITY = "community" # 社群共享
VERIFIED = "verified" # 已验证通过
class ConflictResolution(Enum):
LOCAL_PRIORITY = "local_priority"
COMMUNITY_PRIORITY = "community_priority"
MERGE = "merge"
VERSION = "version"
@dataclass
class RuleConflict:
conflict_id: str # 冲突ID
local_rule: RuleVersion # 本地版本
community_rule: RuleVersion # 社群版本
detected_at: str # 检测时间
resolution_strategy: ConflictResolution # 解决策略
resolution_result: Optional[RuleVersion] # 解决后的版本
user_decision: Optional[str] # 用户决策(可选)
@dataclass
class CommunityRule:
rule_id: str # 规则ID
versions: List[RuleVersion] # 版本链
effectiveness_history: List[Tuple[str, float]] # 效能历史
adoption_count: int # 采纳数
project_tags: Set[str] # 项目标签
leaderboard_position: Optional[int] # 排行位置
leaderboard_score: float # 排行分数
.local-rules/)// .local-rules/check_funds_v1a2b3c4d.json
{
"version_id": "v1a2b3c4d",
"rule_id": "check_funds",
"parent_version": null,
"author_agent": "agent_production_01",
"timestamp": "2026-04-11T10:30:00",
"content": {"threshold": 1000, "require_approval": true},
"effectiveness_score": 87.5,
"status": "published",
"tags": ["finance", "critical"],
"description": "Initial version, requires approval for large transfers",
"breaking_changes"
.federation-log.jsonl){"timestamp": "2026-04-11T10:30:00", "agent_id": "agent_1", "project_id": "default", "rule_id": "check_funds", "version_id": "v1a2b3c4d", "effectiveness": 87.5, "tags": ["finance", "critical"]}
.conflict-log.jsonl){"timestamp": "2026-04-11T10:35:00", "conflict_id": "c_xyz", "local_rule_id": "v123", "community_rule_id": "v456", "resolution": "community_priority"}
1. 规则执行反馈记录 (via rule_optimizer)
optimizer.record_rule_application("check_funds", fixed=True, satisfaction=4.8)
2. 评估效能
metrics = optimizer.evaluate_rule_effectiveness("check_funds")
→ effectiveness_score = 91.2 (优秀)
3. 发布到社群
fed.publish_rule("check_funds", {...}, 91.2, ["finance", "verified"])
4. 社群排行榜
leaderboard_position = 2
adoption_count = 24 (24个Agent采纳)
5. 继续监控
新的反馈 → 效能更新 → 排行调整
1. Agent A 本地规则
"timeout": 10s, "retry": 3
效能: 65 分
2. 社群规则(更新)
"timeout": 5s, "retry": 5, "cache": true
效能: 88 分
3. 检测冲突
ConflictResolver.detect_conflicts(local, community) → True
4. 选择解决策略
ConflictResolution.VERSION (让数据说话)
5. 自动选择
88 > 65 → 使用社群版本
6. 更新与记录
fed.integrate_community_rule(community_rule)
→ 新版本成为主规则,旧版本保存为历史
1. 会话行为分析
behavior_analyzer.analyze_session("s_123")
→ health_score = 35 (异常)
2. 检测异常
anomaly_patterns = ["repeated_errors", "role_drift"]
3. 紧急措施
禁用最近集成的社群规则
恢复到 local_registry 的稳定版本
4. 通知用户
"检测到异常行为,已降级为保守模式"
5. 恢复后升级
继续监控 health_score
health_score > 60 → 逐步恢复高效能规则
session:end — 上报本会话生成的新规则rule:published — 中央库接收规则发布rule:adopted — 其他Agent采纳此规则federation:conflict_detected — 触发冲突解决pytest skills/knowledge-federation/tests/ -v
# 29/29 通过
测试覆盖:
原因:多Agent频繁编辑同一规则,或冲突解决策略选择错误 解决:
.conflict-log.jsonl 的冲突历史resolution_strategy(建议使用 VERSION)project_tags 以隔离作用域原因:初始采纳少、反馈数据不足 解决:
adoption_count 阈值,减少噪声effectiveness_history 是否在增长原因:parent_version 指向的文件被误删或迁移 解决:
.local-rules/ 目录完整性get_rule_genealogy() 诊断断链位置遵循项目主许可证。