Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions devtools/pytest_supervisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import select
import shutil
import signal
import stat
import subprocess
import sys
import time
Expand Down Expand Up @@ -902,6 +903,14 @@ def _parse_args(argv: Sequence[str]) -> tuple[argparse.Namespace, list[str]]:
return parser.parse_args(option_argv), controller_cmd


def _restore_owner_write(root: Path) -> None:
"""Read-only trees (seeded-archive artifacts chmod their directories
non-writable) must not survive cleanup: restore owner-write throughout."""
for candidate in (root, *root.rglob("*")):
with contextlib.suppress(OSError):
candidate.chmod(candidate.stat().st_mode | stat.S_IWUSR)


def cleanup_managed_tmpfs_path(path: Path | None) -> bool:
"""Remove only a harness-owned direct child of the system tmpfs."""
if path is None or not path.name.startswith("pytest-polylogue-"):
Expand All @@ -912,6 +921,9 @@ def cleanup_managed_tmpfs_path(path: Path | None) -> bool:
except OSError:
return False
shutil.rmtree(path, ignore_errors=True)
if path.exists():
_restore_owner_write(path)
shutil.rmtree(path, ignore_errors=True)
return not path.exists()


Expand Down
24 changes: 24 additions & 0 deletions tests/unit/devtools/test_pytest_supervisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import contextlib
import json
import os
import shutil
Expand Down Expand Up @@ -60,6 +61,29 @@ def test_cleanup_managed_tmpfs_path_removes_only_owned_run(tmp_path: Path) -> No
shutil.rmtree(run_root, ignore_errors=True)


def test_cleanup_managed_tmpfs_path_removes_read_only_artifact_trees(tmp_path: Path) -> None:
"""Seeded-archive artifacts chmod their directories read-only; cleanup must
still remove them (polylogue-b9yw7: cleanup.complete=false withheld merge
authority on otherwise-green receipts)."""
run_root = Path("/dev/shm") / f"pytest-polylogue-cleanup-ro-{os.getpid()}-{time.monotonic_ns()}"
try:
artifact = run_root / "seeded-cache" / "artifacts" / "deadbeef" / "wire"
artifact.mkdir(parents=True)
payload = artifact / "seed-00.jsonl"
payload.write_text("{}", encoding="utf-8")
for target in (payload, artifact, artifact.parent):
target.chmod(target.stat().st_mode & ~0o222)

assert cleanup_managed_tmpfs_path(run_root) is True
assert not run_root.exists()
finally:
if run_root.exists():
for candidate in sorted(run_root.rglob("*"), reverse=True):
with contextlib.suppress(OSError):
candidate.chmod(candidate.stat().st_mode | 0o200)
shutil.rmtree(run_root, ignore_errors=True)


def _wait_for_receipt(path: Path, *, status: str | None = None) -> dict[str, object]:
payload: dict[str, object] | None = None

Expand Down