Hexswarm-代理协调协议
AI代理(Hex、Codex、Gemini)通过MCP与共享内存、上下文丰富和基于性能的路由进行通信。
建筑
┌─────────────────────────────────────────────────────────────────┐
│ HexMem (SQLite) │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────────────┐ │
│ │ lessons │ │ facts │ │ events │ │ performance │ │
│ └──────────┘ └──────────┘ └──────────┘ └──────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
▲ ▲ ▲
│ shared_memory.py │
└────────────────┼───────────────┘
│
┌──────────────┐ ┌────┴───────┐ ┌──────────────┐
│ Hex │ │ Context │ │ Smart │
│ (orchestrator)│◄───│ Builder │◄───│ Delegate │
├──────────────┤ └────────────┘ └──────────────┘
│ MCP Server │
│ │
│ MCP Client ──┼──────────────────────────────────────┐
└──────────────┘ │
│ │
▼ ▼
┌──────────────┐ ┌──────────────┐
│ Codex │ │ Gemini │
│ (coder) │ │ (researcher) │
├──────────────┤ ├──────────────┤
│ MCP Server │ │ MCP Server │
└──────────────┘ └──────────────┘快速开始
# Smart delegate (auto-selects best agent, enriches context)
/home/sat/bin/hexswarm/bin/smart-delegate.sh auto code "refactor the storage module"
# Shorthand wrapper
hs code "refactor the storage module"
hs research "find papers on LN routing"
# Task lifecycle visibility
hexswarm-status active # Pending/running tasks
hexswarm-status recent 10 # Last 10 tasks
hexswarm-status stats # Performance statistics
hexswarm-status agent codex # Tasks for specific agent
# Query swarm intelligence
/home/sat/bin/hexswarm/bin/swarm-intel.sh lessons code
/home/sat/bin/hexswarm/bin/swarm-intel.sh best research
/home/sat/bin/hexswarm/bin/swarm-intel.sh context "debug the routing issue"
# Direct MCP calls
mcporter call codex.agent_info
mcporter call hex.agent_performance action=get_stats agent_name=codex特性
上下文丰富
委派任务时,相关上下文会从HexMem自动注入:
- 课程我们在做类似的工作中学到了什么
- 事实:关于任务中主题的已知信息
- 事件:最近的相关活动
# Preview what context would be injected
/home/sat/bin/hexswarm/bin/swarm-intel.sh context "optimize channel fees"共享内存
代理人可以分享经验教训并查询集体知识:
# Share a lesson
mcporter call hex.agent_memory action=share_lesson agent_name=codex \
domain=code lesson="Always validate inputs" context="Found bug in API handler"
# Search lessons
mcporter call hex.agent_memory action=search_lessons query="validation"
# Get lessons by domain
mcporter call hex.agent_memory action=get_lessons domain=code性能跟踪
跟踪哪个代理在哪些任务类型上表现最佳:
# Record performance
mcporter call hex.agent_performance action=record \
agent_name=codex task_type=code success=true duration_seconds=45
# Get best agent for a task type
mcporter call hex.agent_performance action=best_for_task task_type=research
# View stats
mcporter call hex.agent_performance action=get_stats agent_name=codex智能路由
smart-delegate.sh auto 根据以下因素选择最佳代理:
- 历史性能数据(如有)
- 代理配置匹配技能(关键字、域)
- 按任务类型默认路由(代码→法典,研究→双子座)
代理映射技能
在中配置代理首选项 config/agent-skills.json:
{
"task_types": {
"code": {"preferred": "codex", "fallback": "gemini"},
"research": {"preferred": "gemini", "fallback": "codex"}
},
"domains": {
"hive": {"preferred": "codex"},
"lightning": {"preferred": "codex"},
"papers": {"preferred": "gemini"}
},
"keywords": {
"refactor": "codex",
"research": "gemini"
}
}任务生命周期跟踪
所有任务都在HexMem中跟踪(hexswarm_tasks 表):
# View active tasks
hexswarm-status active
# View recent tasks with results
hexswarm-status recent 20
# View statistics
hexswarm-status stats
# View tasks for specific agent
hexswarm-status agent codex任务状态: pending → running → completed | failed
异步通知
对于基于tmux的委托,代理人会致电 notify-done.sh 完成后:
# Check for completions
/home/sat/bin/hexswarm/bin/check-completions.shMCP工具(每个代理11个)
| 工具 | 说明 |
|---|---|
agent_info | 身份、能力、地位 |
agent_status | 可用性、当前任务、队列深度 |
submit_task | 提交工作(分块完成) |
task_status | 检查任务进度 |
task_result | 获取已完成的任务输出 |
cancel_task | 取消待处理/正在运行的任务 |
agent_resources | 上下文/令牌跟踪、容量 |
agent_memory | 分享/查询经验教训、事实、背景 |
agent_performance | 跟踪/查询任务执行情况 |
check_notifications | 检查异步完成 |
内存操作
agent_memory 支持以下操作:
| 动作 | 描述 |
|---|---|
log_event | 记录事件 |
share_fact | 记录事实(主谓宾语) |
get_context | 搜索相关上下文 |
record_handoff | 记录代理间的切换 |
share_lesson | 记录吸取的教训 |
get_lessons | 按领域获取课程 |
search_lessons | 按关键字搜索课程 |
get_agent_lessons | 通过特定代理获取课程 |
文件结构
/home/sat/bin/hexswarm/
├── agent_mcp/
│ ├── __init__.py
│ ├── auth.py # DID verification (stub)
│ ├── context_builder.py # HexMem context enrichment
│ ├── notifications.py # Async completion notifications
│ ├── protocol.py # Task types and schemas
│ ├── resources.py # Token/context tracking
│ ├── server.py # Base MCP server
│ ├── shared_memory.py # HexMem integration
│ └── storage.py # Task persistence
├── servers/
│ ├── codex_server.py
│ ├── gemini_server.py
│ └── hex_server.py
├── bin/
│ ├── smart-delegate.sh # Context-enriched delegation
│ ├── swarm-intel.sh # Query swarm knowledge
│ ├── notify-done.sh # Completion notifications
│ └── check-completions.sh # Check for notifications
└── README.md任务存储器
~/.agent/
├── codex/tasks/{pending,running,completed,failed}/
├── gemini/tasks/{pending,running,completed,failed}/
├── hex/tasks/{pending,running,completed,failed}/
└── notifications/{pending,processed}/代理商DIDs
| 代理 | DID |
|---|---|
| 六角形 | did:cid:bagaaieratn3qejd6mr4y2bk3nliriafoyeftt74tkl7il6bbvakfdupahkla |
| 食品法典委员会 | did:cid:bagaaierawhtwebyik523xjzhmxgfonrw56ssimutvr2surw3iypvgpjzehoa |
| 双子座 | did:cid:bagaaieraafcrruni2vrpp4nmzhwpe2vnjjjuxj2lb5cew76jixjuil7fxoqa |
配置
在中注册的服务器 ~/.mcporter/mcporter.json:
{
"mcpServers": {
"codex": {
"type": "stdio",
"command": "/home/sat/bin/hexswarm/bin/run-codex-server.sh",
"env": {
"CODEX_TASK_DIR": "/home/sat/.agent/codex/tasks",
"CODEX_WORKDIR": "/home/sat/clawd"
}
}
}
}生态系统
Hexswarm是集成代理自治堆栈的一部分:
┌─────────────────────────────────────────────────────────────┐
│ Agent Autonomy Stack │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌──────────┐ delegates ┌──────────┐ falls back to │
│ │ hexswarm │──────────────▶│ hexmux │───────────────────▶│
│ │ (MCP) │ │ (tmux) │ │
│ └────┬─────┘ └──────────┘ │
│ │ │
│ │ reads/writes │
│ ▼ │
│ ┌──────────┐ │
│ │ hexmem │◀── structured memory (lessons, facts, events) │
│ │ (SQLite) │ │
│ └────┬─────┘ │
│ │ │
│ │ backups, signing │
│ ▼ │
│ ┌──────────┐ │
│ │ archon │◀── decentralized identity (DIDs, vaults) │
│ │ (DID) │ │
│ └──────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘组件角色
| 组件 | 目的 | GitHub |
|---|---|---|
| hexswarm | 通过MCP进行代理协调。上下文丰富、性能路由、共享内存。 | hexdemon/hexswarm |
| hexmux | Tmux编排回退。适用于需要写访问权限或MCP不可用时的代理。 | hexdemon/hexmux |
| hexmem | 结构化存储器基板。身份、教训、事实、事件。语义搜索。 | hexdemon/hexmem |
| 阿贡技能 | 去中心化身份操作。DID、凭据、保险库备份。 | 原型技术/代理技能 |
数据流
- 授权:通过hexswarm MCP执行十六进制任务→ 如果需要写入,则回退到hexmux(tmux)
- 上下文:在授权之前,hexswarm从hexmem那里吸取了相关的经验教训/事实/事件
- 学习:特工通过以下方式将课程记录到hexmem
agent_memory工具 - 身份:每个代理都有一个Archon DID,用于未来的加密身份验证
- 备份:hexmem备份到Archon vault以实现去中心化持久性
