乐于分享
好东西不私藏

Harness 实现:Hermes 源码分解

Harness 实现:Hermes 源码分解

OpenClaw 的主线是 Gateway:多 channel、session routing、远程入口和回传。Hermes 的主线完全不同。它最值得研究的地方不是“能调多少工具”,而是它如何把 agent 的经验沉淀成 memory、skills、session search 和后续行为改变。

Hermes README 的定位非常直接:self-improving AI agent,内置 closed learning loop;它会用 agent-curated memory 保存知识,用 periodic nudges 促使自己持久化信息,在复杂任务后自动创建 skills,在使用中改进 skills,并用 FTS5 session search + LLM summarization 做跨会话召回。[1]

这使 Hermes 成为五个样本里最适合讨论 Context/State/Memory 和 Feedback/Evaluation/Co-design 的案例。

组件结构:五个子系统在 Hermes 里的落点

Hermes 的源码结构比“有 memory 工具”更进一步:run_agent.py 的 AIAgent 拥有主 loop、context compression、delegate dispatch、session DB 写入和 background review 入口;hermes_state.py 用 SQLite + FTS5 存 session/message;agent/memory_manager.py 和 tools/memory_tool.py 管长期记忆;tools/session_search_tool.py 把历史会话检索做成 loop tool;agent/background_review.py 把任务后的经验沉淀成 memory/skill 更新。

子系统
源码里的实现组件
运行流程
和其他项目的差异
Agent Loop & Orchestration
AIAgent
 外观、conversation loop、delegate_task、gateway/cron trigger、background review;相关文件:run_agent.pyagent/conversation_loop.pyagent/agent_runtime_helpers.pytools/delegate_tool.pytools/async_delegation.pygateway/run.pycron/scheduler.py
CLI/Gateway/Cron 创建或恢复 session,AIAgent.run_conversation() 组装 context 并循环调用模型和工具;顶层 delegate_task 默认异步后台运行,完成后经 completion queue 回流成后续 turn;主任务结束后可触发后台复盘。
loop 不是一次性回答器,而是长期状态更新器;子任务结果不强塞进当前 tool loop。
Context, State & Memory
SQLite session DB、FTS5、ContextCompressor、MemoryManager、MEMORY/USER、skills、session_search;相关文件:hermes_state.pyagent/context_compressor.pyagent/memory_manager.pytools/memory_tool.pytools/session_search_tool.py
当前消息、session history、压缩摘要、memory/provider、skills 和 session_search 结果共同进入下一轮 context;压缩后的旧消息仍保留在 DB/FTS。源码中 session_search 是零 LLM 调用的 FTS5 窗口检索,和 README 的“LLM summarization”表述不完全一致。
记忆是主轴,且 memory/session/skills 都能反向改变后续 prompt。
Tool Use, Runtime & MCP
tool registry、model_tools.py、deferred tool bridge、MCP client、approval guard、多 terminal backend;相关文件:model_tools.pytools/registry.pytools/tool_search.pytools/mcp_tool.pyhermes_cli/mcp_startup.pytools/terminal_tool.pytools/environments/*
get_tool_definitions()
 根据 enabled/disabled toolsets 暴露工具;大量工具可折叠成 tool_search/tool_describe/tool_call;MCP server 发现后注册成 mcp-* toolset;terminal 类动作再落到 local/Docker/SSH/Singularity/Modal/Daytona。
把 memorysession_searchdelegate_task 也放进 agent loop tools,MCP 不走旁路。
Interface & Product Surface
CLI、Messaging Gateway、cron、TUI gateway;相关文件:hermes_cli/gateway.pygateway/run.pytui_gateway/server.pycron/scheduler.py
多入口都进入同一类 agent/session 流程;Gateway 负责 platform delivery,cron 会在 prompt 前注入 scheduled-job guidance 并把结果投递到目标平台。
Interface 服务长期个人 agent,而不是 OpenClaw 那种纯 Gateway 控制面。
Feedback & Engineering
background review、skill creation/improvement、curator、trajectory、observability、insights;相关文件:agent/background_review.pytools/skill_manager_tool.pytools/skill_usage.pyagent/curator.pyagent/trajectory.pyagent/insights.pydocs/observability/README.md
完成任务后,后台 review 抽取经验,写入 memory 或 skill;skill usage 和 curator 维护技能生命周期;hook/trajectory/insights 输出可观测数据。
feedback 是产品主循环的一部分,不只是观测数据。
Secure Runtime / Sandbox
command approval、多 backend runtime、prompt/secret/path guard、OSV/MCP supply-chain guard;相关文件:SECURITY.mdtools/approval.pytools/environments/*agent/file_safety.pytools/path_security.pytools/tirith_security.pyagent/secret_scope.pytools/osv_check.py
本地执行默认依赖宿主;强隔离通过 Docker/SSH/Modal/Daytona 等 backend;approval/redaction 是防御层,源码文档明确真正边界来自 sandbox/runtime 隔离。
安全边界是可选运行后端组合,不像 Codex 那样进入主叙事。

1. 定位:memory-first / self-improving harness

Hermes 是 memory-first / self-improving 的 agent harness

它不是纯 coding CLI,也不是纯聊天机器人。README 同时强调三个方向:

  1. built-in learning loop;
  2. messaging gateway;
  3. 多 terminal backend:local、Docker、SSH、Singularity、Modal、Daytona。[1]

这说明 Hermes 的目标不是“让模型完成一次任务”,而是让 agent 逐渐形成对用户、任务和环境的持续理解。它把长期状态做成产品特征,而不是附加插件。

用第一篇的五个子系统看,Hermes 的重心排序大概是:

  1. Context, State & Memory;
  2. Feedback, Evaluation & Co-design;
  3. Tool Use, Runtime & MCP;
  4. Agent Loop & Orchestration;
  5. Interface & Product Surface。

2. Agent Loop & Orchestration

Hermes 的核心 loop 在 run_agent.py 的 AIAgent。现有源码显示它管理 conversation flow、tool execution、context compression、memory provider lifecycle、delegate_task、background review 等能力。

和最小 ReAct loop 相比,Hermes loop 至少多了四类编排:

  1. 工具调用循环
    :模型选择工具,model_tools.py 分发工具,结果回填 messages。
  2. delegate/subagent
    delegate_task 允许把子任务分派给 child agents,并限制并发子任务数量。
  3. gateway/cron runner
    :Gateway 和定时任务能触发 agent 工作,说明 loop 不只来自 CLI 输入。
  4. background review
    :主 turn 完成后,可以 spawn memory/skill review thread,把经验沉淀到后续状态。

Anthropic 的《Building effective agents》把 agent 描述为根据环境反馈循环使用工具的系统,并讨论 orchestrator-workers、evaluator-optimizer 等模式。[2] Hermes 的 delegate 和 background review 正好对应这两类方向:一个负责分工,一个负责改进。

所以 Hermes 的 loop 不是简单的 while tool_call。它更像一个长任务控制器:主任务、子任务、压缩、记忆同步、后台复盘都挂在同一条 agent 生命周期上。

3. Context, State & Memory

这是 Hermes 最核心的一层。

Hermes README 明确把 memory、user profile、skills、session search 放进 closed learning loop。[1] 源码里的 hermes_state.py 是 SQLite-backed session storage,带 FTS5 full-text search;agent/context_compressor.py 处理上下文压缩;agent/memory_manager.pymemory_provider.pycontext_engine.py 处理长期记忆和外部 memory provider;tools/memory_tool.py 提供 MEMORY.md / USER.md 类型的持久化记忆。

可以把 Hermes 的 context 分成五层:

  1. 当前 turn
    :用户输入、工具结果、模型输出。
  2. session state
    :SQLite session、messages、branch/delegate 关系、FTS5 search。
  3. 静态记忆
    MEMORY.mdUSER.md、用户画像、项目事实。
  4. skills
    :从经验中生成或由用户维护的 procedural memory。
  5. 外部 memory providers
    :Honcho、Mem0、Holographic 等外部记忆后端。

这和 Claude Code 的 CLAUDE.md 或 Codex 的 AGENTS.md 不一样。Claude/Codex 更强调项目规则如何稳定注入;Hermes 更强调跨 session 的经验如何被召回和改写。

这里的风险也更大:错误记忆、过期偏好、失败 skill、污染的 session summary,都可能在后续任务中被放大。Hermes 这类 harness 的难点不是“有没有 memory”,而是 memory 如何审计、回滚、降权、过期和分层。

4. Tool Use, Runtime & MCP

Hermes 的工具层也很厚。README 列出 40+ tools、toolsets、terminal backends、Tool Gateway、MCP Integration。[1]model_tools.py 中可以看到 _AGENT_LOOP_TOOLS = {"todo", "memory", "session_search", "delegate_task"},并有 MCP/plugin deferred tools、tool search/deferred bridge、approval guard 等结构。

Hermes 工具层有两个特点。

第一,它把 memory 和 session search 也做成 agent loop tools。这意味着模型不仅能执行外部动作,还能主动检索自己的历史和记忆。

第二,它支持多 terminal backend:local、Docker、SSH、Singularity、Modal、Daytona。[1] 这说明 Hermes 的 tool runtime 不绑定一台本地机器。它更像一个可迁移的长期 agent,可以在 VPS、GPU 集群或 serverless 环境里工作。

MCP 在 Hermes 中属于工具扩展层,而不是完整 harness。MCP 官方规范提供 Resources、Prompts、Tools 等 server features,解决的是外部系统连接问题。[3] Hermes 还必须自己负责 loop、context、memory、feedback 和 gateway。

5. Interface & Product Surface

Hermes 的 interface 不是单个 TUI。README 列出 CLI 和 Messaging Gateway,支持 Telegram、Discord、Slack、WhatsApp、Signal、Home Assistant 等入口。[1]

这让 Hermes 更像一个长期个人 agent:

  1. 用户可以从聊天工具给它任务;
  2. 它可以在云端或远程环境继续执行;
  3. session 和 memory 跨入口保留;
  4. voice memo、cron、gateway delivery 可以把 agent 从“终端应用”推向“常驻助手”。

和 OpenClaw 相比,Hermes 的 Gateway 不是第一主角。OpenClaw 研究的是多 channel control plane;Hermes 研究的是长期状态如何穿过这些入口保持一致。

所以 Hermes 的 interface 价值在于:它给长期记忆体提供了多个输入输出通道。

6. Feedback, Evaluation & Co-design

Hermes 把 feedback 做成了产品名片。

README 里的 closed learning loop 包括 agent-curated memory、skill auto creation、skill self-improvement、session search。[1] 源码中 run_agent.py 的 _spawn_background_review 会把 memory/skill review 从主 turn 中分离出去;agent/background_review.py 负责相关后台复盘;agent/curator_backup.py 等模块则显示它意识到经验资产需要备份和管理。

这和 Reflexion 论文的方向很接近:Reflexion 用 verbal feedback 和 episodic memory 让 agent 从 trial-and-error 中改进。[4] OpenAI Cookbook 的 agent improvement loop 也把 traces、feedback、evals 和 optimizer 组织成持续改进飞轮。[5]

Hermes 的特殊之处在于,它不是把 feedback 留给离线 eval,而是把它产品化到日常使用里:

  1. 当前任务完成;
  2. 后台 review 抽取经验;
  3. 经验写入 memory 或 skill;
  4. 后续任务按需召回;
  5. skill 在使用中继续改进。

这就是 model-harness co-design 的低配版本:不一定马上训练模型,但 harness 已经在用运行数据改变下一轮行为。

7. 两个边界支撑

Secure Runtime / Sandbox:Hermes 的执行边界不是单一 OS sandbox,而是多后端 runtime 选择。README 提到 command approval、DM pairing、container isolation,以及 local、Docker、SSH、Singularity、Modal、Daytona 六种 terminal backend。[1]docs/security/network-egress-isolation.md 也提醒:如果使用默认 local backend,网络隔离要依赖宿主环境;更强隔离需要 Docker、Modal、Daytona 等 backend。

Engineering Harness:Hermes 的工程环境包括 docs、observability、batch trajectory、skill hub、migration from OpenClaw、gateway/cron、memory provider 插件。它的工程问题也更复杂:skill 污染、错误 memory、回滚、审计、外部 memory provider 的数据边界,都必须成为工程 harness 的一部分。

8. 结构判断

Hermes 最值得研究的是:它如何把“经验”变成 harness 资产。

一般 coding agent 的状态主要在当前 session;Hermes 试图把跨 session 的用户画像、任务经验、工具流程和技能沉淀下来。这样做的收益是 agent 越用越熟悉用户;代价是长期状态会变成新的复杂性来源。

所以拆 Hermes 时,最重要的问题不是“它有哪些工具”,而是:

它如何决定什么值得记住,如何避免错误经验固化,如何让 memory 和 skills 在未来任务中被正确召回?

9. 参考资料

[1] NousResearch, Hermes Agent GitHub README.https://github.com/NousResearch/hermes-agent

[2] Anthropic Engineering, Building effective agents, 2024-12-19.https://www.anthropic.com/engineering/building-effective-agents

[3] Model Context Protocol specification, 2025-06-18.https://modelcontextprotocol.io/specification/2025-06-18

[4] Shinn et al., Reflexion: Language Agents with Verbal Reinforcement Learning, arXiv, 2023.https://arxiv.org/abs/2303.11366

[5] OpenAI Cookbook, Agent improvement loop.https://developers.openai.com/cookbook/examples/agents_sdk/agent_improvement_loop