一个会"自己长脑子"的AI助手:Hermes Agent 自进化机制解析
关于 Hermes 这个名字
Hermes(赫尔墨斯),希腊神话中的信使之神。不过 Hermes Agent 这个开源项目的定位倒是有点”奢侈品”的意味——它会自己进化、越用越强,不像那些每次都从零开始的工具。

如果你用 AI 助手,每次对话都是独立的——它不记得你上次怎么解决的这个问题,也不会把成功的经验积累下来下次用。这是大多数 AI 助手的通病。
但 Hermes Agent 不一样。它的宣传语是”The agent that grows with you”——一个会自己长脑子的助手。
这不只是噱头。我花了几天时间把它的源代码读了一遍,发现它的自进化机制确实有东西。今天就来从工程角度讲讲它是怎么实现的。
技能自更新:让成功的经验变成可复用技能
触发:不靠事件判断,靠节拍器
大多数框架设计”自进化”时,容易掉进的坑是:设计一堆复杂的事件判断逻辑——”如果任务超过 5 步就创建技能”、”如果用户纠正了就记录”。
Hermes 的做法简单得多:计数器。
# run_agent.py 第 8519-8525 行
# Track tool-calling iterations for skill nudge.
# Counter resets whenever skill_manage is actually used.
if (self._skill_nudge_interval > 0
and "skill_manage" in self.valid_tool_names):
self._iters_since_skill += 1
默认每 10 次工具调用触发一次技能审查。这个数字可以通过配置调整:
# run_agent.py 第 1336-1340 行
self._skill_nudge_interval = 10
skills_config = _agent_cfg.get("skills", {})
self._skill_nudge_interval = int(skills_config.get("creation_nudge_interval", 10))
为什么用计数器而不是事件判断?因为行为可预测、易调试。你永远知道什么时候会触发,不会出现”这个任务到底算不算复杂”的模糊地带。
决策:让一个独立的 AI 来判断
触发之后,Hermes 不是直接创建技能,而是启动一个后台 Review Agent:
# run_agent.py 第 2313-2340 行
def _spawn_background_review(self, messages_snapshot, review_memory=False, review_skills=False):
"""Spawn a background thread to review the conversation for memory/skill saves.
Creates a full AIAgent fork with the same model, tools, and context as the
main session. Writes directly to the shared memory/skill stores.
Never modifies the main conversation history or produces user-visible output.
"""
import threading
def _run_review():
review_agent = AIAgent(model=self.model, max_iterations=8, quiet_mode=True, ...)
review_agent.run_conversation(user_message=prompt, conversation_history=messages_snapshot)
t = threading.Thread(target=_run_review)
t.start()
Review Agent 收到的指令是这样的:
# run_agent.py 第 2289-2310 行
_SKILL_REVIEW_PROMPT = (
"Review the conversation above and consider saving or updating a skill if appropriate.\n\n"
"Focus on: was a non-trivial approach used to complete a task that required trial "
"and error, or changing course due to experiential findings along the way?\n\n"
"If a relevant skill already exists, update it with what you learned. "
"Otherwise, create a new skill if the approach is reusable.\n"
"If nothing is worth saving, just say 'Nothing to save.' and stop."
)
核心思想是:不是代码判断该不该创建技能,是让一个 AI 来判断。 Review Agent 会看对话历史,自己决定有没有值得保存的工作流。
执行:skill_manage 工具
如果 Review Agent 判断值得创建,就会调用 skill_manage 工具:
# tools/skill_manager_tool.py
# 支持的操作
skill_manage(action="create", name="deploy-k8s", content="...") # 创建
skill_manage(action="patch", name="deploy-k8s", old_string="...", new_string="...") # 增量更新
skill_manage(action="edit", name="deploy-k8s", content="...") # 全量替换
skill_manage(action="delete", name="old-skill") # 删除
技能文件保存在 ~/.hermes/skills/,目录结构:
~/.hermes/skills/
├── deploy-k8s/
│ ├── SKILL.md # 主技能文件
│ ├── references/ # 参考文档
│ ├── templates/ # 模板
│ └── scripts/ # 辅助脚本
└── category-name/
└── another-skill/
└── SKILL.md
安全:不因为是 AI 生成就放行
这是我觉得设计得最好的地方:Agent 自己创建的技能,和第三方安装的技能,安全标准完全一样。
# tools/skill_manager_tool.py 第 50-72 行
def _security_scan_skill(skill_dir):
"""Scan a skill directory after write. Returns error string if blocked."""
result = scan_skill(skill_dir, source="agent-created")
allowed, reason = should_allow_install(result)
if allowed is False:
return f"Security scan blocked this skill ({reason})"
# "ask" verdict 对 Agent 创建的技能意味着直接 block
if allowed is None:
logger.warning("Agent-created skill blocked (dangerous findings)")
return f"Security scan blocked this skill ({reason})"
记忆机制:跨会话记住重要的事
两个存储,两个用途
Hermes 的记忆系统分两层:
| 存储 | 用途 | 字符限制 |
|---|---|---|
| MEMORY.md | Agent 的环境笔记、约定、学到的教训 | 2,200 |
| USER.md | 用户偏好、沟通风格、身份 | 1,375 |
这是一个刻意的设计:容量小不是缺陷,是优点。 限制越小,Agent 越需要提炼真正重要的信息,而不是把什么都往里塞。
Frozen Snapshot 模式
Session 开始时,记忆文件从磁盘加载,注入 system prompt。但注意这个细节:
# run_agent.py 注释
# For continuing sessions, we load the stored system prompt from the session DB
# instead of rebuilding. Rebuilding would pick up memory changes from disk that
# the model already knows about (it wrote them!), producing a different system
# prompt and breaking the Anthropic prefix cache.
简单说:一次加载,本 Session 内不变化。 这样做是为了保护 LLM 的 prefix cache,避免缓存失效导致重复计算。
容量满了怎么办
不是自动删除,是报错让 Agent 主动处理:
{
"success": false,
"error": "Memory at 2,100/2,200 chars. Adding this entry (250 chars) would exceed the limit.",
"usage": "2,100/2,200"
}
Agent 必须先合并或删除旧条目,才能添加新内容。这比自动清理更安全——不会无意识丢失重要信息。
数据不丢失
# 顺序很重要:先 flush 记忆,再压缩
# Memory is flushed to disk first (preventing data loss)
# then compress context
每次 turn 结束,先把记忆写到磁盘,再做上下文压缩。即使压缩崩溃,记忆也已经安全。
工程稳定性的几个关键设计
计数器跨 Session 持久
# run_agent.py 第 8206-8208 行
# NOTE: _turns_since_memory and _iters_since_skill are NOT reset here.
# They are initialized in __init__ and must persist across run_conversation calls
# so that nudge logic accumulates correctly in CLI mode.
这很重要。如果每次 /reset 都重置,永远达不到触发阈值。持久化保证了即使频繁重置会话,学习效果也能累积。
后台线程隔离
# 主 Agent 不阻塞,用户无感知延迟
t = threading.Thread(target=_run_review)
t.start()
# 用户立即收到响应
# Review 在后台静默运行
Review Agent 和主 Agent 是完全隔离的:独立线程、独立迭代预算(max 8次)、静默模式。用户不会感受到任何延迟。
Review 结果汇总通知
Review 完成后,不是静默结束,而是通过 callback 通知用户:
# run_agent.py ~2370-2395 行
actions = []
for msg in review_agent._session_messages:
if msg.get("role") != "tool":
continue
data = json.loads(msg.get("content", "{}"))
if not data.get("success"):
continue
if "created" in message.lower():
actions.append(message)
# ...
if actions:
summary = " · ".join(dict.fromkeys(actions))
# 通过 callback 通知用户(Gateway 推送 / CLI 显示)
用户会看到类似”技能创建成功 · 记忆已更新”的通知。
和 OpenClaw 的对比
| 维度 | Hermes Agent | OpenClaw |
|---|---|---|
| 技能创建 | 后台 Review Agent 自动判断 | 人工编写 SKILL.md |
| 触发机制 | 计数器(可预测) | 依赖配置 |
| 记忆持久化 | MEMORY.md + USER.md + SQLite | MEMORY.md + memory skills |
| 自进化 | 原生计数 + 后台推理 | 依赖人工维护 |
| 架构复杂度 | 高(后台线程 + 独立 Agent) | 低(单一 Agent) |
Hermes 的方案更自动化,但代价是架构更复杂。OpenClaw 更简单透明,但需要人工投入更多。
设计哲学
回过头看 Hermes 的设计,有几个有意思的原则:
1. 简单胜于复杂
用计数器代替复杂的事件判断逻辑。看起来简陋,但实际效果更好——可预测、易调试、不依赖模糊的”复杂度”定义。
2. 让 AI 决策,不让代码决策
触发后不是直接创建技能,而是让一个独立的 AI 来判断该不该创建。代码只负责框架,具体的”值不值得保存”交给 AI。
3. 隔离优于侵入
后台 Review 不污染主对话,不影响用户响应延迟。学习和干活是分开的。
4. 安全不打折
Agent 自己创建的技能和第三方技能同等安全标准。防止”自我修改”成为安全漏洞。
5. 数据安全优先
记忆 flush 先于压缩,防止数据丢失。这是最基本的工程素养,但很多框架没做好。
Hermes 的自进化机制不是魔法,本质上是:计数器触发 + 独立 AI 推理 + 工具执行 + 安全扫描。工程上没什么独门秘籍,但每个环节都做得比较扎实。
对于想自己实现类似功能的开发者,我的建议是:先想清楚你要解决什么问题,Hermes 的方案未必是唯一的答案,但它的设计思路值得参考。
夜雨聆风