From 97cd7f4c245af092ee19abf88d9e1a44303ea082 Mon Sep 17 00:00:00 2001 From: Andrew Beveridge Date: Tue, 17 Nov 2026 18:16:03 -0500 Subject: [PATCH] fix(youtube-catalog): keep original metadata for a video first seen and rewritten in one run Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012RYLVVbErmEFhmiW48uuFK --- backend/services/youtube_video_catalog.py | 8 +++++++- backend/tests/test_youtube_video_catalog.py | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/backend/services/youtube_video_catalog.py b/backend/services/youtube_video_catalog.py index 660e1577..08661a4a 100644 --- a/backend/services/youtube_video_catalog.py +++ b/backend/services/youtube_video_catalog.py @@ -176,7 +176,13 @@ def sync_catalog( prev = existing.get(vid) if prev is None: doc.update(first_seen_at=now, last_changed_at=now) - writer.group([(coll.document(vid), doc, False), _history(vid, "first_seen", {}, "observed")]) + ops = [(coll.document(vid), doc, False), _history(vid, "first_seen", {}, "observed")] + if vid in pre_update: + # Rewritten in the same run it was first seen: keep the original. + ours = _diff(metadata_from_video(pre_update[vid]), meta) + if ours: + ops.append(_history(vid, "changed", ours, "backfill")) + writer.group(ops) stats["new"] += 1 continue diff --git a/backend/tests/test_youtube_video_catalog.py b/backend/tests/test_youtube_video_catalog.py index 35ece814..7976b6fe 100644 --- a/backend/tests/test_youtube_video_catalog.py +++ b/backend/tests/test_youtube_video_catalog.py @@ -272,3 +272,13 @@ def test_large_sync_commits_in_multiple_batches_without_splitting_pairs(): hist = {p.split("/")[1] for p in b if "/history/" in p} assert docs == hist assert len(db.collection(catalog.COLLECTION).docs()) == 450 + + +def test_new_video_rewritten_in_same_run_keeps_original_in_history(): + db = MemDB() + _sync(db, {"a": _video("a", desc="new template")}, T0, pre={"a": _video("a", desc="upload description")}) + events = sorted((h["event"], h["source"]) for h in _history(db, "a")) + assert events == [("changed", "backfill"), ("first_seen", "observed")] + changed = [h for h in _history(db, "a") if h["event"] == "changed"][0] + assert changed["changes"]["description"] == {"old": "upload description", "new": "new template"} + assert _doc(db, "a")["description"] == "new template"