$ cat ./article.md

Hermes 观测系统修复报告

$ cat ./section-1.md

📋 问题概述

发现的问题

  1. 3/23 后观测停止 - 3/23 后没有新的观测记录
  2. Pending 状态积压 - 171/174 条记录处于 pending 状态 (98.3%)
  3. Response length 记录异常 - 大多数记录长度为 0

根本原因分析

问题根源: run_conversation() 方法中有 15+ 个提前返回点

↓ _save_multimodal_observation() 只在方法末尾调用一次 ↓ 任何提前返回都会导致观测记录无法更新 ↓ Pending 记录积压,response_length 始终为 0


$ cat ./section-2.md

🔧 修复内容

修复 1: 数据清理(已执行)

修复脚本: ~/hermes-agent/fix_observation_issues.py 操作:
  • 将 171 条长时间 pending 的记录标记为 timeout
  • 数据库已自动备份
结果:
修复前: 171 pending, 3 completed

修复后: 0 pending, 3 completed, 171 timeout

修复 2: 代码修复(已应用)

修改文件: ~/hermes-agent/run_agent.py 关键变更:
  1. 在 while 循环前添加 try 块 (第 3606 行):
<h1>PHASE 3 FIX: Track if observation was recorded and ensure it's always updated</h1>

_observation_recorded = bool(self._last_observation_id)

try: while api_call_count < self.max_iterations and self.iteration_budget.remaining > 0:

  1. 添加 finally 块确保更新 (第 4848 行):
finally:

# PHASE 3 FIX: Ensure observation is always updated on exit if _observation_recorded: _obs_latency_ms = int((time.time() - _obs_start_time) * 1000) self._save_multimodal_observation( api_call_count=api_call_count, latency_ms=_obs_latency_ms, final_response=final_response or "" )

  1. 移除原方法末尾的重复调用

$ cat ./section-3.md

✅ 修复验证

当前状态

| 指标 | 修复前 | 修复后 | 状态 | |------|--------|--------|------| | Pending 记录 | 171 (98.3%) | 0 (0%) | ✅ 已清理 | | Completed 记录 | 3 (1.7%) | 3 (1.7%) | ✅ 正常 | | Timeout 记录 | 0 | 171 (98.3%) | ✅ 已标记 | | Response length 非零 | 3 | 3 | ⚠️ 需观察 | | 最新记录时间 | 3/23 | 3/23 | ⚠️ 需测试 |

验证结果

======================================================================

🔍 观测系统修复验证报告 ======================================================================

  1. Pending 记录检查:

当前 pending 数量: 0 ✅ 通过: 没有 pending 记录积压

  1. 观测状态分布:

completed: 3 timeout: 171

  1. Response Length 记录:

平均长度: 4 最大长度: 300 非零记录: 3 / 174

  1. 最新记录时间: 2026-03-23 22:05:53

⚠️ 警告: 最新记录是 165.1 小时前 ======================================================================


$ cat ./section-4.md

🧪 测试建议

测试步骤

  1. 重启 Hermes Agent
   hermes restart

# 或 python cli.py

  1. 执行测试对话
   用户: 你好,这是一个测试消息

   
  1. 验证观测记录
   python ~/hermes-agent/verify_observation_fix.py

   
  1. 检查数据库
   sqlite3 ~/.hermes/state.db "SELECT * FROM interaction_observations ORDER BY timestamp DESC LIMIT 5;"

   

预期结果

  • ✅ 生成新的 observation 记录
  • ✅ action_type = 'completed'
  • ✅ response_length > 0
  • ✅ latency_ms > 0
  • ✅ 记录时间 = 当前时间

$ cat ./section-5.md

📊 技术细节

问题代码分析

原代码问题:
<h1>run_conversation() 方法中有 15+ 个提前返回点</h1>

if some_error: return {"error": "..."} # 观测记录不会更新!

if max_retries_exceeded: return {"error": "..."} # 观测记录不会更新!

<h1>... 更多提前返回 ...</h1>

<h1>只有这里调用更新,但可能永远不会执行到</h1> _save_multimodal_observation(...)

修复后代码:
_observation_recorded = bool(self._last_observation_id)

try: while ...: # ... 主循环 ... if some_error: return {"error": "..."} # finally 块仍会执行! finally: if _observation_recorded: _save_multimodal_observation(...) # 确保总是更新!


$ cat ./section-6.md

🎯 改进建议

短期

  1. 已修复: 使用 try...finally 确保观测记录更新
  2. 已修复: 清理历史 pending 记录
  3. 待验证: 运行测试对话确认修复有效

中期

  1. 添加监控告警

- 当 pending 记录超过阈值时告警 - 当观测停止超过 1 小时时告警

  1. 添加健康检查端点
   # /health/observations

{ "status": "healthy", "pending_count": 0, "last_record_time": "2026-03-30T19:00:00Z" }

  1. 改进错误处理

- 记录观测更新失败的原因 - 添加重试机制

长期

  1. 观测系统重构

- 使用上下文管理器 (context manager) - 支持批量更新 - 异步写入优化性能

  1. 可视化仪表板

- 实时观测统计 - 趋势分析 - 异常检测


$ cat ./section-7.md

📁 相关文件

| 文件 | 描述 | |------|------| | ~/hermes-agent/run_agent.py | 主修复文件 | | ~/hermes-agent/fix_observation_issues.py | 数据修复脚本 | | ~/hermes-agent/verify_observation_fix.py | 验证脚本 | | ~/.hermes/state.db | SQLite 数据库 | | ~/.hermes/state.db.backup.* | 数据库备份 |


$ cat ./section-8.md

📝 变更日志

2026-03-30

  • 19:05 - 执行数据修复,清理 171 条 pending 记录
  • 19:06 - 应用代码修复,添加 try...finally 块
  • 19:07 - 生成修复报告

$ cat ./section-9.md

✅ 检查清单

  • [x] 识别问题根本原因
  • [x] 执行数据修复
  • [x] 应用代码修复
  • [x] 生成修复报告
  • [ ] 运行测试对话验证
  • [ ] 监控观测系统运行状态
  • [ ] 添加监控告警(可选)

修复完成时间: 2026-03-30 19:07 修复人员: Hermes Agent 下次检查: 建议运行测试对话后立即验证