From b324fae61158d4141504a91e9191079c57d886c9 Mon Sep 17 00:00:00 2001 From: WuL0og <2992782994@qq.com> Date: Mon, 7 Sep 2026 22:20:06 +0800 Subject: [PATCH] fix(continue): snapshot the active log on reset Agents write to a random log ID, while reset used the PID-derived fallback. Pass agent.log_path, preserve legacy PID callers, and treat False as logging disabled. Reimplements the focused fix from #552 on current main. --- frontends/continue_cmd.py | 10 +-- frontends/tests/test_continue_log_snapshot.py | 65 +++++++++++++++++++ 2 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 frontends/tests/test_continue_log_snapshot.py diff --git a/frontends/continue_cmd.py b/frontends/continue_cmd.py index 3e926c11c..b081a49c5 100644 --- a/frontends/continue_cmd.py +++ b/frontends/continue_cmd.py @@ -448,10 +448,10 @@ def _current_log_path(pid=None): return os.path.join(_LOG_DIR, f'model_responses_{pid}.txt') -def _snapshot_current_log(pid=None): - """Persist current PID log as a standalone recoverable snapshot, then clear it.""" - path = _current_log_path(pid) - if not os.path.isfile(path): +def _snapshot_current_log(pid=None, path=None): + """Persist the active log as a standalone recoverable snapshot, then clear it.""" + path = _current_log_path(pid) if path is None else path + if not path or not os.path.isfile(path): return None try: with open(path, encoding='utf-8', errors='replace') as fh: @@ -477,7 +477,7 @@ def reset_conversation(agent, message='🆕 已开启新对话,当前上下文 agent.abort() except Exception: pass - _snapshot_current_log() + _snapshot_current_log(path=getattr(agent, 'log_path', None)) if hasattr(agent, 'history'): agent.history = [] for client in _agent_clients(agent): diff --git a/frontends/tests/test_continue_log_snapshot.py b/frontends/tests/test_continue_log_snapshot.py new file mode 100644 index 000000000..f7e0b86df --- /dev/null +++ b/frontends/tests/test_continue_log_snapshot.py @@ -0,0 +1,65 @@ +import os +from importlib.util import module_from_spec, spec_from_file_location +from pathlib import Path +from types import SimpleNamespace + + +ROOT = Path(__file__).resolve().parents[2] +SPEC = spec_from_file_location("continue_cmd_under_test", ROOT / "frontends" / "continue_cmd.py") +assert SPEC and SPEC.loader +continue_cmd = module_from_spec(SPEC) +SPEC.loader.exec_module(continue_cmd) + +LOG_CONTENT = ( + "=== Prompt === 2026-01-01 00:00:00\nhello\n\n" + "=== Response === 2026-01-01 00:00:01 model=test\nworld\n\n" +) + + +def _agent(log_path): + backend = SimpleNamespace(history=["backend turn"]) + client = SimpleNamespace(backend=backend, last_tools="cached tools") + return SimpleNamespace( + abort=lambda: None, log_path=log_path, history=["agent turn"], + llmclients=[client], llmclient=client, handler=object(), + ) + + +def test_reset_snapshots_the_agents_active_log(tmp_path, monkeypatch): + active_log = tmp_path / f"model_responses_{os.getpid() + 1}.txt" + active_log.write_text(LOG_CONTENT, encoding="utf-8") + agent = _agent(str(active_log)) + monkeypatch.setattr(continue_cmd, "_LOG_DIR", str(tmp_path)) + + continue_cmd.reset_conversation(agent) + + snapshots = list(tmp_path.glob("model_responses_snapshot_*.txt")) + assert active_log.read_text(encoding="utf-8") == "" + assert len(snapshots) == 1 + assert snapshots[0].read_text(encoding="utf-8") == LOG_CONTENT + assert agent.history == [] + assert agent.llmclient.backend.history == [] + assert agent.llmclient.last_tools == "" + assert agent.handler is None + + +def test_reset_does_not_touch_pid_log_when_logging_is_disabled(tmp_path, monkeypatch): + pid_log = tmp_path / f"model_responses_{os.getpid()}.txt" + pid_log.write_text(LOG_CONTENT, encoding="utf-8") + monkeypatch.setattr(continue_cmd, "_LOG_DIR", str(tmp_path)) + + continue_cmd.reset_conversation(_agent(False)) + + assert pid_log.read_text(encoding="utf-8") == LOG_CONTENT + assert not list(tmp_path.glob("model_responses_snapshot_*.txt")) + + +def test_snapshot_retains_the_legacy_pid_fallback(tmp_path, monkeypatch): + pid_log = tmp_path / "model_responses_123456.txt" + pid_log.write_text(LOG_CONTENT, encoding="utf-8") + monkeypatch.setattr(continue_cmd, "_LOG_DIR", str(tmp_path)) + + snapshot = continue_cmd._snapshot_current_log(123456) + + assert pid_log.read_text(encoding="utf-8") == "" + assert Path(snapshot).read_text(encoding="utf-8") == LOG_CONTENT