fix(m0): get a stopped or reloaded YouTube player going again and measure the window again

On the gaming PC the player errored about 38 s into the first window with
Jarvis off; the kit had no way to notice, so every later window measured a
paused, empty player, and after the user reloaded the page the injected
counters were gone and the snapshot script threw, ending the stage early.

- The snapshot reports a lost page (counters gone, or the script could not
  run while the page loaded) and the player's own error screen (first line
  only, bracketed IDs removed) instead of throwing.
- Before each window the kit presses play on a stopped player and loads the
  page again when that is not enough, or when the page was lost or errored.
- A window the player spoilt (error, reload, pause, ad, seek, buffering, no
  frames) is measured again, up to 3 times in all, with the earlier attempts
  and the fixes kept in the row; a lower quality or frame rate is not, since
  a busy GPU can cause it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-11 01:54:34 +09:00
parent 37e27a2648
commit 6d0b3d5bec
3 changed files with 216 additions and 27 deletions

View File

@@ -1325,13 +1325,16 @@ def test_the_comparison_adds_up_only_valid_busy_windows_on_each_side_of_the_cap(
class _FakePage:
"""The watch page: 60 frames shown per snapshot step, a few dropped; records what the stage did."""
"""The watch page: 60 frames shown per snapshot step, a few dropped; records what the stage did.
``breaks_at`` lists the snapshots (counted from 1) at which the page was loaded again meanwhile;
the player then stays broken until ``keep_room`` loads it again."""
def __init__(self, starts=True, quality="hd1080", height=1080):
def __init__(self, starts=True, quality="hd1080", height=1080, breaks_at=()):
self.starts, self.quality, self.height = starts, quality, height
self.total = self.dropped = 0
self.events: list[str] = []
self.closed = False
self.breaks_at, self.snaps, self.broken, self.reloads = set(breaks_at), 0, False, 0
def start(self, quality="hd1080", timeout=180):
self.events.append("video_start")
@@ -1339,12 +1342,21 @@ class _FakePage:
"height": self.height if self.starts else None, "ad_seen": False}
def keep_room(self):
pass
if not self.broken:
return []
self.broken, self.total, self.dropped = False, 0, 0
self.reloads += 1
return ["loaded the page again"]
def reset(self):
pass
def snapshot(self):
self.snaps += 1
if self.snaps in self.breaks_at:
self.broken = True
if self.broken:
return {"lost": True}
self.total += 3600
self.dropped += 2
return _snap(self.total, self.dropped, quality=self.quality, height=self.height)
@@ -1435,6 +1447,8 @@ def test_a_video_stuck_below_the_asked_quality_counts_no_window(tmp_path, monkey
r, _, _, _ = _run_video_stage(tmp_path, monkeypatch, fake_server, _FakePage(quality="medium", height=360))
assert r["status"] == "partial" and any("medium" in e["error"] for e in r["errors"])
assert not any(x["video"]["valid"] for x in r["video_rows"])
# A lower quality may come from the GPU itself, so such a window is not measured again.
assert not any(x["video"].get("earlier_attempts") for x in r["video_rows"])
assert r["comparison"]["no_cap"]["windows"] == 0 and r["comparison"]["cap95"]["windows"] == 0
@@ -1445,3 +1459,106 @@ def test_a_cap_that_could_not_pause_jarvis_does_not_count_as_a_capped_window(tmp
assert r["comparison"]["cap95"]["windows"] == 0 and r["comparison"]["no_cap"]["windows"] == 2
assert r["status"] == "partial" and any("could not be paused" in e["error"] for e in r["errors"])
assert not running["held"]
@pytest.mark.parametrize("before,after,retry", [
(_snap(1000, 5), {"lost": True}, True), # the page was loaded again
(_snap(1000, 5), _snap(4600, 9, error=True, error_text="x"), True), # the player's error screen
(_snap(1000, 5), _snap(640, 0, paused=True, height=0), True), # what the gaming PC reported
(_snap(1000, 5), _snap(4600, 9, ad=True), True),
(_snap(1000, 5), _snap(4600, 9, waits=2), True),
(_snap(1000, 5, quality="medium", height=360), _snap(4600, 9, quality="medium", height=360), False),
(_snap(1000, 5), _snap(2800, 9), False), # the frame rate fell
])
def test_only_a_window_the_player_spoilt_is_worth_measuring_again(before, after, retry):
from m0lib import video
w = video.window_stats(before, after, 60.0, video.WANT)
assert not w["valid"] and w["retry"] is retry
def test_a_reloaded_page_is_measured_again_and_the_stage_goes_on(tmp_path, monkeypatch, fake_server):
from m0lib import video
# Two snapshots a window, one more pair for the window measured again: the video alone, then the first busy one.
page = _FakePage(breaks_at={2, 8})
r, _, running, stt = _run_video_stage(tmp_path, monkeypatch, fake_server, page)
assert r["status"] == "ok" and page.reloads == 2
rows = {x["key"]: x for x in r["video_rows"]}
assert all(x["video"]["valid"] for x in rows.values())
for key in ("video_only", "busy"):
assert len(rows[key]["video"]["earlier_attempts"]) == 1
assert rows[key]["video"]["player_fixes"] == ["loaded the page again"]
assert any(key in n and "measured again" in n for n in r["notes"])
assert rows["busy"]["llm"]["decode_tok_s"]["n"] > 0
assert r["comparison"]["no_cap"]["windows"] == 2 and r["comparison"]["cap95"]["windows"] == 2
assert page.closed and running["now"] is None and stt.loaded is None and not running["held"]
def test_a_player_that_keeps_breaking_is_tried_a_few_times_per_window_and_never_stops_the_stage(
tmp_path, monkeypatch, fake_server):
from m0lib import video
page = _FakePage(breaks_at=set(range(2, 400, 2))) # every window's end
r, _, running, stt = _run_video_stage(tmp_path, monkeypatch, fake_server, page)
rows = r["video_rows"]
assert [x["key"] for x in rows][-1] == "video_only_again"
assert all(not x["video"]["valid"] and len(x["video"]["earlier_attempts"]) == video.ATTEMPTS - 1 for x in rows)
assert r["comparison"]["no_cap"]["windows"] == 0 and r["comparison"]["cap95"]["windows"] == 0
assert page.closed and running["now"] is None and stt.loaded is None and not running["held"]
class _ScriptedCdp:
"""The DevTools side of a watch page whose player is stopped or broken until it is loaded again."""
def __init__(self, first: dict, plays_again: bool):
from m0lib import video
self.v = video
self.first, self.plays_again = first, plays_again
self.navigated: list[str] = []
self.pressed = 0
def _ok(self) -> bool:
return bool(self.navigated) or (self.plays_again and self.pressed > 0)
def evaluate(self, expr):
if expr == self.v._SNAPSHOT:
if not self._ok():
return dict(self.first)
return {**_snap(1000, 0), "paused": False, "ad_now": False, "time": 30.0, "duration": 634.0}
if expr == self.v._STEP:
self.pressed += 1
return {"state": "playing" if self._ok() else "starting", "levels": ["hd1080"]}
return True
def call(self, method, params=None):
if method == "Page.navigate":
self.navigated.append(params["url"])
return {}
@pytest.mark.parametrize("first,plays_again,reloaded", [
({"lost": True}, False, True), # loaded again by hand: counters gone
({**_snap(640, 0), "error": True, "error_text": "문제가 발생했습니다. (재생 ID: Ab12Cd)"}, False, True),
({**_snap(640, 0), "paused": True, "height": 0}, True, False), # play is enough
({**_snap(640, 0), "paused": True, "height": 0}, False, True), # play is not
])
def test_before_a_window_a_stopped_or_broken_player_is_started_again(monkeypatch, first, plays_again, reloaded):
from m0lib import video
monkeypatch.setattr(video, "SETTLE_S", 0)
monkeypatch.setattr(video, "REPLAY_S", 0.01)
monkeypatch.setattr(video.time, "sleep", lambda s: None)
cdp = _ScriptedCdp(first, plays_again)
page = video.VideoPage(cdp, url="https://example.invalid/watch")
did = page.keep_room()
assert cdp.navigated == (["https://example.invalid/watch"] if reloaded else [])
assert ("loaded the page again" in did) is reloaded and did
assert not any("Ab12Cd" in d for d in did) # YouTube's playback ID is not kept
assert not page.snapshot().get("lost")
def test_a_page_script_that_fails_while_the_page_loads_reads_as_a_lost_page():
from m0lib import video
class Broken:
def evaluate(self, expr):
raise RuntimeError("page script failed: Execution context was destroyed.")
assert video.VideoPage(Broken()).snapshot()["lost"] is True