Year and media type checked alongside the title on every repair and match (build 66)
release-gate / verify (push) Successful in 10m9s
pauljd/davidson-ci: Davidson CI / quality (push) Failing after 2m54s
pauljd/davidson-ci: Davidson CI / ai-review (push) Failing after 3m29s

Paul, 18 November 2026: verify the year and media type too, especially for remakes. A provider-ID repair is refused
when its year differs from the approval's by more than one (release dates over New Year), whatever the name; with no
year, the name must fit; two works under one ID need the exact year. A saved film ID now holding a remake isn't trusted.
The server move does the same, and a film never matches a series of the same name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-18 15:04:18 +00:00
co-authored by Claude Opus 5.5
parent 224b10499a
commit fb6f68258d
5 changed files with 64 additions and 10 deletions
@@ -474,7 +474,7 @@ internal class EmbySampleSession(private val transport: EmbyTransport, private v
// A placeholder from a server move has no item to ask for: it goes straight to its provider ID.
var found = if (accepted.sourceId != null && accepted.durationMs != null) accepted.sourceId!! to accepted.durationMs!!
else if (accepted.id.startsWith(MOVED_PREFIX)) null
else { requests++; playableFilm(accepted.id, programming.englishAudio, accepted.title) }
else { requests++; playableFilm(accepted.id, programming.englishAudio, accepted.title, accepted.year) }
// Gone (renumbered, or moved from another server): found again by its provider ID, one request (18 November 2026).
if (found == null && accepted.ref.isNotEmpty()) {
// A repair kept from before is tried first; a stale one is dropped and looked up afresh, once.
@@ -485,7 +485,7 @@ internal class EmbySampleSession(private val transport: EmbyTransport, private v
} ?: break
if (id == accepted.id) break
requests++
found = playableFilm(id, programming.englishAudio, accepted.title)
found = playableFilm(id, programming.englishAudio, accepted.title, accepted.year)
if (found != null) { refound[accepted.id] = id; accepted = accepted.copy(id = id, sourceId = null, durationMs = null); break }
IdRepairs.forget("Movie", accepted.ref)
}
@@ -524,15 +524,17 @@ internal class EmbySampleSession(private val transport: EmbyTransport, private v
}
/** One film's first copy this box can play, as (source id, runtime), by its exact library item; null if it has none. */
private fun playableFilm(id: String, englishAudio: Boolean, title: String? = null): Pair<String, Long>? {
private fun playableFilm(id: String, englishAudio: Boolean, title: String? = null, year: Int? = null): Pair<String, Long>? {
if (!EmbyClient.validId(id)) return null
val reply = runCatching { transport.execute(EmbyRequest("GET", "/Items?UserId=$userId&Ids=$id&Fields=MediaSources&EnableImages=false&EnableUserData=false",
val reply = runCatching { transport.execute(EmbyRequest("GET", "/Items?UserId=$userId&Ids=$id&Fields=MediaSources,ProductionYear&EnableImages=false&EnableUserData=false",
token = currentToken(), readTimeoutMs = WHOLE_SERIES_TIMEOUT_MS)) }.getOrNull() ?: return null
if (reply.status !in 200..299) return null
val row = EmbyClient.parse(reply.body).optJSONArray("Items")?.optJSONObject(0)?.takeIf { it.optString("Id") == id && it.optString("Type") == "Movie" } ?: return null
// A renumbered server can give an old number to another film (18 November 2026): one whose name shares nothing with
// the approval's isn't it, and the approval is then looked up by its provider ID instead.
if (title != null && !sameFilmName(row.optString("Name"), title)) return null
// Nor one from another year: a remake under the same name (Paul, 18 November 2026).
if (year != null && row.optInt("ProductionYear", 0).let { it > 0 && kotlin.math.abs(it - year) > 1 }) return null
return playableSource(row, BoxDecoders::canDecode, englishAudio)
}
fun suggestFor(rules: List<SuggestionRule>, alreadyInPool: Set<String>, rejected: Set<String>, takenTitles: Set<String> = emptySet(), checkActive: () -> Unit = {}) =
@@ -15,7 +15,8 @@ import java.util.concurrent.ConcurrentHashMap
* - two lookups for the same title at once become one;
* - every lookup is an ordinary request, so it's paced and counted within the household budget like any other;
* - only what Orbit needs, when it needs it: a programme about to play, an approval its channel is reading. Never a sweep.
* Ambiguity is the caller's to refuse (see [pickByProvider]): a lookup that could be two different titles repairs nothing.
* Ambiguity is the caller's to refuse (see [pickByProvider]): a lookup that could be two different titles repairs nothing,
* and the media type, the year and the name are all checked, so a remake is never taken for the original.
*/
internal object IdRepairs {
const val FIRST_COOLDOWN_MS = 6 * 60 * 60_000L
@@ -93,13 +94,16 @@ internal fun pickByProvider(rows: List<JSONObject>, title: String, year: Int?):
val works = rows.groupBy { moveTitleKey(it.optString("Name")) + "|" + it.optInt("ProductionYear", 0).takeIf { y -> y > 0 } }
if (works.size > 1) {
// Different names or years under one ID: only the one that is plainly this title, if exactly one is.
val fitting = works.values.filter { group -> group.first().let { r -> nameFits(r.optString("Name"), title) && (year == null || r.optInt("ProductionYear", 0).let { it == 0 || it == year }) } }
val fitting = works.values.filter { group -> group.first().let { r -> nameFits(r.optString("Name"), title) && (year == null || r.optInt("ProductionYear", 0) == year) } }
return fitting.singleOrNull()?.first()
}
val r = rows.first()
val yearOk = year == null || r.optInt("ProductionYear", 0).let { it == 0 || kotlin.math.abs(it - year) <= 1 }
// The ID is the content's own; a renamed title is still it, unless its year says otherwise.
return r.takeIf { yearOk || nameFits(it.optString("Name"), title) }
// Year first (Paul, 18 November 2026: remakes share names): when both are known they must agree, give or take a year
// for release dates that straddle New Year, whatever the name says.
val found = r.optInt("ProductionYear", 0)
if (year != null && found > 0 && kotlin.math.abs(found - year) > 1) return null
// The ID is the content's own, so a renamed title is still it when the years agree; with no year to compare, the name must.
return r.takeIf { (year != null && found > 0) || nameFits(it.optString("Name"), title) }
}
private fun nameFits(a: String, b: String): Boolean {
@@ -123,7 +123,10 @@ internal class MoveMatcher(private val old: MoveCatalogue, private val new: Move
val byRef = MOVE_SOURCES.flatMap { s -> was.refs[s]?.let { new.byRef(type, s, it) }.orEmpty() }.distinctBy { it.id }
if (byRef.isNotEmpty()) {
// Each candidate shares an ID with the old title; one that contradicts another of its IDs is a different work.
val agreed = byRef.filter { c -> MOVE_SOURCES.none { s -> was.refs[s] != null && c.refs[s] != null && !c.refs[s].equals(was.refs[s], true) } }
// And the years agree when both are known (a year either side for release dates over New Year): a remake that
// somehow carries the original's ID is still a remake.
val agreed = byRef.filter { c -> MOVE_SOURCES.none { s -> was.refs[s] != null && c.refs[s] != null && !c.refs[s].equals(was.refs[s], true) } &&
(y == null || c.year == null || kotlin.math.abs(c.year - y) <= 1) }
return@getOrPut if (agreed.isNotEmpty()) MoveMatch(oldId, name, type, y, pick(agreed).id, MoveHow.Provider)
else MoveMatch(oldId, name, type, y, null, MoveHow.Ambiguous, byRef.distinctBy { "${it.title}|${it.year}" }.take(5))
}
@@ -66,3 +66,36 @@ class IdRepairsTest {
@Test fun aDifferentFilmWithAnotherNameAndYearIsRefused() =
assertNull(pickByProvider(listOf(row("a", "Krull", 1983)), "Tremors", 1990))
}
/** Year and type alongside the title (Paul, 18 November 2026), so a remake is never taken for the original. */
class RemakeGuardTest {
private fun row(id: String, name: String, year: Int? = null) = JSONObject().put("Id", id).put("Name", name).apply { year?.let { put("ProductionYear", it) } }
@Test fun aRemakeWithTheSameNameIsRefusedByItsYear() =
assertNull(pickByProvider(listOf(row("a", "The Blob", 1988)), "The Blob", 1958))
@Test fun aReleaseDateStraddlingNewYearStillCounts() =
assertEquals("a", pickByProvider(listOf(row("a", "Nosferatu the Vampyre", 1979)), "Nosferatu the Vampyre", 1978)?.optString("Id"))
@Test fun withNoYearToCompareTheNameMustFit() {
assertNull(pickByProvider(listOf(row("a", "Krull")), "Tremors", null))
assertEquals("a", pickByProvider(listOf(row("a", "Tremors")), "Tremors", null)?.optString("Id"))
}
@Test fun twoWorksUnderOneIdNeedTheExactYear() =
assertNull(pickByProvider(listOf(row("a", "Halloween", 1978), row("b", "Halloween", 2018)), "Halloween", 1979))
@Test fun aSavedFilmIdNowHoldingTheRemakeIsntTrusted() {
val transport = EmbyTransport { r ->
if ("Ids=f1" in r.path) EmbyReply(200, JSONObject().put("Items", org.json.JSONArray().put(row("f1", "The Fog", 2005).put("Type", "Movie")
.put("MediaSources", org.json.JSONArray().put(JSONObject().put("Id", "s1").put("Container", "mkv").put("RunTimeTicks", 90 * 60_000L * 10_000))))).toString())
else EmbyReply(200, JSONObject().put("Items", org.json.JSONArray()).toString())
}
EmbyServer.origin = "https://emby.example.com"; IdRepairs.resetForTest()
try {
val s = EmbySampleSession(transport, "fixture-token-private", "fixture-user", EmbySample(emptyList(), 0))
val read = s.readChannelPool(ChannelProgramming(305, "After Dark", emptyList(), filmRestMs = 1L), null, ChannelChoices(listOf(Suggestion("f1", "The Fog", 1980, "Movie", "", ""))))
assertTrue(read.series.isEmpty())
} finally { EmbyServer.origin = ""; IdRepairs.resetForTest() }
}
}
@@ -41,6 +41,18 @@ class ServerMoveTest {
assertNull(m.newId); assertEquals(MoveHow.Missing, m.how)
}
@Test fun aRemakeCarryingTheOriginalsIdIsStillNotTaken() {
val old = MemoryMoveCatalogue(sequenceOf(film("1", "The Fog", 1980, "imdb" to "tt0080749")))
val new = MemoryMoveCatalogue(sequenceOf(film("2", "The Fog", 2005, "imdb" to "tt0080749")))
assertNull(MoveMatcher(old, new).match("1", "The Fog", "Movie", 1980).newId)
}
@Test fun aFilmAndASeriesWithOneNameNeverMatchEachOther() {
val old = MemoryMoveCatalogue(sequenceOf(film("1", "Fargo", 1996)))
val new = MemoryMoveCatalogue(sequenceOf(show("2", "Fargo", 1996)))
assertNull(MoveMatcher(old, new).match("1", "Fargo", "Movie", 1996).newId)
}
@Test fun noIdsAtAllFallsBackToTitleAndYear() {
val old = MemoryMoveCatalogue(sequenceOf(film("1", "Robin Redbreast", 1970)))
val new = MemoryMoveCatalogue(sequenceOf(film("2", "Robin Redbreast (1970)", 1970), film("3", "Robin Redbreast", 2020)))