【Bug已解决】openclaw: "conversation history lost" / Cannot resume session — OpenClaw 对话历史丢失解决方案

1. 问题描述

OpenClaw 的对话历史丢失,无法恢复之前的会话:

# 对话历史丢失
$ openclaw --continue
Warning: No previous session found
Starting a new session.

# 或会话恢复失败
$ openclaw --resume session-abc123
Error: Session not found
Session 'session-abc123' does not exist.

# 或历史被清除
$ openclaw --continue
# 之前的对话内容完全丢失

# 或会话文件损坏
$ openclaw --continue
Error: Session file corrupted
Cannot load session history.

这个问题在以下场景中特别常见:

  • 会话文件被删除
  • 清理工具误删
  • 异常退出导致文件损坏
  • 磁盘空间不足
  • 会话 ID 错误
  • 权限问题

2. 原因分析

原因分类表

原因分类 具体表现 占比
文件被删除 rm/清理 约 35%
异常退出 损坏 约 25%
磁盘不足 写入失败 约 15%
ID 错误 不存在 约 10%
权限问题 EACCES 约 10%
清理工具 误删 约 5%

3. 解决方案

方案一:检查会话文件(最推荐)

# 步骤 1:检查会话目录
ls -la ~/.openclaw/sessions/

# 步骤 2:如果文件存在
openclaw --list-sessions

# 步骤 3:恢复最近的会话
openclaw --continue

# 步骤 4:验证
openclaw --debug 2>&1 | grep -i "session"

方案二:使用正确的会话 ID

# 步骤 1:列出所有会话
openclaw --list-sessions

# 步骤 2:使用正确的 ID
openclaw --resume session-correct-id

# 步骤 3:或使用最近的
openclaw --continue

# 步骤 4:验证
openclaw --debug 2>&1 | grep -i "session"

方案三:修复损坏的会话

# 步骤 1:检查会话文件
python3 -m json.tool ~/.openclaw/sessions/current.json

# 步骤 2:如果损坏
rm ~/.openclaw/sessions/current.json

# 步骤 3:或尝试修复
python3 -c "
import json
try:
    with open('/root/.openclaw/sessions/current.json') as f:
        data = json.load(f)
    print('Valid session')
except:
    print('Corrupted, removing')
    import os
    os.remove('/root/.openclaw/sessions/current.json')
"

# 步骤 4:验证
openclaw "task"  # 新会话

方案四:检查权限

# 步骤 1:检查权限
ls -la ~/.openclaw/sessions/

# 步骤 2:修复权限
sudo chown -R $(whoami) ~/.openclaw/sessions/
chmod -R u+rw ~/.openclaw/sessions/

# 步骤 3:验证
ls -la ~/.openclaw/sessions/

# 步骤 4:测试
openclaw "task"
openclaw --continue

方案五:检查磁盘空间

# 步骤 1:检查磁盘
df -h ~

# 步骤 2:如果空间不足
rm -rf ~/.openclaw/sessions/old-*
pip cache purge
npm cache clean --force

# 步骤 3:验证空间
df -h ~

# 步骤 4:测试
openclaw "task"
openclaw --continue

方案六:使用文件保存上下文

# 步骤 1:保存对话到文件
openclaw --print "将之前的讨论总结保存到 docs/summary.md"

# 步骤 2:新会话引用
openclaw "参考 docs/summary.md 继续工作"

# 步骤 3:或使用 Git
git add -A
git commit -m "Save progress"

# 步骤 4:验证
cat docs/summary.md

4. 各方案对比总结

方案 适用场景 推荐指数 难度
方案一:检查文件 排查 ⭐⭐⭐⭐⭐
方案二:正确 ID ID 错误 ⭐⭐⭐⭐⭐
方案三:修复损坏 损坏 ⭐⭐⭐⭐⭐
方案四:权限 EACCES ⭐⭐⭐⭐
方案五:磁盘 磁盘满 ⭐⭐⭐⭐
方案六:文件保存 长期 ⭐⭐⭐⭐⭐

5. 常见问题 FAQ

5.1 会话文件在哪里

~/.openclaw/sessions/ 目录下。

5.2 如何列出所有会话

openclaw --list-sessions

5.3 如何恢复会话

openclaw --continue  # 恢复最近的
openclaw --resume session-id  # 恢复指定的

5.4 会话文件损坏怎么办

删除损坏的文件,重新开始新会话。

5.5 如何避免历史丢失

使用 exit 正常退出,定期保存讨论到文件。

5.6 --continue 和 --resume 的区别

  • --continue: 恢复最近的会话
  • --resume <id>: 恢复指定的会话

5.7 清理工具误删会话

~/.openclaw/sessions/ 加入清理工具的排除列表。

5.8 如何保存上下文

openclaw --print "将讨论保存到 docs/summary.md"

5.9 会话文件大小

通常几 KB 到几 MB,取决于对话长度。

5.10 排查清单速查表

□ 1. ls ~/.openclaw/sessions/ 检查
□ 2. openclaw --list-sessions 列出
□ 3. openclaw --continue 恢复最近
□ 4. openclaw --resume session-id
□ 5. python3 -m json.tool 验证
□ 6. rm 损坏的会话文件
□ 7. sudo chown -R $(whoami) 权限
□ 8. df -h 检查磁盘
□ 9. openclaw --print "保存到文件"
□ 10. exit 正常退出

6. 总结

  1. 根本原因:对话历史丢失最常见原因是文件被删除(35%)和异常退出导致损坏(25%)
  2. 最佳实践:使用 openclaw --list-sessions 检查,openclaw --continue 恢复
  3. 修复损坏rm ~/.openclaw/sessions/current.json 删除损坏文件,重新开始
  4. 保存上下文:使用 openclaw --print "保存到 docs/summary.md" 保存讨论
  5. 最佳实践建议:使用 exit 正常退出,定期保存讨论到文件,避免清理工具误删

故障排查流程图

flowchart TD
    A[对话历史丢失] --> B[ls ~/.openclaw/sessions/]
    B --> C{文件存在?}
    C -->|是| D[openclaw --list-sessions]
    C -->|否| E[检查磁盘空间]
    D --> F[openclaw --continue]
    F --> G{恢复成功?}
    G -->|是| H[✅ 问题解决]
    G -->|否| I[检查文件损坏]
    E --> j[df -h ~]
    j --> K{空间足够?}
    K -->|否| L[清理空间]
    K -->|是| M[检查权限]
    L --> F
    M --> N[sudo chown -R $(whoami)]
    N --> F
    I --> O[python3 -m json.tool]
    O --> P{JSON 有效?}
    P -->|否| Q[rm 损坏文件]
    P -->|是| R[检查会话 ID]
    Q --> S[openclaw "task" 新会话]
    R --> T[openclaw --resume 正确 ID]
    T --> G
    S --> U[使用文件保存上下文]
    U --> V[openclaw --print "保存到 docs/summary.md"]
    V --> W[新会话引用文件]
    W --> H
    H --> X[长期: exit 退出 + 保存文件 + 排除清理]
    X --> Y[✅ 长期方案]
Logo

CANN开发者社区旨在汇聚广大开发者,围绕CANN架构重构、算子开发、部署应用优化等核心方向,展开深度交流与思想碰撞,携手共同促进CANN开放生态突破!

更多推荐