diff --git a/mcp-server/src/legal_mcp/services/db.py b/mcp-server/src/legal_mcp/services/db.py index 1f3bdb2..8f52561 100644 --- a/mcp-server/src/legal_mcp/services/db.py +++ b/mcp-server/src/legal_mcp/services/db.py @@ -1713,7 +1713,12 @@ async def list_cases( where.append("archived_at IS NULL") where_clause = f"WHERE {' AND '.join(where)}" if where else "" args.append(limit) - sql = f"SELECT * FROM cases {where_clause} ORDER BY updated_at DESC LIMIT ${len(args)}" + # Archived list is ordered by archive date (newest-first) so the order is + # server-authoritative and the /archive page is correct on load, regardless + # of client-side sorting. NULLS LAST is defensive — the archived_only filter + # already excludes NULLs. + order_by = "archived_at DESC NULLS LAST" if archived_only else "updated_at DESC" + sql = f"SELECT * FROM cases {where_clause} ORDER BY {order_by} LIMIT ${len(args)}" async with pool.acquire() as conn: rows = await conn.fetch(sql, *args) return [_row_to_case(r) for r in rows]