diff --git a/layout_picker.py b/layout_picker.py index 03b8fe4..a6568e8 100644 --- a/layout_picker.py +++ b/layout_picker.py @@ -44,7 +44,17 @@ by hand wins: this only ever ADDS, so an explicit choice is never overridden. import json import os -ENABLED = os.environ.get("AUTO_LAYOUT", "0") == "1" +# AUTO_LAYOUT=1 decides and applies. AUTO_LAYOUT=shadow decides, logs, and +# applies NOTHING: the render is byte-for-byte what it would have been. +# +# Shadow exists because everything measured about this picker was measured on 48 +# YouTube clips chosen by hand, and the material users actually upload is a +# different distribution nobody has looked at. A week of shadow answers "what +# does it say about OUR videos" for 0.002 USD and ~2s per video, with no way to +# damage a clip somebody paid for. +_MODE = os.environ.get("AUTO_LAYOUT", "0").strip().lower() +SHADOW = _MODE == "shadow" +ENABLED = _MODE == "1" or SHADOW # 12 frames at 1024px wide. Both numbers are measured, not guessed: see above. SAMPLE_FRAMES = int(os.environ.get("LAYOUT_SAMPLE_FRAMES", "12")) @@ -172,8 +182,19 @@ def pick(video_path, video_duration): def pick_and_apply(video_path, video_duration): - """Convenience for the pipeline: decide, switch on, report what changed.""" + """Decide, switch on (unless shadowing), report what changed.""" decision = pick(video_path, video_duration) + + if SHADOW: + # One greppable line per job. Deliberately not routed through the + # analytics module: that one is opt-in and host-scoped, and a shadow + # run has to work on any deployment, including self-hosted. + would = _module_flags(decision) + print(f"[layout-shadow] decision={decision} " + f"would_enable={','.join(would) if would else 'none'} " + f"duration={video_duration:.0f}s") + return decision + touched = apply(decision) if touched: print(f" ✅ Enabled: {', '.join(touched)}") diff --git a/tests/test_layout_picker.py b/tests/test_layout_picker.py index 705310e..d6df4f2 100644 --- a/tests/test_layout_picker.py +++ b/tests/test_layout_picker.py @@ -115,3 +115,34 @@ class TestSampleFrames: # The 12/1024px pair is measured; keep it overridable without a deploy. monkeypatch.setenv("LAYOUT_SAMPLE_FRAMES", "6") assert layout_picker.SAMPLE_FRAMES == 12 # read at import, not per call + + +class TestShadowMode: + def test_shadow_decides_but_applies_nothing(self, monkeypatch): + # The whole point: the render must be identical to a run with the + # picker switched off. + mods = fake_modules(monkeypatch) + monkeypatch.setattr(layout_picker, "ENABLED", True) + monkeypatch.setattr(layout_picker, "SHADOW", True) + monkeypatch.setattr(layout_picker, "pick", lambda *a, **k: "screencast") + assert layout_picker.pick_and_apply("v.mp4", 60) == "screencast" + assert not any(m.ENABLED for m in mods.values()) + + def test_normal_mode_still_applies(self, monkeypatch): + mods = fake_modules(monkeypatch) + monkeypatch.setattr(layout_picker, "ENABLED", True) + monkeypatch.setattr(layout_picker, "SHADOW", False) + monkeypatch.setattr(layout_picker, "pick", lambda *a, **k: "screencast") + layout_picker.pick_and_apply("v.mp4", 60) + assert mods["screencast_layout"].ENABLED is True + + def test_shadow_logs_one_greppable_line(self, monkeypatch, capsys): + fake_modules(monkeypatch) + monkeypatch.setattr(layout_picker, "ENABLED", True) + monkeypatch.setattr(layout_picker, "SHADOW", True) + monkeypatch.setattr(layout_picker, "pick", lambda *a, **k: "split") + layout_picker.pick_and_apply("v.mp4", 90) + out = capsys.readouterr().out + assert "[layout-shadow]" in out + assert "decision=split" in out + assert "would_enable=split_layout,active_speaker" in out