From 1cc2218cf04833476481c9fcdb38d026d5319b5f Mon Sep 17 00:00:00 2001 From: Lihua <1017343802@qq.com> Date: Fri, 11 Sep 2026 20:54:20 -0700 Subject: [PATCH] docs(chat): explain event sequence ordering invariant Signed-off-by: Lihua <1017343802@qq.com> --- loopx/chat_store.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/loopx/chat_store.py b/loopx/chat_store.py index e4832ba7ac..bb51aa8d28 100644 --- a/loopx/chat_store.py +++ b/loopx/chat_store.py @@ -1320,6 +1320,9 @@ def flush_events(self, session_id: str, turn_id: str) -> int: try: with exclusive_file_lock(path, agent_id="loopx-chat", operation="append_chat_events"): rows = self._event_rows_locked(session_id, turn_id) + # Allocate after the last persisted sequence and append under the + # same file lock: row order stays strictly increasing by sequence. + # Compaction preserves this order but may leave sequence gaps. sequence = int(rows[-1].get("sequence") or 0) if rows else 0 for event in pending: sequence += 1 @@ -1355,6 +1358,8 @@ def events_after(self, session_id: str, turn_id: str, event_id: str | None) -> l if rows is None: with exclusive_file_lock(path, agent_id="loopx-chat", operation="read_chat_events"): rows = self._event_rows_locked(session_id, turn_id) + # Relies on the sequence ordering maintained by flush_events and compaction; + # gaps are valid, but inserting or rewriting rows must preserve that order. start = bisect_right( rows, after,