diff --git a/core/web_terminal.py b/core/web_terminal.py index 8669abcf..9d82ad3d 100644 --- a/core/web_terminal.py +++ b/core/web_terminal.py @@ -835,10 +835,14 @@ class WebTerminal(MainTerminal): def __del__(self): """析构函数,确保资源释放""" try: - # 保存当前对话 + # 保存当前对话(仅当内存中确有消息时)。 + # 竞态重建/回收场景下,被丢弃的实例可能只设了 + # current_conversation_id 而 conversation_history 为空, + # 此时保存会把磁盘上非空的对话覆盖成空消息。 if hasattr(self, 'context_manager') and self.context_manager: - if self.context_manager.current_conversation_id: - self.context_manager.save_current_conversation() + cm = self.context_manager + if cm.current_conversation_id and getattr(cm, 'conversation_history', None): + cm.save_current_conversation() # 关闭所有终端 if hasattr(self, 'terminal_manager') and self.terminal_manager: diff --git a/server/context.py b/server/context.py index 4ffdb75f..44455b0d 100644 --- a/server/context.py +++ b/server/context.py @@ -893,7 +893,8 @@ def reap_idle_conversation_terminals(now: Optional[float] = None) -> int: continue try: cm = getattr(terminal, "context_manager", None) - if cm and getattr(cm, "current_conversation_id", None): + # 与 __del__ 同理:空 history 保存会把磁盘上非空对话覆盖为空 + if cm and getattr(cm, "current_conversation_id", None) and getattr(cm, "conversation_history", None): cm.save_current_conversation() except Exception as exc: debug_log(f"[ConvTerminalReaper] 保存对话失败 {conversation_id}: {exc}")