From 80493ccf6275758f1098c584cc202b51c250414f Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 22 Jul 2026 12:28:06 +0800 Subject: [PATCH] fix: order local list_documents newest-first, matching the cloud API The server's GET /docs/ returns createdAt DESC (id ASC tiebreak) and the cloud backend preserves that order, so list_documents()[0] meant the newest document on cloud but the oldest on local (ORDER BY created_at ascending). The same order also feeds _get_all_doc_ids' doc priority for multi-doc chat. rowid DESC tiebreaks same-second inserts by insertion order. --- pageindex/storage/sqlite.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pageindex/storage/sqlite.py b/pageindex/storage/sqlite.py index bcd9d81..945eec5 100644 --- a/pageindex/storage/sqlite.py +++ b/pageindex/storage/sqlite.py @@ -210,7 +210,9 @@ class SQLiteStorage: def list_documents(self, collection: str) -> list[dict]: conn = self._get_conn() rows = conn.execute( - "SELECT doc_id, doc_name, doc_description, doc_type FROM documents WHERE collection_name = ? ORDER BY created_at", + # Newest first, matching the cloud API's createdAt DESC order; + # rowid breaks same-second ties by insertion order. + "SELECT doc_id, doc_name, doc_description, doc_type FROM documents WHERE collection_name = ? ORDER BY created_at DESC, rowid DESC", (collection,), ).fetchall() return [{"doc_id": r[0], "doc_name": r[1], "doc_description": r[2] or "", "doc_type": r[3]} for r in rows]