From 8628954f3093177209bb0c5c404c353ab40a3fc6 Mon Sep 17 00:00:00 2001 From: JOJO <1498581755@qq.com> Date: Mon, 20 Jul 2026 13:26:18 +0800 Subject: [PATCH] =?UTF-8?q?fix(server):=20=E4=BF=AE=E5=A4=8D=E5=AF=B9?= =?UTF-8?q?=E8=AF=9D=E6=B6=88=E6=81=AF=E8=A2=AB=E7=A9=BA=E5=8E=86=E5=8F=B2?= =?UTF-8?q?=E8=A6=86=E7=9B=96=E2=80=94=E2=80=94WebTerminal.=5F=5Fdel=5F=5F?= =?UTF-8?q?=E4=B8=8E=E5=9B=9E=E6=94=B6=E5=99=A8=E4=BF=9D=E5=AD=98=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E9=9D=9E=E7=A9=BA=E5=AE=88=E5=8D=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 对话级 terminal 隔离后,被丢弃的实例(竞态重复、回收重建、TTL回收) 常处于 current_conversation_id 已设但 conversation_history 为空的状态, GC 触发 __del__ 时无条件 save_current_conversation 把磁盘非空对话覆盖为空。 两处保存路径均增加 conversation_history 非空守卫。 --- core/web_terminal.py | 10 +++++++--- server/context.py | 3 ++- 2 files changed, 9 insertions(+), 4 deletions(-) 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}")