Taste keeps what a newer Orbit wrote, through tidying and Rooms sync

Unknown fields on a known signal are carried through; a line with an unknown kind
or schema is kept exactly as written (if it has an id and a time). Sending, merging
and the start-up tidy all keep them. Once both TVs have this, a new kind of
signal (Taste Stage 2's caught-end) can be installed one TV at a time safely.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-09 17:23:46 +00:00
co-authored by Claude Opus 5.5
parent 32322f1d63
commit b099bb5175
4 changed files with 81 additions and 9 deletions
@@ -72,7 +72,7 @@ internal object RoomSync {
scheduleDir.listFiles { f -> Regex("continue-[A-Za-z0-9]{1,40}\\.json").matches(f.name) }?.forEach { files.put(it.name, it.readText()) }
File(scheduleDir, "taste").listFiles()?.forEach { f ->
when {
f.name.endsWith(".events.jsonl") -> f.readLines().filter { l -> TasteEvent.of(l)?.let { it.atMs >= sinceMs - ROOM_SYNC_OVERLAP_MS } == true }
f.name.endsWith(".events.jsonl") -> f.readLines().filter { l -> (TasteEvent.of(l)?.atMs ?: runCatching { org.json.JSONObject(l).getLong("w") }.getOrNull())?.let { it >= sinceMs - ROOM_SYNC_OVERLAP_MS } == true }
.takeIf { it.isNotEmpty() }?.let { files.put("taste/${f.name}", it.joinToString("\n") + "\n") }
f.name.endsWith(".state.json") -> files.put("taste/${f.name}", f.readText())
}
@@ -98,9 +98,10 @@ internal fun mergeFamilyFiles(dir: File, files: JSONObject, nowMs: Long = System
when {
name.startsWith("taste/") && name.endsWith(".events.jsonl") && TasteFiles.matches(name.removePrefix("taste/")) -> {
val target = File(dir, name).also { it.parentFile?.mkdirs() }
val here = if (target.exists()) target.readLines().mapNotNull { TasteEvent.of(it) } else emptyList()
val merged = compactTaste(here + text.lines().mapNotNull { TasteEvent.of(it) }, nowMs)
writeSafely(target, merged.joinToString("") { it.toLine() + "\n" })
// Lines a newer Orbit wrote are kept as they are, so this TV never loses them (compactTasteLines).
val here = if (target.exists()) target.readLines() else emptyList()
val merged = compactTasteLines(here + text.lines(), nowMs)
writeSafely(target, merged.joinToString("") { it + "\n" })
}
name.startsWith("taste/") && name.endsWith(".state.json") && TasteFiles.matches(name.removePrefix("taste/")) ->
File(dir, name).let { if (!it.exists()) { writeSafely(it, text) } }
@@ -36,8 +36,11 @@ internal const val TASTE_BRIEF_MS = 5 * 60_000L
/** One signal. [id] is random so two rooms' logs can later be joined without duplicates (Rooms sync is not built yet). */
internal class TasteEvent(val id: String, val profile: String, val kind: TasteKind, val itemId: String, val seriesId: String?, val title: String,
val channel: Int?, val atMs: Long, val watchedMs: Long?, val source: String) {
fun toLine(): String = JSONObject().put("v", TASTE_SCHEMA).put("id", id).put("p", profile).put("k", kind.key).put("i", itemId)
val channel: Int?, val atMs: Long, val watchedMs: Long?, val source: String,
/** Fields a newer Orbit added that this one doesn't know (9 November 2026): carried through untouched, never dropped. */
val extras: JSONObject? = null) {
fun toLine(): String = JSONObject().apply { extras?.let { e -> e.keys().forEach { k -> put(k, e.get(k)) } } }
.put("v", TASTE_SCHEMA).put("id", id).put("p", profile).put("k", kind.key).put("i", itemId)
.put("s", seriesId ?: JSONObject.NULL).put("t", title.take(160)).put("c", channel ?: JSONObject.NULL).put("w", atMs)
.put("ms", watchedMs ?: JSONObject.NULL).put("src", source.take(24)).toString()
companion object {
@@ -48,7 +51,8 @@ internal class TasteEvent(val id: String, val profile: String, val kind: TasteKi
val item = o.getString("i").takeIf(EmbyClient::validId) ?: return null
TasteEvent(o.getString("id"), o.getString("p"), TasteKind.of(o.getString("k")) ?: return null, item,
o.optString("s").takeIf { !o.isNull("s") && EmbyClient.validId(it) }, o.optString("t"), if (o.isNull("c")) null else o.optInt("c"),
o.getLong("w"), if (o.isNull("ms")) null else o.optLong("ms"), o.optString("src"))
o.getLong("w"), if (o.isNull("ms")) null else o.optLong("ms"), o.optString("src"),
JSONObject().also { e -> o.keys().forEach { k -> if (k !in TASTE_KNOWN_FIELDS) e.put(k, o.get(k)) } }.takeIf { it.length() > 0 })
}.getOrNull()
}
}
@@ -57,6 +61,29 @@ internal class TasteEvent(val id: String, val profile: String, val kind: TasteKi
internal fun tasteDuplicate(recent: List<TasteEvent>, event: TasteEvent): Boolean =
recent.any { it.id == event.id || (it.profile == event.profile && it.kind == event.kind && it.itemId == event.itemId && kotlin.math.abs(it.atMs - event.atMs) < TASTE_DUPLICATE_MS) }
internal val TASTE_KNOWN_FIELDS = setOf("v", "id", "p", "k", "i", "s", "t", "c", "w", "ms", "src")
/**
* The log's lines kept within its bounds, keeping what this Orbit can't read (9 November 2026): a line from a newer Orbit
* (a new kind of signal, or a newer schema) is kept exactly as written, as long as it has an id and a time, so tidying up
* or a sync with an older TV never loses it. Damaged lines are still dropped.
*/
internal fun compactTasteLines(lines: List<String>, nowMs: Long): List<String> {
val byId = LinkedHashMap<String, Pair<Long, String>>()
for (line in lines) {
if (line.isBlank()) continue
val known = TasteEvent.of(line)
val (id, at, text) = if (known != null) Triple(known.id, known.atMs, known.toLine())
else runCatching { JSONObject(line) }.getOrNull()?.let { o ->
val id = o.optString("id").takeIf { it.isNotBlank() && it.length <= 64 } ?: return@let null
if (!o.has("w") || line.length > 4_000) null else Triple(id, o.optLong("w"), line.trim())
} ?: continue
if (nowMs - at > TASTE_MAX_AGE_MS || at > nowMs + 60_000L) continue
byId.putIfAbsent(id, at to text)
}
return byId.values.sortedBy { it.first }.takeLast(TASTE_MAX_EVENTS).map { it.second }
}
/** The log kept within its bounds: a year at most, the newest [TASTE_MAX_EVENTS], no repeated event ids, oldest first. */
internal fun compactTaste(events: List<TasteEvent>, nowMs: Long): List<TasteEvent> =
events.filter { nowMs - it.atMs <= TASTE_MAX_AGE_MS && it.atMs <= nowMs + 60_000L }.distinctBy { it.id }.sortedBy { it.atMs }.takeLast(TASTE_MAX_EVENTS)
@@ -140,9 +167,9 @@ internal class TasteStore(val directory: File) {
/** The log within its bounds, written through a temporary file with one backup kept. */
@Synchronized fun compact(profile: String, nowMs: Long) {
val file = eventsFile(profile); if (!file.exists()) return
val kept = compactTaste(events(profile), nowMs)
val kept = compactTasteLines(file.readLines(), nowMs)
runCatching { file.copyTo(File(directory, file.name + ".bak"), overwrite = true) }
writeWhole(file, kept.joinToString("") { it.toLine() + "\n" })
writeWhole(file, kept.joinToString("") { it + "\n" })
recent.remove(profile)
}