hermes - 💡(How to fix) Fix [Bug] create_session 静默失败 + 消息重复写入 — 会话从 state.db 消失 [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
NousResearch/hermes-agent#12563Fetched 2026-04-20 12:18:14
View on GitHub
Comments
0
Participants
1
Timeline
0
Reactions
0
Author
Participants

Error Message

try: session_id = await state.create_session(...) except Exception as e: logger.warning(f"Failed to create session: {e}") # ← 静默失败,没有补救 return # session_id 未设置,后续 append_message 跳过

Code Example

try:
        session_id = await state.create_session(...)
    except Exception as e:
        logger.warning(f"Failed to create session: {e}")  # ← 静默失败,没有补救
        return  # session_id 未设置,后续 append_message 跳过
RAW_BUFFERClick to expand / collapse

Bug 描述

两个相关 Bug 导致会话从 `state.db` 消失,但 JSON 文件正常保存。

### Bug 1:create_session() 静默失败 → session 从未进入数据库

**位置:** `run_agent.py` 第 ~1253 行

当 `create_session()` 遇到 SQLite 锁竞争错误时,会捕获异常、打印 warning,然后直接 return。Session 的 JSON 文件正常创建,但 **sessions 表里没有对应的行**。之后 `append_message` 因为 session row 不存在,跳过写入。

**代码路径:**
```python
try:
    session_id = await state.create_session(...)
except Exception as e:
    logger.warning(f"Failed to create session: {e}")  # ← 静默失败,没有补救
    return  # session_id 未设置,后续 append_message 跳过
```

**影响:** Session JSON 文件堆积在 `~/.hermes/sessions/`,但 `session_search` 查不到(它查的是 DB)。

### Bug 2:_flush_messages_to_session_db 重复写入所有消息

**位置:** `run_agent.py` ~第 2590 行,`_flush_messages_to_session_db`

每次刷新消息时,**所有消息都被重新写入一遍**,而不是只写入新增的消息。`_last_flushed_db_idx` 游标没有正确追踪已写入的消息。导致在 DB 中的 session,message_count 是实际 JSON 消息数的 **2 倍**。

## 复现步骤

1. 同时运行多个 Hermes 实例(cron + CLI 并发)
2. 观察 `state.db` 的 session 数量少于 `~/.hermes/sessions/*.json` 文件数量
3. 对于存活在 DB 中的 session,message_count 是 JSON 实际数量的 2 倍

## 环境

- Hermes(NousResearch/hermes-agent)
- 系统:WSL2(Ubuntu 22.04)
- 数据库:SQLite state.db

## 预期 vs 实际

| 项目 | 预期 | 实际 |
|------|------|------|
| Session 在 DB 中 | JSON 创建后就有 | create_session 失败时消失 |
| 消息数量 | 准确递增 | 受影响 session 是 2 倍 |
| session_search | 返回所有 session | 只返回成功写入 DB 的 |

## 建议修复

**Bug 1:** 添加重试逻辑 + 错误升级。至少把 `logger.warning` 改成 `logger.error` 让错误可见。

**Bug 2:** 确保 `_last_flushed_db_idx` 在写入成功后更新,而不是写入前。可通过对比写入前后的 message_count 验证。

## 相关 Issue

- Issue #12029:session 泄漏 / end_session 未调用(同类根因)
- Issue #11511:FTS5 中文搜索失败(已修复)

extent analysis

TL;DR

Fix the silent failure in create_session() by adding retry logic and error escalation, and update _last_flushed_db_idx after successful message writes to prevent duplicate message writes.

Guidance

  • For Bug 1, modify the create_session() exception handling to include retry logic and escalate the error from logger.warning to logger.error to make the issue more visible.
  • For Bug 2, update the _last_flushed_db_idx cursor after successful message writes to prevent re-writing all messages and ensure accurate message counts.
  • Verify the fixes by checking the session counts in state.db and the message counts in the JSON files, and ensure that session_search returns all expected sessions.
  • To mitigate the issue temporarily, consider reducing the concurrency of Hermes instances or implementing a queue to handle session creations and message writes.

Example

try:
    session_id = await state.create_session(...)
except Exception as e:
    logger.error(f"Failed to create session: {e}")  # Escalate error
    # Add retry logic here, e.g., retry after a short delay
    # ...

Notes

The provided guidance assumes that the issue is caused by the described bugs and that the fixes will resolve the problems. However, additional testing and verification are necessary to ensure that the fixes work as expected and do not introduce new issues.

Recommendation

Apply the suggested workarounds, specifically adding retry logic and error escalation to create_session() and updating _last_flushed_db_idx after successful message writes, to address the identified bugs and prevent session loss and duplicate message writes.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING