Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/agents/pre-pr-checklist.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,24 @@ New findings (section 1 candidates / product constraints):
- **WebView 不走应用代理,只认系统全局代理**:墙内网络下没有系统代理的盒子连授权页都打不开(ERR_CONNECTION_TIMED_OUT),OAuth 登录页加载完全不受 App 代理配置保护;只有 token 交换(OkHttp)走应用代理。面向墙内用户的登录方案必须把系统代理写进前置条件,或做二维码等不依赖 TV 出站的替代路径
- **bgm 验证码是自动化天敌**:可疑 IP(代理出口)下几乎必弹验证码,自动化连错 4 次后停手(锁号风险);TV 端遥控器输验证码同样痛苦。验证码环节建议只做人走查,不进自动化
- **Composed-path divergence(沿用 v2.16 条目)**:本次同样先 unit-test 后真机,设备行为与单测一致,未复现发散

### RSS 扫描卡死修复:离线 archive 按查询全量解析 + URL 噪音系列名(patch)

Own checks(HK1 真机证据 + 单测):

- [真机] ANi RSS 全量扫描冻在 55 文件/黑色五葉草约 15 分钟,取消近 10 分钟才生效;线程 CPU 时间静止(IO 阻塞非死循环);结束后 anime/episode/index_entry 全 0 行
- [真机] 设备 `MiruLog` 埋点钉死:`bangumi.archive.search` 单次耗时 **704993ms**(11.7 分钟),query 仅 6 字符(`A1%80%`);986MB `subject.jsonlines` 存在(当日 13:47 下载),前一日无 archive 时同款扫描 105 秒跑完
- 根因一定位就改小:自动扫描 enrichment 传 `includeArchive = false`(扫描恒有网,archive 只留给手动搜索);archive 本体改为流式单遍 + 有界 top-k + 每 4096 行 `ensureActive`(取消立刻生效);分类器对 URL 路径只用文件名做 PATH 证据、跳过 host/date/百分号段
- 单测:`searchAnime skips archive when includeArchive is false`(archive 有完美命中也被跳过,只走在线);`search honours coroutine cancellation`(100 万行文件 200ms 取消必抛);分类器 `remote URL path does not outvote filename title` + `encoded path segment is not treated as show context`;扫描单测断言 enrichment 全传 `false`
- 现有 archive 排序/匹配单测全过(流式 top-k 与全排序语义一致;季节降权只降不升,top-k 内重排等价)

Cross-checks:

- 手动搜索(`searchManualAnime`/`searchPreferredWith` 聚合)仍走 archive + 在线双路,行为不变;手动搜索 ViewModel 自带 35s/90s `withTimeout`,现在因 archive 循环可协作取消而真正有效
- `AniListScraper` 等其他 `MetadataScraper` 实现同步加参(忽略该标志,无行为变化);单测 fake 全量更新
- WebDAV/SMB 本地语义路径不受分类器改动影响(machinery 过滤只认 scheme/host/日期/百分号段;`Season N`/第 x 季等季节模式原样保留)
- full `./gradlew assembleDebug test lint` green

New failure class (section 1 candidate):

- **Offline-index-at-query-time** —— 离线 archive/索引文件在每次查询时全量解析 + 全排序,单测里几行 fixture 毫秒级全过,真机上 1GB 文件把 50MB 堆的 TV 盒子拖进 10 分钟 GC 泥潭。检查:任何“加载本地大文件再查询”的路径,必须验证真实量级文件在目标设备上的单次耗时;查询必须是流式 + 有界内存 + 可取消;耗时型预处理只做一次(构建期/下载期),绝不在查询热路径上
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,11 @@ class ScanCoordinator @Inject constructor(
"confidence_threshold" to RECOGNITION_CONFIDENCE_THRESHOLD.toString(),
)
)
var match = bangumi.searchByAlias(animeName, candidates).getOrNull()
// Auto-scan always has network (it just fetched the feed) and must stay
// responsive over dozens of series: the 1GB offline archive costs minutes
// per query on TV hardware (full parse + match + sort, no index yet),
// so scans use online-only search. Manual search keeps the archive path.
var match = bangumi.searchByAlias(animeName, candidates, includeArchive = false).getOrNull()
var matchedBy = "alias"
if (match != null) {
MiruLog.i(
Expand All @@ -681,7 +685,7 @@ class ScanCoordinator @Inject constructor(
if (fallbackAttemptSample.size < MAX_RECOGNITION_ATTEMPT_SAMPLE_IN_LOG) {
fallbackAttemptSample += candidate
}
match = bangumi.searchAnime(candidate).getOrNull()
match = bangumi.searchAnime(candidate, includeArchive = false).getOrNull()
?.firstOrNull { it.confidence >= RECOGNITION_CONFIDENCE_THRESHOLD }
if (match != null) {
matchedBy = "fallback_search"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ class VideoDirectoryClassifier(
private fun modelPathText(path: String, fileName: String): String {
val normalized = path.replace('\\', '/').trim()
if (normalized.isBlank()) return fileName
// Remote URLs carry no title signal in their machinery (scheme/host/date/hash
// segments); parsing the whole URL fabricates %-fragment titles (e.g. "A1%80%")
// that outscore the clean filename evidence. Use the filename only; real folder
// names still vote through FOLDER_BERT / show-context evidence.
if (normalized.contains("://")) return fileName
if (normalized.length <= maxParsedTextLength) return normalized

val segments = pathSegments(normalized)
Expand Down Expand Up @@ -458,7 +463,8 @@ class VideoDirectoryClassifier(
this?.takeIf { it > 0 }

private fun String.shouldParseContextSegment(): Boolean =
usableName() != null || numericContextPatterns.any { it.matches(normalizedContextName()) }
!isUrlMachinerySegment() &&
(usableName() != null || numericContextPatterns.any { it.matches(normalizedContextName()) })

private fun findShowContext(
segments: List<String>,
Expand All @@ -471,6 +477,9 @@ class VideoDirectoryClassifier(
}
for (index in candidateSegments.indices.reversed()) {
val rawName = candidateSegments[index]
// Skip URL machinery (host/date/encoded segments): they are not show names
// and otherwise win show-context over the real filename evidence.
if (rawName.isUrlMachinerySegment()) continue
val release = ReleaseFilenameParser.parse(rawName)
if (release != null) {
val seriesName = release.seriesName.usableName()
Expand All @@ -485,6 +494,19 @@ class VideoDirectoryClassifier(
return null
}

private fun String.isUrlMachinerySegment(): Boolean {
val raw = trim().trimEnd('/')
if (raw.isEmpty()) return true
if (raw.contains("://")) return true
if (raw.equals("https:", ignoreCase = true) || raw.equals("http:", ignoreCase = true)) return true
// Percent-encoded blob (series/file link segment): title signal lives in the
// decoded filename, not in the raw triplet soup.
if (PERCENT_TRIPLET_REGEX.containsMatchIn(raw)) return true
if (URL_HOST_SEGMENT_REGEX.matches(raw)) return true
if (URL_DATE_SEGMENT_REGEX.matches(raw)) return true
return false
}

private fun String.isGenericContextName(): Boolean {
val normalized = normalizedContextName()
if (normalized in genericContextNames) return true
Expand Down Expand Up @@ -581,6 +603,10 @@ class VideoDirectoryClassifier(
Regex("""(?i)^(?:season|series|s)\s*\d{1,2}$"""),
Regex("""(?i)^(?:ep|episode|part)\s*[\d一二三四五六七八九十]+(?:[a-z])?$""")
)
private val PERCENT_TRIPLET_REGEX = Regex("%[0-9A-Fa-f]{2}")
private val URL_HOST_SEGMENT_REGEX =
Regex("""^[A-Za-z0-9]([A-Za-z0-9.\-]*[A-Za-z0-9])?\.[A-Za-z]{2,}(:\d+)?$""")
private val URL_DATE_SEGMENT_REGEX = Regex("""^\d{4}-\d{1,2}(-\d{1,2})?$""")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ class ScanCoordinatorTest {
assertEquals(25, indexRepository.entries.single().episodeNumber)
assertEquals("Dr STONE 新石紀", scraper.normalizedName)
assertTrue(scraper.aliasCandidates.contains("Dr STONE 新石紀 第四季"))
assertTrue(
"Auto-scan must skip the offline archive (full 1GB parse per query hangs TV scans)",
scraper.includeArchiveFlags.isNotEmpty() && scraper.includeArchiveFlags.all { !it },
)
}

@Test
Expand Down Expand Up @@ -1093,9 +1097,13 @@ class ScanCoordinatorTest {
override val sourceName: String = "Bangumi"
var normalizedName: String? = null
var aliasCandidates: List<String> = emptyList()
val includeArchiveFlags = mutableListOf<Boolean>()

override suspend fun searchAnime(query: String): Result<List<ScraperResult>> =
Result.success(emptyList())
override suspend fun searchAnime(
query: String,
includeArchive: Boolean,
): Result<List<ScraperResult>> =
Result.success<List<ScraperResult>>(emptyList()).also { includeArchiveFlags += includeArchive }

override suspend fun getAnimeDetails(animeId: String): Result<Anime> =
Result.failure(AppError.ScrapingError.NoMatchFound(animeId))
Expand All @@ -1105,10 +1113,12 @@ class ScanCoordinatorTest {

override suspend fun searchByAlias(
normalizedName: String,
candidates: List<String>
candidates: List<String>,
includeArchive: Boolean,
): Result<ScraperResult?> {
this.normalizedName = normalizedName
this.aliasCandidates = candidates
includeArchiveFlags += includeArchive
return Result.success(null)
}
}
Expand All @@ -1118,7 +1128,10 @@ class ScanCoordinatorTest {
) : MetadataScraper {
override val sourceName: String = "Bangumi"

override suspend fun searchAnime(query: String): Result<List<ScraperResult>> =
override suspend fun searchAnime(
query: String,
includeArchive: Boolean,
): Result<List<ScraperResult>> =
Result.success(listOf(match()))

override suspend fun getAnimeDetails(animeId: String): Result<Anime> =
Expand All @@ -1136,7 +1149,8 @@ class ScanCoordinatorTest {

override suspend fun searchByAlias(
normalizedName: String,
candidates: List<String>
candidates: List<String>,
includeArchive: Boolean,
): Result<ScraperResult?> = Result.success(match())

private fun match(): ScraperResult =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.miruplay.tv.scanner
import com.miruplay.tv.model.FilenameMetadataParser
import com.miruplay.tv.model.FilenameParseResult
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
Expand Down Expand Up @@ -358,6 +359,32 @@ class VideoDirectoryClassifierTest {
assertTrue(result.titleCandidates.none { it.contains("电视剧/逐玉") })
}

@Test
fun `remote URL path does not outvote filename title`() {
val fileName = "[ANi] BLEACH 死神 千年血戰篇-禍進譚- - 49 [1080P][Baha][WEB-DL][AAC AVC][CHT].mp4"
val result = classifier.classifyVideo(
path = "https://resources.ani.rip/2026-10/%5BANi%5D%20BLEACH%20%E6%AD%BB%E7%A5%9E%20%E5%8D%83%E5%B9%B4%E8%A1%80%E6%88%B0%E7%AF%87-%E7%A6%8D%E9%80%B2%E8%AD%9A-%20-%2049%20%5B1080P%5D%5BBaha%5D%5BWEB-DL%5D%5BAAC%20AVC%5D%5BCHT%5D.mp4?d=mp4",
fileName = fileName,
)

// Host/date/percent-triplet URL machinery must never become the show name.
assertFalse(result.animeName.contains("%"))
assertFalse(result.animeName.contains("resources", ignoreCase = true))
assertFalse(result.animeName.contains("2026"))
assertTrue(result.animeName.contains("BLEACH"))
}

@Test
fun `encoded path segment is not treated as show context`() {
val result = classifier.classifyVideo(
path = "https://resources.ani.rip/2026-10/%E9%BB%91%20test.mp4",
fileName = "黑 test.mp4",
)

assertFalse(result.animeName.contains("%"))
assertFalse(result.animeName.contains("2026"))
}

private class StaticFilenameParser(
private val result: FilenameParseResult
) : FilenameMetadataParser {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ interface MetadataScraper {
/**
* Search for anime by query.
*/
suspend fun searchAnime(query: String): Result<List<ScraperResult>>
suspend fun searchAnime(
query: String,
includeArchive: Boolean = true,
): Result<List<ScraperResult>>

/**
* Get full anime details by ID.
Expand All @@ -35,6 +38,7 @@ interface MetadataScraper {
suspend fun searchByAlias(
normalizedName: String,
candidates: List<String>,
includeArchive: Boolean = true,
): Result<ScraperResult?>
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ suspend fun BangumiApiClient.searchByAlias(
normalizedName: String,
candidates: List<String>,
confidenceThreshold: Float = BANGUMI_ALIAS_CONFIDENCE_THRESHOLD,
includeArchive: Boolean = true,
): Result<ScraperResult?> = PerformanceLog.measureSuspendResult(
tag = "BangumiPerformance",
operation = "bangumi.alias_search",
Expand All @@ -34,7 +35,14 @@ suspend fun BangumiApiClient.searchByAlias(

val matches = linkedMapOf<String, ScraperResult>()
for (candidate in uniqueCandidates) {
val result = searchAnime(candidate).getOrNull().orEmpty()
val result = searchAnime(
candidate,
archiveMinimumConfidence = confidenceThreshold,
includeOnlineWhenArchiveMatches = false,
includeDirectIdResult = false,
onlineLimit = 10,
includeArchive = includeArchive,
).getOrNull().orEmpty()
.firstOrNull { it.confidence >= confidenceThreshold }
if (result != null) {
val existing = matches[result.animeId]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,17 @@ class BangumiApiClient(
client.configureProxy(proxyConfig)
}

suspend fun searchAnime(query: String): Result<List<ScraperResult>> =
suspend fun searchAnime(
query: String,
includeArchive: Boolean = true,
): Result<List<ScraperResult>> =
searchAnime(
query = query,
archiveMinimumConfidence = ARCHIVE_AUTO_MINIMUM_CONFIDENCE,
includeOnlineWhenArchiveMatches = false,
includeDirectIdResult = false,
onlineLimit = 10,
includeArchive = includeArchive,
)

suspend fun searchAnimeForManualMatch(query: String): Result<List<ScraperResult>> =
Expand All @@ -75,12 +79,13 @@ class BangumiApiClient(
onlineLimit = 20,
)

private suspend fun searchAnime(
internal suspend fun searchAnime(
query: String,
archiveMinimumConfidence: Float,
includeOnlineWhenArchiveMatches: Boolean,
includeDirectIdResult: Boolean,
onlineLimit: Int,
includeArchive: Boolean = true,
): Result<List<ScraperResult>> = PerformanceLog.measureSuspendResult(
tag = PERFORMANCE_TAG,
operation = "bangumi.search",
Expand All @@ -89,6 +94,7 @@ class BangumiApiClient(
"online_limit" to onlineLimit.toString(),
"include_direct_id_result" to includeDirectIdResult.toString(),
"archive_minimum_confidence" to archiveMinimumConfidence.toString(),
"include_archive" to includeArchive.toString(),
),
) {
withContext(Dispatchers.IO) {
Expand All @@ -98,11 +104,15 @@ class BangumiApiClient(
directIdSearchResult(query)?.let { merged.addBest(it) }
}

val localResults = archiveSearch?.search(
query = query,
limit = onlineLimit,
minimumConfidence = archiveMinimumConfidence,
).orEmpty()
val localResults = if (includeArchive) {
archiveSearch?.search(
query = query,
limit = onlineLimit,
minimumConfidence = archiveMinimumConfidence,
).orEmpty()
} else {
emptyList()
}
localResults.forEach { merged.addBest(it) }
if (localResults.isNotEmpty() && !includeOnlineWhenArchiveMatches) {
return@withContext Result.success(localResults)
Expand Down
Loading
Loading