Rest blocks that share most of their films together (no change to today's channels); Option C verified proposal
pauljd/davidson-ci: Davidson CI / quality (push) Failing after 2m4s
pauljd/davidson-ci: Davidson CI / ai-review (push) Failing after 1m51s

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-04 10:18:13 +00:00
co-authored by Claude Opus 5.5
parent 5f7018354c
commit 036b82b78a
5 changed files with 121 additions and 4 deletions
@@ -364,6 +364,7 @@ internal fun extendFilmSchedule(pool: List<PoolSeries>, cursor: ScheduleCursor,
var last = cursor.lastSeriesId
var start = fromMs
val marks = cursor.marks.toMutableMap()
val blockRest = HashMap<DayPart?, Long>()
while (start < untilMs) {
val (available, block, part) = dayPartFor(pool, dayParts, start, zone)
// An event naming only a series plays its next whole episode, once a night. Everywhere else is films only.
@@ -380,7 +381,7 @@ internal fun extendFilmSchedule(pool: List<PoolSeries>, cursor: ScheduleCursor,
}
val films = available.filter { it.isFilm }.ifEmpty { pool.filter { it.isFilm } }.ifEmpty { available }
// Events keep the channel's rest; everyday blocks, when adaptive, rest by how long their own films last.
val rest = if (adaptiveRest && part?.days.isNullOrEmpty()) adaptiveRestMs(films, part, restMs) else restMs
val rest = if (adaptiveRest && part?.days.isNullOrEmpty()) blockRest.getOrPut(part) { sharedAdaptiveRestMs(pool, dayParts, part, restMs) } else restMs
val (film, why) = chooseFilm(films, shown, start, rest, avoid)
val copy = film.episodes.first()
val item = SampleItem(film.title, "Movie", copy.durationMs / 60_000, "", copy.id, copy.durationMs)
@@ -405,13 +406,46 @@ internal const val ADAPTIVE_REST_CEILING_MS = 14 * 24 * 60 * 60_000L
* pick comes from a real shortlist instead of the same longest-waiting order every cycle. When it's longer, up to
* [ADAPTIVE_REST_LONG_SHARE] of it but never less than [restMs], so a big pool airs more of itself.
*/
internal fun adaptiveRestMs(films: List<PoolSeries>, part: DayPart?, restMs: Long): Long {
val hours = part?.let { ((it.toHour - it.fromHour + 24) % 24).let { h -> if (h == 0) 24 else h } } ?: 24
internal fun adaptiveRestMs(films: List<PoolSeries>, part: DayPart?, restMs: Long): Long = adaptiveRestMs(films, hoursOf(part), restMs)
private fun hoursOf(part: DayPart?) = part?.let { ((it.toHour - it.fromHour + 24) % 24).let { h -> if (h == 0) 24 else h } } ?: 24
private fun adaptiveRestMs(films: List<PoolSeries>, hours: Int, restMs: Long): Long {
val cycle = films.sumOf { it.episodes.first().durationMs }.toDouble() * 24 / hours
val rest = if (cycle * ADAPTIVE_REST_SHORT_SHARE < restMs) cycle * ADAPTIVE_REST_SHORT_SHARE else maxOf(restMs.toDouble(), cycle * ADAPTIVE_REST_LONG_SHARE)
return rest.toLong().coerceIn(ADAPTIVE_REST_FLOOR_MS, ADAPTIVE_REST_CEILING_MS)
}
/** The films an everyday block may play, as [dayPartFor] chooses them, without its fallbacks. */
private fun filmsIn(pool: List<PoolSeries>, part: DayPart): List<PoolSeries> {
if (part.titles.isEmpty() && part.group.isEmpty()) return pool.filter { it.isFilm }
val titles = part.titles.map(::seriesKey).toSet()
val films = part.titles.map { seriesKey(FilmIdentity.of(it).title) }.toSet()
return pool.filter { it.isFilm && (seriesKey(it.title) in titles || seriesKey(it.title) in films || (part.group.isNotEmpty() && it.group == part.group)) }
}
/**
* [adaptiveRestMs] for an everyday block that shares most of its films with another everyday block (each holding at least
* half the other's films). A film rests across every block it plays in, so such a block's films are rested by how many
* hours a day all the everyday blocks draw on them, each block in proportion to how much of its pool they make up, and
* never longer than the block alone would rest them. Without this, a short block sharing most of its films with a long one
* keeps a rest worked out from its own few hours and falls back to the longest-waiting order (After Dark's 18:00 to 21:00
* in Option C). A block with no such partner is rested on its own exactly as before.
*/
internal fun sharedAdaptiveRestMs(pool: List<PoolSeries>, dayParts: List<DayPart>, part: DayPart?, restMs: Long): Long {
if (part == null) return adaptiveRestMs(pool.filter { it.isFilm }, null, restMs)
val own = filmsIn(pool, part).ifEmpty { pool.filter { it.isFilm } }
val alone = adaptiveRestMs(own, part, restMs)
val ids = own.mapTo(HashSet()) { it.id }
val everyday = dayParts.filter { it.days.isEmpty() }.map { it to filmsIn(pool, it) }.filter { it.second.isNotEmpty() }
val partnered = everyday.any { (other, films) -> other != part && films.count { it.id in ids }.let { shared -> shared * 2 >= own.size && shared * 2 >= films.size } }
if (!partnered) return alone
val demandHours = everyday.sumOf { (other, films) -> hoursOf(other) * films.count { it.id in ids }.toDouble() / films.size }
val cycle = own.sumOf { it.episodes.first().durationMs }.toDouble() * 24 / demandHours
val shared = if (cycle * ADAPTIVE_REST_SHORT_SHARE < restMs) cycle * ADAPTIVE_REST_SHORT_SHARE else maxOf(restMs.toDouble(), cycle * ADAPTIVE_REST_LONG_SHARE)
return minOf(alone, shared.toLong().coerceIn(ADAPTIVE_REST_FLOOR_MS, ADAPTIVE_REST_CEILING_MS))
}
/** A one-a-night event counts as having had its turn for this long after a programme from it starts. */
private const val ONCE_A_NIGHT_MS = 12 * 60 * 60_000L
/** How far ahead an on-time appointment is looked for, and how late after its hour it may start. */