diff --git a/navi/core/pg_session_store.py b/navi/core/pg_session_store.py index d793530..a1d55ca 100644 --- a/navi/core/pg_session_store.py +++ b/navi/core/pg_session_store.py @@ -242,28 +242,22 @@ pool = await self._get_pool() async with pool.acquire() as conn: row = await conn.fetchrow( - "SELECT id, profile_id, user_id, messages, context, pinned, created_at, last_active, context_token_count, name, planning_logs " + "SELECT id, profile_id, user_id, pinned, created_at, last_active, context_token_count, name, planning_logs " "FROM sessions WHERE id = $1", session_id, ) if not row: return None - msg_rows = await conn.fetch( - "SELECT * FROM session_messages WHERE session_id = $1 AND is_display = true ORDER BY sequence_number", + # Load all messages once so messages[] and context[] share + # the same Python objects (id() matching in the agent works). + all_rows = await conn.fetch( + "SELECT * FROM session_messages WHERE session_id = $1 ORDER BY sequence_number", session_id, ) - if msg_rows: - messages = [_row_to_message(r) for r in msg_rows] - ctx_rows = await conn.fetch( - "SELECT * FROM session_messages WHERE session_id = $1 AND is_context = true ORDER BY sequence_number", - session_id, - ) - context = [_row_to_message(r) for r in ctx_rows] - else: - messages = _deserialize(row["messages"]) - context_json = row["context"] - context = _deserialize(context_json) if context_json else list(messages) + all_messages = [_row_to_message(r) for r in all_rows] + messages = [m for m in all_messages if m.is_display] + context = [m for m in all_messages if m.is_context] planning_logs_raw = row["planning_logs"] planning_logs = json.loads(planning_logs_raw) if planning_logs_raw else []