Cutover pre-checks: PPV date, Joe Bob, Drive-In, broadcast vs viewing, immediate rollback and recovery after airing (simulated)
pauljd/davidson-ci: Davidson CI / quality (push) Failing after 1m46s
pauljd/davidson-ci: Davidson CI / ai-review (push) Failing after 1m24s

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-04 11:16:33 +00:00
co-authored by Claude Opus 5.5
parent da37a8039c
commit d384dcff41
4 changed files with 281 additions and 1 deletions
@@ -24,6 +24,7 @@ class CutoverSimulationTest {
private val dir = System.getenv("ORBIT_REPLAY_DIR")?.let(::File)
private val zone = java.time.ZoneId.of("Europe/London")
private val day = 24 * 60 * 60_000L
private fun local(ms: Long) = java.time.Instant.ofEpochMilli(ms).atZone(zone)
@Test fun cutover() {
assumeTrue(dir != null && dir.isDirectory)
@@ -72,6 +73,7 @@ class CutoverSimulationTest {
md.appendLine("| Ch | Channel | Kept (started by T) | On air at T kept | Unaired bookings dropped | New bookings | New plan ends | Seam | Places corrected |")
md.appendLine("|---|---|---|---|---|---|---|---|---|")
var problems = 0
val rollbackWindow = HashMap<Int, Long>()
val outDir = System.getenv("ORBIT_CUTOVER_OUT")?.let(::File)?.also { it.mkdirs() }
val out = outDir?.let(::ScheduleStore)
for (p in channels) {
@@ -82,7 +84,18 @@ class CutoverSimulationTest {
val new = after.entries.filter { it.startMs > at }
val seamOk = new.isEmpty() || k.entries.isEmpty() || new.first().startMs == k.entries.last().endMs
val gaps = after.entries.zipWithNext().count { (a, b) -> a.endMs != b.startMs }
if (!keptOk || !seamOk || gaps > 0) problems++
// Episode order from the seam on: every new booking follows its show's last one, kept or new, in order or along its
// deck. (Bookings before T are what they are: some were made before a show was set to mix.)
val pool = pools.getValue(p.number)
val orderBreaks = after.entries.groupBy { it.seriesId }.entries.sumOf { (id, es) ->
val s = pool.firstOrNull { it.id == id } ?: return@sumOf 0
if (s.isFilm) 0 else {
val running = if (p.shuffleEpisodes || p.mixes(s)) shuffledEpisodes(s).map { it.mark } else s.episodes.map { it.mark }
es.zipWithNext().count { (x, y) -> y.startMs > at && running[(running.indexOf(x.mark) + 1) % running.size] != y.mark && y.mark != running.first() }
}
}
if (!keptOk || !seamOk || gaps > 0 || orderBreaks > 0) problems++
rollbackWindow[p.number] = (onAir?.endMs ?: at) - at
md.appendLine("| ${p.number} | ${p.name} | ${k.entries.size} | ${if (onAir != null) "yes" else "none on air"} | ${before.entries.count { it.startMs > at }} | ${new.size} | " +
"${java.time.Instant.ofEpochMilli(after.endMs).atZone(zone).toLocalDate()} | ${if (seamOk && gaps == 0) "no gap" else "GAP $gaps"} | ${corrected[p.number]} |")
out?.savePlan(user, p.number, after)
@@ -97,6 +110,41 @@ class CutoverSimulationTest {
md.appendLine("- New bookings carrying a presentation key (special idents): $keyed.")
md.appendLine("- \"Places corrected\" counts shows whose old plan tail had moved past episodes that were only booked, not aired; after the cutover their place is what actually aired.")
md.appendLine("- Drive-In plan, tickets, choices, failed files and pools: not touched.")
md.appendLine("- Episode order across the seam: in-order shows never step back and shuffled shows follow their deck on every channel (checked).")
md.appendLine("- Immediate rollback: restoring the snapshot is seamless while each channel's on-air programme is still playing, because the old and new plans are identical up to its end. That window is ${rollbackWindow.values.min() / 60_000} to ${rollbackWindow.values.max() / 60_000} minutes after T, depending on the channel.")
// Every keyed block and event, and the appointments, on the rebooked network.
md.appendLine().appendLine("## Presentation keys and appointments in the rebooked week").appendLine()
for (p in channels) for (part in p.dayParts.filter { it.key.isNotEmpty() || it.onTime }) {
val es = newOnly.getValue(p.number).filter { e -> dayPartFor(pools.getValue(p.number), listOf(part), e.startMs, zone).third == part && (part.key.isEmpty() || e.presentation == part.key) }
val nights = es.groupBy { local(it.startMs).let { l -> if (l.hour < part.fromHour && part.fromHour > part.toHour) l.toLocalDate().minusDays(1) else l.toLocalDate() } }
val lates = nights.map { (d, x) -> (x.minOf { it.startMs } - d.atTime(part.fromHour, 0).atZone(zone).toInstant().toEpochMilli()) / 60_000 }.sorted()
md.appendLine("- ${p.name} ${part.key.ifEmpty { "event ${part.fromHour}-${part.toHour}" }}${if (part.onTime) " (appointment ${part.fromHour}:00)" else ""}: ${es.size} bookings with its key on ${nights.size} occasions" +
(if (lates.isNotEmpty()) "; first programme ${lates.joinToString()} min after the hour" else ""))
}
// PPV Sunday and Joe Bob Saturday in detail.
md.appendLine().appendLine("## PPV Sunday and Joe Bob Saturday").appendLine()
fun detail(n: Int, key: String) = newOnly[n].orEmpty().filter { it.presentation == key && (n != 401 || it.media.item.type == "Movie") }.joinToString("\n") { e ->
val fileLength = pools.getValue(n).flatMap { it.episodes }.firstOrNull { it.id == e.media.item.id }?.durationMs
"- ${local(e.startMs).toLocalDateTime().withSecond(0).withNano(0)} to ${local(e.endMs).toLocalDateTime().withSecond(0).withNano(0)}: ${e.media.item.title} (${e.media.durationMs / 60_000} min, the whole file: ${fileLength == e.media.durationMs}) [${e.why.take(120)}]"
}
md.appendLine("PPV Sunday (Ring X, ringx.ppv):").appendLine(detail(401, "ringx.ppv").ifEmpty { "- none in the week" })
md.appendLine("PPV order: ${PpvSunday.take(3).joinToString()} ... (none has aired yet: ${old.getValue(401).entries.none { it.presentation == "ringx.ppv" && it.startMs <= at }})")
md.appendLine("Joe Bob Saturday (After Dark, afterdark.drivein):").appendLine(detail(AFTER_DARK_NUMBER, "afterdark.drivein").ifEmpty { "- none in the week" })
val joeBobAired = progress[AFTER_DARK_NUMBER]?.aired?.marks?.let { m -> pools.getValue(AFTER_DARK_NUMBER).firstOrNull { it.title == LAST_DRIVE_IN }?.let { m[it.id] } }
md.appendLine("Joe Bob's last aired episode before T: ${joeBobAired ?: "none yet"}; the rebooked episode follows it in order.")
// The Drive-In stays outside the migration.
md.appendLine().appendLine("## The Drive-In").appendLine()
val tickets = source.loadTickets(user)
md.appendLine("- Its plan is read only to avoid clashes, never rewritten: ${driveIn?.entries?.size ?: 0} programmes, unchanged.")
md.appendLine("- Tickets: ${tickets.size} saved, ${tickets.keys.count { it > at }} for screenings after T, each still in the Drive-In plan: ${tickets.keys.filter { it > at }.all { t -> driveIn?.entries?.any { it.startMs == t } == true }}. The tickets file isn't touched.")
md.appendLine("- New linear bookings on another channel at the same time as a Drive-In film: ${newOnly.values.flatten().count { e -> driveIn?.entries?.any { d -> seriesKey(d.media.item.title) == seriesKey(e.media.item.title.substringBefore(" / ")) && d.startMs < e.endMs && e.startMs < d.endMs } == true }}.")
// Broadcast record and viewing kept apart.
val notes = File(dir, "playback-notes.txt").takeIf { it.exists() }?.readLines().orEmpty()
val watchedStarts = notes.mapNotNull { Regex("channel=(\\d+) programme start=(\\d+)").find(it)?.let { m -> m.groupValues[1].toInt() to m.groupValues[2].toLong() } }.toSet()
val keptAll = kept.flatMap { (n, k) -> k.entries.map { n to it.startMs } }
md.appendLine().appendLine("## Broadcast record and viewing").appendLine()
md.appendLine("- Episode, shuffle and film positions are rebuilt from the broadcast record (the aired cursor plus programmes started by T), whether or not anyone watched: ${keptAll.size} broadcasts kept, of which ${keptAll.count { it in watchedStarts }} were also tuned to on the box according to its playback notes.")
md.appendLine("- Viewing history (playback-notes.txt, ${watchedStarts.size} programmes tuned to) is never used to move a position and is left untouched.")
File(dir, "cutover-simulation.md").writeText(md.toString())
println(md)
assertEquals(0, problems); assertEquals(0, clashes)