From d5279c81ff295e3a7a5be7b1bb9842ff8a43ff21 Mon Sep 17 00:00:00 2001 From: "duanjialing.777" Date: Tue, 8 Sep 2026 18:20:44 +0800 Subject: [PATCH] perf(chat): skip redundant transcript scans Signed-off-by: duanjialing.777 --- loopx/chat_store.py | 7 ++-- tests/test_chat_session_active_turn.py | 46 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/loopx/chat_store.py b/loopx/chat_store.py index 2bcfa43573..6215c481ca 100644 --- a/loopx/chat_store.py +++ b/loopx/chat_store.py @@ -489,9 +489,10 @@ def append_message( "created_at": utc_now(), } with exclusive_file_lock(path, agent_id="loopx-chat", operation="append_chat_message"): - for existing in _read_jsonl(path): - if existing.get("message_id") == payload["message_id"]: - return existing + if message_id: + for existing in _read_jsonl(path): + if existing.get("message_id") == payload["message_id"]: + return existing _append_jsonl(path, payload) return payload diff --git a/tests/test_chat_session_active_turn.py b/tests/test_chat_session_active_turn.py index f10d343bf2..938759c1fa 100644 --- a/tests/test_chat_session_active_turn.py +++ b/tests/test_chat_session_active_turn.py @@ -186,6 +186,52 @@ def test_chat_idempotency_keys_reject_different_requests(tmp_path: Path) -> None ) +def test_generated_message_id_skips_transcript_deduplication( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, +) -> None: + store = ChatSessionStore(tmp_path) + session_id = str( + store.create_session( + goal_id="goal-one", + agent_id="codex", + executor_endpoint_id="codex", + adapter_kind="codex_app_server", + upstream_thread_id="thread-one", + upstream_mode="chat", + )["session_id"] + ) + expected = store.append_message( + session_id, + role="agent", + text="durable completion", + message_id="completion-one", + ) + assert ( + store.append_message( + session_id, + role="agent", + text="ignored replay", + message_id="completion-one", + ) + == expected + ) + + read_messages = chat_store._read_jsonl + monkeypatch.setattr( + chat_store, + "_read_jsonl", + lambda _path: pytest.fail("generated message IDs must not scan the transcript"), + ) + appended = store.append_message(session_id, role="user", text="new message") + + assert appended["message_id"] != "completion-one" + assert read_messages(store._session_dir(session_id) / "messages.jsonl") == [ + expected, + appended, + ] + + @pytest.mark.parametrize("restart", [False, True]) @pytest.mark.parametrize("with_image", [False, True]) def test_managed_replay_compares_durable_attachments(