From 3e9109cc5aea9065e135932b9f474083ef8805db Mon Sep 17 00:00:00 2001 From: Pauljd Date: Wed, 4 Nov 2026 10:24:03 +0000 Subject: [PATCH] Coordinated cutover plan and simulation on box copies (nothing applied) Co-Authored-By: Claude Opus 5.5 (1M context) --- .../tv/orbit/classic/CutoverSimulationTest.kt | 104 ++++++++++++++++++ docs/cutover-plan.md | 67 +++++++++++ docs/replays/cutover-simulation-2026-11-04.md | 41 +++++++ 3 files changed, 212 insertions(+) create mode 100644 android/app/src/test/java/tv/orbit/classic/CutoverSimulationTest.kt create mode 100644 docs/cutover-plan.md create mode 100644 docs/replays/cutover-simulation-2026-11-04.md diff --git a/android/app/src/test/java/tv/orbit/classic/CutoverSimulationTest.kt b/android/app/src/test/java/tv/orbit/classic/CutoverSimulationTest.kt new file mode 100644 index 00000000..0c652fff --- /dev/null +++ b/android/app/src/test/java/tv/orbit/classic/CutoverSimulationTest.kt @@ -0,0 +1,104 @@ +package tv.orbit.classic + +import org.json.JSONObject +import org.junit.Assert.assertEquals +import org.junit.Assume.assumeTrue +import org.junit.Test +import java.io.File + +/** + * The coordinated cutover, simulated on copies of the box's files (4 November 2026). Nothing touches the box: + * ORBIT_REPLAY_DIR= ORBIT_CUTOVER_AT= ORBIT_CUTOVER_OUT= ./gradlew :app:testDebugUnitTest --tests '*CutoverSimulationTest*' + * + * At the cutover moment T, for every scheduled channel: + * 1. Keep everything that has started by T, including the programme on air, so nothing playing is interrupted. + * 2. Drop the bookings that haven't started. They never aired, so they must not count: the channel's place is rebuilt + * from what has actually gone out (its saved aired cursor, brought up to T with the kept programmes), never from the + * old plan's tail, which had already moved past the unaired bookings. + * 3. Rebook seven days from the end of the programme on air, with today's rules, every channel in turn avoiding the + * others (all cut back first, so nothing avoids a booking that is about to disappear). + * The Drive-In, its tickets, suggestion choices, failed files and pools are left exactly as they are. + * Writes the new plan and progress files to ORBIT_CUTOVER_OUT and cutover-simulation.md to the replay folder. + */ +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 + + @Test fun cutover() { + assumeTrue(dir != null && dir.isDirectory) + val source = ScheduleStore(dir!!) + val user = JSONObject(File(dir, "schedule-101.json").readText()).getString("user") + val channels = SmartChannels.filter { source.loadPlan(user, it.number) != null && source.loadPool(user, it.number) != null }.sortedBy { it.number } + val at = System.getenv("ORBIT_CUTOVER_AT")?.toLong() ?: channels.mapNotNull { source.loadPlan(user, it.number)?.entries?.firstOrNull()?.startMs }.max() + SCHEDULE_KEEP_PAST_MS + 60 * 60_000L + val pools = channels.associate { it.number to withoutFailed(source.loadPool(user, it.number)!!.series, source.loadFailed(user)) } + val old = channels.associate { it.number to source.loadPlan(user, it.number)!! } + val progress = channels.associate { it.number to source.loadProgress(user, it.number) } + val driveIn = source.loadPlan(user, DRIVE_IN_NUMBER) + + // 1 and 2: cut every plan back to what has started by T, and rebuild its place from what has gone out. + val kept = HashMap() + val corrected = HashMap() + for (p in channels) { + val plan = old.getValue(p.number); val pool = pools.getValue(p.number) + val keep = plan.entries.filter { it.startMs <= at } + val base = progress[p.number]?.aired ?: ScheduleCursor() + val aired = airedCursor(base, keep, at) + // The last show in each block, and when each series last started its run, from the kept programmes only. + val blockLast = aired.blockLast.toMutableMap() + val runStarted = HashMap() + keep.forEach { e -> + blockLast[dayPartFor(pool, p.dayParts, e.startMs, zone).second] = e.seriesId + pool.firstOrNull { it.id == e.seriesId }?.let { s -> + val running = if (p.shuffleEpisodes || p.mixes(s)) shuffledEpisodes(s) else s.episodes + if (running.firstOrNull()?.mark == e.mark) runStarted[s.id] = e.startMs + } + } + // Tallies start level: they only matter relative to each other, and the old ones counted bookings that never aired. + val cursor = aired.copy(blockLast = blockLast, tallies = emptyMap(), runStarted = runStarted) + corrected[p.number] = plan.tail.marks.count { (id, mark) -> cursor.marks[id] != mark } + kept[p.number] = ChannelPlan(keep, cursor) + } + // 3: rebook every channel in turn, each avoiding the others as they now stand. + val rebooked = HashMap(kept) + for (p in channels) { + val others = clashesWith(rebooked.filterKeys { it != p.number }.values.toList() + listOfNotNull(driveIn)) + val k = kept.getValue(p.number) + rebooked[p.number] = topUpPlan(k, k.tail, pools.getValue(p.number), at, p.filmRestMs, p.finishTogether, p.dayParts, zone, others, + shuffle = p.shuffleEpisodes, mixed = p::mixes, weightOf = p.tierWeights?.let { p::weightOf }, adaptiveRest = p.adaptiveFilmRest) ?: k + } + + val md = StringBuilder("# Coordinated cutover: simulation on box copies\n\nCutover moment T = ${java.time.Instant.ofEpochMilli(at).atZone(zone).toLocalDateTime().withNano(0)} (London).\n\n") + 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 outDir = System.getenv("ORBIT_CUTOVER_OUT")?.let(::File)?.also { it.mkdirs() } + val out = outDir?.let(::ScheduleStore) + for (p in channels) { + val before = old.getValue(p.number); val k = kept.getValue(p.number); val after = rebooked.getValue(p.number) + val onAir = before.entries.firstOrNull { it.startMs <= at && at < it.endMs } + val keptInAfter = after.entries.take(after.entries.indexOfFirst { it.startMs > at }.let { if (it < 0) after.entries.size else it }) + val keptOk = k.entries.dropWhile { it.endMs < at - SCHEDULE_KEEP_PAST_MS } == keptInAfter + 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++ + 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) + out?.saveProgress(user, p.number, ScheduleStore.SavedProgress(progress[p.number]?.enabled ?: true, airedCursor(progress[p.number]?.aired ?: ScheduleCursor(), k.entries, at))) + } + // The rebooked network, checked together. + val newOnly = rebooked.mapValues { (_, plan) -> plan.entries.filter { it.startMs > at } } + val byTitle = newOnly.flatMap { (n, es) -> es.map { n to it } }.groupBy { seriesKey(it.second.media.item.title.substringBefore(" / ")) } + val clashes = byTitle.values.sumOf { list -> list.indices.sumOf { i -> list.drop(i + 1).count { b -> b.first != list[i].first && list[i].second.startMs < b.second.endMs && b.second.startMs < list[i].second.endMs } } } + val keyed = newOnly.values.sumOf { es -> es.count { it.presentation.isNotEmpty() } } + md.appendLine().appendLine("- New bookings clashing with another channel: $clashes.") + 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.") + File(dir, "cutover-simulation.md").writeText(md.toString()) + println(md) + assertEquals(0, problems); assertEquals(0, clashes) + } +} diff --git a/docs/cutover-plan.md b/docs/cutover-plan.md new file mode 100644 index 00000000..0e2fa9fd --- /dev/null +++ b/docs/cutover-plan.md @@ -0,0 +1,67 @@ +# Coordinated cutover: the exact plan + +This is a proposal from 4 November 2026. **Nothing here has been done. The cutover runs only on Paul's explicit approval of this plan.** + +## Aim + +Launch the finished, curated Orbit network everywhere at once, instead of letting each channel's old committed week run out until about 10 November. The cutover rewrites, once, every scheduled channel's bookings that haven't started yet. After that, the normal rule returns: committed bookings are never rewritten. + +## What it does, per channel + +At the cutover moment T, for all 30 scheduled channels: + +1. **Keep** everything that has started by T, including the programme on air. Nothing playing is interrupted. +2. **Drop** the bookings that haven't started. They never aired, so they don't count. Each channel's place, meaning its episode positions, shuffle-deck positions and which films it has shown, is rebuilt from what has actually gone out: its saved aired cursor, brought up to T with the kept programmes. It isn't taken from the old plan's tail, which had already moved past the unaired bookings. + - Weighted tallies start level, because the old ones counted bookings that never aired. + - Run-restart times and each block's last show are rebuilt from the kept programmes. +3. **Rebook** seven days from the end of the programme on air, with today's rules. All channels are cut back first, then rebooked in turn, each avoiding every other channel, so nothing avoids a booking that's about to disappear. + +**Not touched:** the Drive-In's plan and tickets, suggestion choices, failed-file records, episode pools, settings and playback notes. + +## Impact (simulation on box copies from 4 November 10:05) + +Full table: docs/replays/cutover-simulation-2026-11-04.md (CutoverSimulationTest). The simulation used T = 4 November 13:00. + +- **Programmes kept:** 1,330 that had started by T, including the programme on air on every channel. +- **Unaired bookings dropped:** about 7,200, across all 30 channels. +- **New bookings:** about 9,200, to 11 November. No gaps at any seam, and **no clashes** between channels. +- **Places corrected:** about 600 shows across the series channels had an old plan tail that had moved past episodes that were only booked, not aired. After the cutover each resumes from what actually aired; Vortex alone has 81. Film channels rest from actual showings. +- **What arrives straight away:** + - **Presentation keys:** 282 new bookings carry one, so special idents start at once. Riot's blocks, After Dark's nights, Horror's Slasher Saturday, Gold's Sunday, Three's Scottish night, Ring X's nights and the rest no longer wait until 10 November. + - **Events the old committed week didn't have:** the first PPV Sunday (8 November 19:08, WrestleMania III) and Joe Bob Saturday on After Dark (7 November 21:19). + +## Procedure + +Each step happens only after Paul's approval, at a time when nothing is being watched. + +1. **Prerequisites:** + - The release with every approved change is installed. Today that would be 0.99.8, plus any of the audit findings Paul approves first. + - The simulation is re-run on fresh copies, and Paul approves the exact time. +2. **Stop Orbit** (`am force-stop`), so no top-up runs during the change. +3. **Full snapshot, three ways:** + - The whole `no_backup/schedule` folder, every file including the `.prev` copies, copied to this machine. + - The same folder copied on the box to `no_backup/schedule-rollback-