Choosing RX1 vs RX2

Based on what does the TTN decides if it sends the downlink message in the first or the second interval?
I tried searching in TTS source but I got lost in the code.

Any specific reason to want to know or just curious?

Which region(s)?

EU868 would decide mostly by available duty-cycle. Default RX2 frequency has 10% duty-cycle available vs 1% for other bands.

Scheduling collision with other packets would be another consideration.

Suspicion that the device is missing packets on either RX window may favor rx1 or rx2.

EU868
Actually, I should write join 1 vs join 2 interval.
I was dealing with a bug in device where it was joining only after several retries (when it received join accept in join interval 2) so it got me thinking how is this decided. To me it seemed like 90% of the responses were sent in join 1.
Can you please point me to TTS code where this is decided?

This was the bug:

And why not, if that slot is available.

I wouldn’t expect too many of us here to be that deep in to the source code - the TTI staff who wrote the code will know but will be busy with doing TTI stuff.

I’d note that the LoRaMac in Newt is very old now and so you may find all sorts of other issues arise as TTS is far more picky about compliance.