From 81b61f71dcfcd1ecaacc905cec22a9f0549decc5 Mon Sep 17 00:00:00 2001 From: Anionix Date: Fri, 24 Jul 2026 15:18:02 +0900 Subject: [PATCH] fix: create custom run ancestors privately (#498) --- src/format_bench/workflow.py | 22 ++++++++++++++---- tests/test_workflow.py | 44 ++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/src/format_bench/workflow.py b/src/format_bench/workflow.py index c8427ca..16077f3 100644 --- a/src/format_bench/workflow.py +++ b/src/format_bench/workflow.py @@ -49,6 +49,20 @@ def _default_run_dir(root: Path, dataset_id: str) -> Path: return root / "runs" / f"{dataset_id}-{stamp}" +def _create_missing_private_parents(destination: Path) -> None: + missing: list[Path] = [] + parent = destination.parent + while not parent.exists(): + missing.append(parent) + parent = parent.parent + + # LLM contract: EXISTING_ANCESTOR -> PRIVATE_ANCESTORS_READY | FAILED. + # Create shallow-to-deep without parents=True so every new level requests + # 0700; any mkdir failure stops before the private run leaf is created. + for directory in reversed(missing): + directory.mkdir(mode=0o700) + + def _safe_format_component( value: object, label: str, *, extension: bool = False ) -> str: @@ -143,11 +157,9 @@ def prepare_run( destination = run_dir or _default_run_dir(root, dataset_id) if run_dir is None: destination.parent.mkdir(mode=0o700, exist_ok=True) - destination.mkdir( - mode=0o700, - parents=run_dir is not None, - exist_ok=False, - ) + else: + _create_missing_private_parents(destination) + destination.mkdir(mode=0o700, exist_ok=False) input_dir = destination / "input" input_dir.mkdir(mode=0o700) diff --git a/tests/test_workflow.py b/tests/test_workflow.py index cb0ee9b..017a4c9 100644 --- a/tests/test_workflow.py +++ b/tests/test_workflow.py @@ -215,6 +215,50 @@ def record_mode( assert (run_dir / "manifest.json").is_file() +@pytest.mark.skipif(os.name != "posix", reason="POSIX directory-mode contract") +@pytest.mark.parametrize("mask", (0o002, 0o000)) +def test_prepare_run_creates_each_missing_custom_ancestor_privately( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + mask: int, +) -> None: + root = Path(__file__).parents[1] + existing_parent = tmp_path / "existing" + existing_parent.mkdir(mode=0o700) + first_ancestor = existing_parent / "first" + second_ancestor = first_ancestor / "second" + run_dir = second_ancestor / "run" + created_levels = (first_ancestor, second_ancestor, run_dir) + requested_modes: dict[Path, int] = {} + real_mkdir = Path.mkdir + + def record_mode( + path: Path, + mode: int = 0o777, + parents: bool = False, + exist_ok: bool = False, + ) -> None: + if path in created_levels: + requested_modes[path] = mode + assert parents is False + real_mkdir(path, mode=mode, parents=parents, exist_ok=exist_ok) + + monkeypatch.setattr(Path, "mkdir", record_mode) + + previous_mask = os.umask(mask) + try: + prepared = prepare_run(root, DATASET, run_dir, fixture=True, selected=()) + finally: + os.umask(previous_mask) + + assert prepared == run_dir + assert requested_modes == {directory: 0o700 for directory in created_levels} + for directory in created_levels: + assert stat.S_IMODE(directory.stat().st_mode) == 0o700 + assert (run_dir / "input" / "manifest.json").is_file() + assert (run_dir / "manifest.json").is_file() + + @pytest.mark.skipif(os.name != "posix", reason="POSIX directory-mode contract") @pytest.mark.parametrize("mask", (0o002, 0o000)) def test_prepare_run_creates_missing_default_parent_privately(