0.83.1: no third in a row while another show waits, proven against the 0.81.4 baselines
pauljd/davidson-ci: Davidson CI / quality (push) Failing after 3m11s
pauljd/davidson-ci: Davidson CI / ai-review (push) Failing after 1m38s

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-03 10:17:15 +00:00
co-authored by Claude Opus 5.5
parent 310d878ef3
commit 34ea521d62
8 changed files with 115 additions and 22 deletions
@@ -228,7 +228,11 @@ internal fun extendFilmSchedule(pool: List<PoolSeries>, cursor: ScheduleCursor,
/** Shows take turns, one episode each, back to back from [fromMs] until [untilMs] is covered. */
internal fun extendSchedule(pool: List<PoolSeries>, cursor: ScheduleCursor, fromMs: Long, untilMs: Long, filmRestMs: Long? = null, finishTogether: Boolean = false,
dayParts: List<DayPart> = emptyList(), zone: java.time.ZoneId = java.time.ZoneId.systemDefault(), avoid: ProgrammeClash = { _, _, _ -> false }, shuffle: Boolean = false): ChannelPlan {
dayParts: List<DayPart> = emptyList(), zone: java.time.ZoneId = java.time.ZoneId.systemDefault(), avoid: ProgrammeClash = { _, _, _ -> false }, shuffle: Boolean = false,
/** How many times the cursor's last show had already played in a row, so a run carries across a top-up. */
runBefore: Int = 0,
/** Off only in tests, to show that this rule is the one difference from planning before it existed. */
noThirdInARow: Boolean = true): ChannelPlan {
require(pool.isNotEmpty() && pool.all { it.episodes.isNotEmpty() })
if (filmRestMs != null) return extendFilmSchedule(pool, cursor, fromMs, untilMs, filmRestMs, dayParts, zone, avoid)
val marks = cursor.marks.toMutableMap()
@@ -236,6 +240,8 @@ internal fun extendSchedule(pool: List<PoolSeries>, cursor: ScheduleCursor, from
var last = cursor.lastSeriesId
var start = fromMs
val mixes = HashMap<String, List<PoolEpisode>>()
// How many times in a row the last show has played, counted from the start of this extension.
var run = if (cursor.lastSeriesId != null) runBefore else 0
fun running(show: PoolSeries) = if (shuffle) mixes.getOrPut(show.id) { shuffledEpisodes(show) } else show.episodes
fun following(show: PoolSeries): PoolEpisode {
if (!shuffle) return nextEpisode(show, marks[show.id])
@@ -257,11 +263,21 @@ internal fun extendSchedule(pool: List<PoolSeries>, cursor: ScheduleCursor, from
else -> true
}
}
val series = picked ?: nextInTurn(pool, last, allowed)
val chosen = picked ?: nextInTurn(pool, last, allowed)
// Never a third in a row while another show in the block is only waiting for the round to finish. That one starts its next round early.
val early = if (!noThirdInARow || !finishTogether || chosen.id != last || run < 2) null else turnOrder(pool, last).firstOrNull { show ->
show.id != chosen.id && show in available && marks[show.id] == running(show).last().mark && !avoid(show, start, start + running(show).first().durationMs)
}
if (early != null) marks.remove(early.id)
val series = early ?: chosen
val episode = following(series)
val why = buildString {
append(block)
append(if (picked != null) "; next in turn" else "; fallback: every show in turn airs elsewhere then")
append(when {
early != null -> "; started its next round early, so ${chosen.title} doesn't play a third time in a row"
picked != null -> "; next in turn"
else -> "; fallback: every show in turn airs elsewhere then"
})
if (shuffle) append(", episode from its shuffled order")
if (restarted) append("; every show had finished, so all started again")
if (waiting.isNotEmpty()) append("; deferred until the rest finish: ${someOf(waiting)}")
@@ -271,6 +287,7 @@ internal fun extendSchedule(pool: List<PoolSeries>, cursor: ScheduleCursor, from
val media = ScheduledMedia(item, episode.sourceId, episode.durationMs)
entries += ScheduleEntry(series.id, episode.mark, media, start, why)
marks[series.id] = episode.mark
run = if (series.id == last) run + 1 else 1
last = series.id
start += episode.durationMs
}
@@ -298,12 +315,13 @@ internal fun airedCursor(base: ScheduleCursor, entries: List<ScheduleEntry>, now
* With no usable plan it starts from now at the aired position.
*/
internal fun topUpPlan(existing: ChannelPlan?, aired: ScheduleCursor, pool: List<PoolSeries>?, nowMs: Long, filmRestMs: Long? = null, finishTogether: Boolean = false,
dayParts: List<DayPart> = emptyList(), zone: java.time.ZoneId = java.time.ZoneId.systemDefault(), avoid: ProgrammeClash = { _, _, _ -> false }, shuffle: Boolean = false): ChannelPlan? {
dayParts: List<DayPart> = emptyList(), zone: java.time.ZoneId = java.time.ZoneId.systemDefault(), avoid: ProgrammeClash = { _, _, _ -> false }, shuffle: Boolean = false, noThirdInARow: Boolean = true): ChannelPlan? {
val kept = existing?.let { plan -> plan.copy(entries = plan.entries.dropWhile { it.endMs < nowMs - SCHEDULE_KEEP_PAST_MS }) }
if (kept != null && kept.endMs >= nowMs + SCHEDULE_TOP_UP_MS) return kept
if (pool.isNullOrEmpty()) return kept
val from = if (kept != null && kept.endMs > nowMs) kept.endMs else nowMs
val added = extendSchedule(pool, existing?.tail ?: aired, from, nowMs + SCHEDULE_AHEAD_MS, filmRestMs, finishTogether, dayParts, zone, avoid, shuffle)
val trailing = kept?.entries?.lastOrNull()?.let { tail -> kept.entries.asReversed().takeWhile { it.seriesId == tail.seriesId }.size } ?: 0
val added = extendSchedule(pool, existing?.tail ?: aired, from, nowMs + SCHEDULE_AHEAD_MS, filmRestMs, finishTogether, dayParts, zone, avoid, shuffle, trailing, noThirdInARow)
return ChannelPlan(kept?.entries.orEmpty() + added.entries, added.tail)
}
@@ -66,7 +66,7 @@ internal class HttpsEmbyTransport(internal val deviceId: String = UUID.randomUUI
connection.connectTimeout = 10_000
connection.readTimeout = request.readTimeoutMs ?: if (request.path == "/Sessions/Logout") 5_000 else 15_000
connection.setRequestProperty("Accept", "application/json")
connection.setRequestProperty("X-Emby-Authorization", "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.83.0\"")
connection.setRequestProperty("X-Emby-Authorization", "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.83.1\"")
request.token?.let { connection.setRequestProperty("X-Emby-Token", it) }
if (request.method == "POST") {
val bytes = request.body.orEmpty().toByteArray(Charsets.UTF_8)
@@ -33,7 +33,7 @@ internal class SecureMediaDataSource(private val session: EmbySampleSession) : B
next.instanceFollowRedirects = false
next.connectTimeout = 10_000
next.readTimeout = 20_000
next.setRequestProperty("User-Agent", "OrbitTV/0.83.0")
next.setRequestProperty("User-Agent", "OrbitTV/0.83.1")
next.setRequestProperty("Accept-Encoding", "identity")
session.mediaHeaders(address).forEach { (key, value) -> next.setRequestProperty(key, value) }
if (dataSpec.position != 0L || dataSpec.length != C.LENGTH_UNSET.toLong()) {