Repeat investigation for Rewind, Dark Files and Vortex (read-only); file rest is now a parameter, default unchanged
pauljd/davidson-ci: Davidson CI / quality (push) Failing after 12m53s
pauljd/davidson-ci: Davidson CI / ai-review (push) Failing after 1m34s

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-04 02:03:43 +00:00
co-authored by Claude Opus 5.5
parent e21c746a8e
commit 8eeeeeac49
4 changed files with 337 additions and 10 deletions
@@ -362,7 +362,9 @@ internal const val SAME_FILE_MS = 24 * 60 * 60_000L
internal fun extendWeighted(pool: List<PoolSeries>, cursor: ScheduleCursor, fromMs: Long, untilMs: Long, weightOf: (PoolSeries) -> Double,
dayParts: List<DayPart>, zone: java.time.ZoneId, avoid: ProgrammeClash, shuffle: Boolean, mixed: (PoolSeries) -> Boolean,
/** Files aired shortly before [fromMs], for the same-file guard across a top-up. */
recentFiles: Map<String, Long> = emptyMap()): ChannelPlan {
recentFiles: Map<String, Long> = emptyMap(),
/** How long a file rests before it may air again while another show could play. [SAME_FILE_MS] unless a channel asks for longer. */
fileRestMs: Long = SAME_FILE_MS): ChannelPlan {
val marks = cursor.marks.toMutableMap()
val shown = cursor.filmShown.toMutableMap()
val tallies = cursor.tallies.mapValues { it.value.toMutableMap() }.toMutableMap()
@@ -397,7 +399,7 @@ internal fun extendWeighted(pool: List<PoolSeries>, cursor: ScheduleCursor, from
val unclashed = available.filter { !avoid(it, start, start + following(it).durationMs) }
val clear = unclashed.ifEmpty { available }
val rules = listOf<(PoolSeries) -> Boolean>(
{ it.id != last }, { aired[following(it).id]?.let { at -> start - at < SAME_FILE_MS } != true }, { following(it).mark != running(it).first().mark || runStarted[it.id]?.let { at -> start - at < RUN_REST_MS } != true })
{ it.id != last }, { aired[following(it).id]?.let { at -> start - at < fileRestMs } != true }, { following(it).mark != running(it).first().mark || runStarted[it.id]?.let { at -> start - at < RUN_REST_MS } != true })
// Drop the softest rules first, only as far as needed to leave a choice.
val eligible = (rules.size downTo 0).asSequence().map { kept -> clear.filter { show -> rules.take(kept).all { it(show) } } }.first { it.isNotEmpty() }
// A show held back by the 24-hour or restart rule is brought up level, so it comes back to its share without a burst.
@@ -433,7 +435,7 @@ internal fun extendWeighted(pool: List<PoolSeries>, cursor: ScheduleCursor, from
*/
internal fun extendByBlock(pool: List<PoolSeries>, cursor: ScheduleCursor, fromMs: Long, untilMs: Long, weightOf: (PoolSeries) -> Double,
dayParts: List<DayPart>, zone: java.time.ZoneId, avoid: ProgrammeClash, shuffle: Boolean, mixed: (PoolSeries) -> Boolean, finishTogether: Boolean,
recentFiles: Map<String, Long> = emptyMap(), runBefore: Int = 0): ChannelPlan {
recentFiles: Map<String, Long> = emptyMap(), runBefore: Int = 0, fileRestMs: Long = SAME_FILE_MS): ChannelPlan {
fun weightedAt(at: Long) = dayPartFor(pool, dayParts, at, zone).third?.weighted != false
val hour = 60 * 60_000L
val entries = mutableListOf<ScheduleEntry>()
@@ -445,8 +447,8 @@ internal fun extendByBlock(pool: List<PoolSeries>, cursor: ScheduleCursor, fromM
var end = java.time.Instant.ofEpochMilli(start).atZone(zone).truncatedTo(java.time.temporal.ChronoUnit.HOURS).toInstant().toEpochMilli() + hour
while (end < untilMs && weightedAt(end) == weighted) end += hour
val stretch = if (weighted) {
val recent = recentFiles + entries.filter { it.startMs >= start - SAME_FILE_MS }.associate { it.media.item.id to it.startMs }
extendWeighted(pool, tail, start, minOf(end, untilMs), weightOf, dayParts, zone, avoid, shuffle, mixed, recent)
val recent = recentFiles + entries.filter { it.startMs >= start - fileRestMs }.associate { it.media.item.id to it.startMs }
extendWeighted(pool, tail, start, minOf(end, untilMs), weightOf, dayParts, zone, avoid, shuffle, mixed, recent, fileRestMs)
} else {
val trailing = entries.lastOrNull()?.let { t -> entries.asReversed().takeWhile { it.seriesId == t.seriesId }.size } ?: runBefore
extendSchedule(pool, tail, start, minOf(end, untilMs), null, finishTogether, dayParts, zone, avoid, shuffle, mixed, trailing).let { plan ->
@@ -607,15 +609,17 @@ internal fun topUpPlan(existing: ChannelPlan?, aired: ScheduleCursor, pool: List
/** Set for a channel with editorially weighted rotation ([ChannelProgramming.weightOf]); null keeps equal turns. */
weightOf: ((PoolSeries) -> Double)? = null,
/** Film channels only: see [ChannelProgramming.adaptiveFilmRest]. */
adaptiveRest: Boolean = false): ChannelPlan? {
adaptiveRest: Boolean = false,
/** Weighted channels only: see [extendWeighted]. */
fileRestMs: Long = SAME_FILE_MS): 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 trailing = kept?.entries?.lastOrNull()?.let { tail -> kept.entries.asReversed().takeWhile { it.seriesId == tail.seriesId }.size } ?: 0
val added = if (weightOf != null && filmRestMs == null) {
val recent = kept?.entries.orEmpty().filter { it.startMs >= from - SAME_FILE_MS }.associate { it.media.item.id to it.startMs }
extendByBlock(pool, existing?.tail ?: aired, from, nowMs + SCHEDULE_AHEAD_MS, weightOf, dayParts, zone, avoid, shuffle, mixed, finishTogether, recent, trailing)
val recent = kept?.entries.orEmpty().filter { it.startMs >= from - fileRestMs }.associate { it.media.item.id to it.startMs }
extendByBlock(pool, existing?.tail ?: aired, from, nowMs + SCHEDULE_AHEAD_MS, weightOf, dayParts, zone, avoid, shuffle, mixed, finishTogether, recent, trailing, fileRestMs)
} else extendSchedule(pool, existing?.tail ?: aired, from, nowMs + SCHEDULE_AHEAD_MS, filmRestMs, finishTogether, dayParts, zone, avoid, shuffle, mixed, trailing, noThirdInARow, blockRotation, adaptiveRest)
return ChannelPlan(kept?.entries.orEmpty() + added.entries, added.tail)
}