Same as M1 but with one extra rule: regardless of the 30-minute gap, the event IMMEDIATELY AFTER a logout always starts a new session. (A logout terminates the current session even if the next event is seconds later.)
Sort by user_id then event_ts ascending.