Skip to content

Commit f0375cc

Browse files
authored
perf(chat): skip redundant transcript scans (#4092)
Signed-off-by: duanjialing.777 <duanjialing.777@bytedance.com> Co-authored-by: duanjialing.777 <duanjialing.777@bytedance.com>
1 parent b8742e8 commit f0375cc

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

‎loopx/chat_store.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,10 @@ def append_message(
489489
"created_at": utc_now(),
490490
}
491491
with exclusive_file_lock(path, agent_id="loopx-chat", operation="append_chat_message"):
492-
for existing in _read_jsonl(path):
493-
if existing.get("message_id") == payload["message_id"]:
494-
return existing
492+
if message_id:
493+
for existing in _read_jsonl(path):
494+
if existing.get("message_id") == payload["message_id"]:
495+
return existing
495496
_append_jsonl(path, payload)
496497
return payload
497498

‎tests/test_chat_session_active_turn.py‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,52 @@ def test_chat_idempotency_keys_reject_different_requests(tmp_path: Path) -> None
186186
)
187187

188188

189+
def test_generated_message_id_skips_transcript_deduplication(
190+
tmp_path: Path,
191+
monkeypatch: pytest.MonkeyPatch,
192+
) -> None:
193+
store = ChatSessionStore(tmp_path)
194+
session_id = str(
195+
store.create_session(
196+
goal_id="goal-one",
197+
agent_id="codex",
198+
executor_endpoint_id="codex",
199+
adapter_kind="codex_app_server",
200+
upstream_thread_id="thread-one",
201+
upstream_mode="chat",
202+
)["session_id"]
203+
)
204+
expected = store.append_message(
205+
session_id,
206+
role="agent",
207+
text="durable completion",
208+
message_id="completion-one",
209+
)
210+
assert (
211+
store.append_message(
212+
session_id,
213+
role="agent",
214+
text="ignored replay",
215+
message_id="completion-one",
216+
)
217+
== expected
218+
)
219+
220+
read_messages = chat_store._read_jsonl
221+
monkeypatch.setattr(
222+
chat_store,
223+
"_read_jsonl",
224+
lambda _path: pytest.fail("generated message IDs must not scan the transcript"),
225+
)
226+
appended = store.append_message(session_id, role="user", text="new message")
227+
228+
assert appended["message_id"] != "completion-one"
229+
assert read_messages(store._session_dir(session_id) / "messages.jsonl") == [
230+
expected,
231+
appended,
232+
]
233+
234+
189235
@pytest.mark.parametrize("restart", [False, True])
190236
@pytest.mark.parametrize("with_image", [False, True])
191237
def test_managed_replay_compares_durable_attachments(

0 commit comments

Comments
 (0)