diff --git a/tests/unit/test_results.py b/tests/unit/test_results.py
index 7fb0ae1..66cd4ef 100644
--- a/tests/unit/test_results.py
+++ b/tests/unit/test_results.py
@@ -489,3 +489,50 @@ def test_write_results_roundtrip(tmp_path):
assert path.endswith("/42/results.json")
with open(path) as f:
assert json.load(f)["level"] == 3
+
+
+def test_fresh_artifact_dir_removes_owned_stale_artifacts(tmp_path):
+ """Run-dir collision guard (2026-10-05): drone build numbers restarted while old-era run dirs
+ linger in /var/lib/cc-ci-runs — a new build reusing an old number must not mix the old run's
+ artifacts (fossil screenshot.png from ANOTHER recipe) into its card. Only OWNED artifacts go."""
+ d = tmp_path / "35"
+ d.mkdir()
+ (d / "results.json").write_text('{"old": true}')
+ (d / "screenshot.png").write_bytes(b"stale-fossil")
+ (d / "summary.png").write_bytes(b"stale")
+ (d / "badge.svg").write_text("")
+ (d / "lint.txt").write_text("old")
+ (d / "summary.html").write_text("
")
+ (d / "junit").mkdir()
+ (d / "junit" / "old.xml").write_text("")
+ (d / "untracked-thing.bin").write_bytes(b"keep me") # NOT owned — must survive
+
+ R.fresh_artifact_dir(str(d))
+
+ for name in (
+ "results.json",
+ "screenshot.png",
+ "summary.png",
+ "badge.svg",
+ "lint.txt",
+ "summary.html",
+ ):
+ assert not (d / name).exists(), f"stale owned artifact {name} survived"
+ assert not (d / "junit").exists(), "stale junit dir survived"
+ assert (d / "untracked-thing.bin").read_bytes() == b"keep me", "unowned file was deleted"
+
+
+def test_fresh_artifact_dir_creates_missing_dir(tmp_path):
+ d = tmp_path / "36"
+ out = R.fresh_artifact_dir(str(d))
+ assert out == str(d) and d.is_dir()
+
+
+def test_fresh_artifact_dir_tolerant_of_odd_entries(tmp_path):
+ """R7: never raise into the run — a dir where an owned name is itself a dir must still work."""
+ d = tmp_path / "37"
+ d.mkdir()
+ (d / "screenshot.png").mkdir() # perverse: a DIRECTORY named screenshot.png
+ (d / "junit").mkdir()
+ R.fresh_artifact_dir(str(d))
+ assert not (d / "screenshot.png").exists() and not (d / "junit").exists()
diff --git a/tests/unit/test_screenshot.py b/tests/unit/test_screenshot.py
index 6c83d90..f9859f1 100644
--- a/tests/unit/test_screenshot.py
+++ b/tests/unit/test_screenshot.py
@@ -184,3 +184,78 @@ def test_screenshot_reachable_through_real_load_path(tmp_path):
hook = S._load_screenshot_hook(meta)
assert callable(hook), "SCREENSHOT hook did not survive the orchestrator load path (R2)"
assert S._load_screenshot_hook(meta_mod.load("no-such", tests_dir=str(tmp_path))) is None
+
+
+# ---- capture() fossil guard (2026-10-05) -----------------------------------------------------
+# Drone build numbers restarted when its DB was re-created while /var/lib/cc-ci-runs still holds
+# old-era dirs numbered into the thousands; run 35 (!testme gitea #10) collided with a May-31 run
+# 35 and embedded custom-html-tiny's fossil screenshot.png because the hook branch SKIPPED the
+# snap when out_path already existed. capture() must always snap fresh.
+
+
+class _FakeLaunchable:
+ def __init__(self, page):
+ self._page = page
+ self.chromium = self
+
+ def launch(self, args=None):
+ return self
+
+ def new_context(self, **kw):
+ return self
+
+ def new_page(self):
+ return self._page
+
+ def close(self):
+ pass
+
+
+class _FakeSyncPlaywright:
+ """sync_playwright() replacement: context-manager factory handing out one fake browser."""
+
+ def __init__(self, page):
+ self._page = page
+
+ def __call__(self):
+ return self
+
+ def __enter__(self):
+ return _FakeLaunchable(self._page)
+
+ def __exit__(self, *exc):
+ return False
+
+
+def _install_fake_playwright(monkeypatch, page):
+ import types
+
+ fake_api = types.ModuleType("playwright.sync_api")
+ fake_api.sync_playwright = _FakeSyncPlaywright(page)
+ fake = types.ModuleType("playwright")
+ fake.sync_api = fake_api
+ monkeypatch.setitem(sys.modules, "playwright", fake)
+ monkeypatch.setitem(sys.modules, "playwright.sync_api", fake_api)
+
+
+def test_capture_overwrites_fossil_screenshot(tmp_path, monkeypatch):
+ """A pre-existing out_path in a recycled run artifact dir must never be embedded: the hook
+ branch snaps fresh regardless (the old exists-guard returned the fossil as 'captured')."""
+ out_path = str(tmp_path / "screenshot.png")
+ with open(out_path, "wb") as f:
+ f.write(b"STALE-FOSSIL-FROM-ANOTHER-RECIPE" * 400)
+
+ fired = {}
+ page = _FakePage([35707]) # one real-sized frame, like a painted page
+
+ def hook(p, ctx):
+ fired["yes"] = True # navigation only — hooks never save the PNG themselves
+
+ _install_fake_playwright(monkeypatch, page)
+ got = S.capture("gitea.example.org", out_path, recipe_meta={"SCREENSHOT": hook})
+
+ assert got == out_path, "capture must report success after snapping fresh"
+ assert fired.get("yes"), "SCREENSHOT hook must run"
+ assert page.shots == 1, "capture must snap exactly once — the fossil is not a captured frame"
+ with open(out_path, "rb") as f:
+ assert f.read(4) == b"\x89PNG", "out_path still holds the fossil — snap did not overwrite"