fix(python): repair devicePixelRatio the same way on every launch

The DPR repair snaps an off-grid ratio to the nearest real scaling step and
keeps the first of two equally near steps. The steps were frozenset
literals, and a frozenset literal iterates in one order when the module is
compiled from source and another when it is loaded back from a .pyc. So a
midpoint such as 1.125 became 1.25 on the first launch after an install and
1 on every launch after it: the same pinned identity presented two different
devicePixelRatio values.

The steps are now ascending tuples, so a tie always goes to the lower step.
The test runs the repair in two fresh interpreters that share a bytecode
cache, compiling in the first and loading in the second. It failed before
this change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jake Writer
2026-09-25 15:31:39 -06:00
co-authored by Claude Opus 5.5
parent a3dbe40d1a
commit 86a20c73b7
2 changed files with 42 additions and 3 deletions
+34
View File
@@ -149,3 +149,37 @@ class TestEveryIdentityIsCoherent:
for i, preset in enumerate(fp.load_presets("150")["presets"][os_name]):
config = launch(os=os_name, fingerprint_preset=preset)
assert coherence.validate(config, get_target_os(config)) == [], (os_name, i)
_MIDPOINT_REPAIRS = """
from camoufox import coherence
out = []
for os_key, steps in sorted(coherence.PLAUSIBLE_DPR.items()):
steps = sorted(steps)
for low, high in zip(steps, steps[1:]):
config = {"window.devicePixelRatio": (low + high) / 2}
coherence.apply(config, os_key)
out.append(config["window.devicePixelRatio"])
print(out)
"""
def test_a_midpoint_repairs_to_the_lower_step_whether_or_not_bytecode_is_cached(tmp_path):
"""The steps were frozensets, and min() keeps the first of equal distances.
A frozenset literal iterates in one order when compiled from source and in
another when loaded back from a .pyc, so the same identity repaired
differently on its first launch than on later ones."""
import subprocess
import sys
from pathlib import Path
env = {"PYTHONPYCACHEPREFIX": str(tmp_path), "PYTHONPATH": str(Path(coherence.__file__).parents[1])}
runs = [
subprocess.run([sys.executable, "-c", _MIDPOINT_REPAIRS], env=env, capture_output=True,
text=True, check=True).stdout
for _ in range(2) # the first compiles and writes the .pyc, the second loads it
]
assert runs[0] == runs[1]
lower = [low for _, steps in sorted(coherence.PLAUSIBLE_DPR.items())
for low in sorted(steps)[:-1]]
assert runs[0].strip() == str(lower)