Quiet first week on a new server: background 100 an hour, no collections or evidence reads
release-gate / verify (push) Successful in 9m41s
pauljd/davidson-ci: Davidson CI / quality (push) Failing after 3m45s
pauljd/davidson-ci: Davidson CI / ai-review (push) Failing after 3m13s

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-12 08:22:13 +00:00
co-authored by Claude Opus 5.5
parent 0beee1d8a2
commit e9f2d267f6
5 changed files with 35 additions and 4 deletions
@@ -432,6 +432,8 @@ internal fun EmbyConnectionScreen(modifier: Modifier = Modifier, visible: Boolea
while (true) {
// Refreshed each round, so it's there once the catalogue finishes its first full read (0.129 audit).
withContext(Dispatchers.IO) { active.libraryTitles = runCatching { catalogue.libraryTitles(active.accountId) }.getOrNull() }
val quiet = withContext(Dispatchers.IO) { workspace.quietStart() }
BackgroundPacing.budget = if (quiet) QUIET_START_BUDGET else BackgroundPacing.HOURLY_BUDGET
for (programming in SmartChannels) {
val loop = smartLoop(programming) ?: run {
// A channel of your own that is switched on but whose first plan never finished (an update arrived mid-read,
@@ -458,7 +460,7 @@ internal fun EmbyConnectionScreen(modifier: Modifier = Modifier, visible: Boolea
catch (cancelled: CancellationException) { throw cancelled }
catch (_: Exception) { /* Tried again next hour. */ }
// Stage C's light daily library refresh, in the background. A failure changes nothing and is tried next hour.
try { withContext(Dispatchers.IO) { BackgroundPacing.run { scheduler.readEvidence(active, active.accountId, catalogue)
if (!quiet) try { withContext(Dispatchers.IO) { BackgroundPacing.run { scheduler.readEvidence(active, active.accountId, catalogue)
runCatching { scheduler.eventIdeas(active, active.accountId, catalogue) }.onFailure { android.util.Log.w("OrbitSchedule", "event ideas failed=${it.javaClass.simpleName}") }
directorReports(scheduleStore, catalogue, active.accountId) } } }
catch (cancelled: CancellationException) { throw cancelled }
@@ -105,6 +105,8 @@ internal object BackgroundPacing {
const val GAP_MS = 1_500L
/** Background requests per TV in any 60 minutes (Paul: never thousands an hour again; 200 since Orbit was banned from a share, 12 November 2026). */
const val HOURLY_BUDGET = 200
/** The budget in force: [HOURLY_BUDGET], or less during a new server's quiet start ([ServerWorkspace.quietStart]). */
@Volatile var budget = HOURLY_BUDGET
/**
* Every request this TV makes, playback included, in any 60 minutes (12 November 2026). Watching is never stopped, but
* once the TV reaches this, background work stops until the hour has room again, and Settings says so.
@@ -162,12 +164,12 @@ internal object BackgroundPacing {
val wait = lastMs + GAP_MS - now
if (wait > 0) { sleep(wait); now = clock() }
while (sent.isNotEmpty() && sent.first() <= now - 60 * 60_000L) sent.removeFirst()
if (sent.size >= HOURLY_BUDGET) throw PacedOut()
if (sent.size >= budget) throw PacedOut()
if (ServerRequests.inLastHour() >= TOTAL_CEILING) { ceilingHitMs = now; throw PacedOut() }
lastMs = now; sent.addLast(now)
}
}
internal fun resetForTest() { lastMs = 0; lastSetupMs = 0; sent.clear(); ceilingHitMs = null; clock = System::currentTimeMillis; sleep = { Thread.sleep(it) } }
internal fun resetForTest() { lastMs = 0; lastSetupMs = 0; sent.clear(); ceilingHitMs = null; budget = HOURLY_BUDGET; clock = System::currentTimeMillis; sleep = { Thread.sleep(it) } }
@Synchronized internal fun usedThisHour(): Int { val now = clock(); return sent.count { it > now - 60 * 60_000L } }
}
@@ -663,4 +663,5 @@ internal fun SettingsSubPage(first: FocusRequester?, onBack: (() -> Unit)?, modi
}
/** How the background budget reads in Settings. */
internal val BACKGROUND_BUDGET_LINE = "at most ${BackgroundPacing.HOURLY_BUDGET} an hour, one at a time"
internal val BACKGROUND_BUDGET_LINE get() = "at most ${BackgroundPacing.budget} an hour, one at a time" +
if (BackgroundPacing.budget < BackgroundPacing.HOURLY_BUDGET) " (a quiet first week on this server: collections aren't read yet)" else ""
@@ -35,8 +35,21 @@ internal class ServerWorkspace(val origin: String, val root: File, val artworkDi
fun close() { if (catalogueOnce.isInitialized()) runCatching { catalogue.close() } }
val schedules: ScheduleStore by lazy { scheduleDir.let { migrateChannelNumbers(it); ScheduleStore(it) } }
internal var appContext: Context? = null
/**
* Quiet start (12 November 2026, after a ban): for the first [QUIET_START_MS] on a server, Orbit only does what playing
* its channels needs. Background reads keep to [QUIET_START_BUDGET] an hour, and the collections and the Chair's evidence
* aren't read at all. The clock starts the first time this box uses the server.
*/
fun quietStart(nowMs: Long = System.currentTimeMillis()): Boolean {
val stamp = File(root, "first-used")
val first = runCatching { stamp.readText().trim().toLong() }.getOrNull() ?: nowMs.also { runCatching { root.mkdirs(); stamp.writeText(it.toString()) } }
return nowMs - first < QUIET_START_MS
}
}
internal const val QUIET_START_MS = 7 * 24 * 60 * 60_000L
internal const val QUIET_START_BUDGET = 100
internal object Workspaces {
/** The original server's key alias, as it always was. Others get their own, so removing one never touches another. */
private const val ORIGINAL_ALIAS = "orbit-connection-key-v1"
@@ -45,4 +45,17 @@ class SafetyLimitsTest {
// The rest begins exactly ten minutes after the last press (plus the same small margin as a programme's end).
assertEquals(GUIDE_PICTURE_REST_MS + 50, programmeEndDelayMs(1_000L + GUIDE_PICTURE_REST_MS, 1_000L))
}
@Test fun aNewServerGetsAQuietFirstWeek() {
val root = kotlin.io.path.createTempDirectory("quiet").toFile()
val w = ServerWorkspace("http://example.test", root, root, "alias", original = false)
assertTrue(w.quietStart(now)) // first use starts the clock
assertTrue(w.quietStart(now + 6 * 24 * 60 * 60_000L))
assertFalse(w.quietStart(now + QUIET_START_MS))
// The budget in force is what the limiter keeps to.
BackgroundPacing.budget = QUIET_START_BUDGET
var made = 0
while (background()) made++
assertEquals(100, made)
}
}