Repository navigation
fix(harnesses): docker solver removes a kept work tree with an unlistable directory #1638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
antonwolfy
wants to merge
3
commits into
intel:main
Choose a base branch
from
antonwolfy:fix/rmtree-unlistable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+178
−16
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
4e4d0ad
fix(harnesses): docker solver removes a kept work tree with an unlist…
antonwolfy cbce4e3
docs(harnesses): say the stale tree made the snapshot copy fail, not …
antonwolfy 4a99ec5
fix(harnesses): remove_tree closes its descriptors under test and log…
antonwolfy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
dokimi/changelog.d/20261204_100000_docker-solver-rmtree-unlistable.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <!-- | ||
| Copyright (C) 2026 Intel Corporation | ||
| SPDX-License-Identifier: Apache-2.0 | ||
| --> | ||
|
|
||
| ### Fixed | ||
|
|
||
| - **A kept agent work tree is replaced when the previous one has a directory the agent made unlistable** — the docker solver cleared a previous run's `work` directory (and a staged directory) with `shutil.rmtree(..., ignore_errors=True)`, so a `chmod 111`, `555` or `000` directory left the old tree in place silently; the `shutil.copytree` that follows then failed with `FileExistsError`, which the solver logged, and the old snapshot was kept without the new files. It now removes the tree the way `AgentWorkspace.cleanup` does (the shared `remove_tree`): each directory is made searchable and writable first, and a tree that still stays is logged with its path and the reason; it still never raises. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| # Copyright (C) 2026 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """docker_solver._rmtree removes a kept agent tree the agent made unlistable (#1511).""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import shutil | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| import dokimi.harnesses.agent_sandbox as sandbox | ||
| from dokimi.harnesses import docker_solver | ||
|
|
||
| _NOT_ROOT = pytest.mark.skipif(not hasattr(os, "geteuid") or os.geteuid() == 0, reason="root lists a mode 111 directory") | ||
| _CAN_UNLOCK = pytest.mark.skipif(not sandbox._can_unlock(), reason="the repair needs O_NOFOLLOW, O_DIRECTORY and /proc/self/fd") | ||
|
|
||
|
|
||
| def _tree(base: Path, mode: int, *, nested: bool) -> Path: | ||
| work = base / "work" | ||
| (work / "sub" / "deep").mkdir(parents=True) | ||
| (work / "sub" / "a.txt").write_text("old a", encoding="utf-8") | ||
| (work / "sub" / "deep" / "b.txt").write_text("old b", encoding="utf-8") | ||
| if nested: | ||
| (work / "sub" / "deep").chmod(mode) | ||
| (work / "sub").chmod(mode) | ||
| return work | ||
|
|
||
|
|
||
| def _unlock(work: Path) -> None: | ||
| for sub in (work / "sub", work / "sub" / "deep"): | ||
| if sub.exists(): | ||
| sub.chmod(0o700) | ||
|
|
||
|
|
||
| @_NOT_ROOT | ||
| @_CAN_UNLOCK | ||
| @pytest.mark.parametrize("nested", [False, True]) | ||
| @pytest.mark.parametrize("mode", [0o111, 0o555, 0o000]) | ||
| def test_rmtree_removes_a_tree_with_an_unlistable_directory(tmp_path, mode, nested): | ||
| work = _tree(tmp_path, mode, nested=nested) | ||
| try: | ||
| docker_solver._rmtree(work) | ||
| assert not work.exists() | ||
| finally: | ||
| if work.exists(): | ||
| _unlock(work) | ||
|
|
||
|
|
||
| @_NOT_ROOT | ||
| @_CAN_UNLOCK | ||
| def test_the_new_tree_copied_after_rmtree_holds_only_the_new_files(tmp_path): | ||
| """#1511: a kept tree that stayed made ``copytree`` fail with ``FileExistsError``, so the old snapshot was kept.""" | ||
| work = _tree(tmp_path, 0o111, nested=True) | ||
| fresh = tmp_path / "fresh" | ||
| (fresh / "sub").mkdir(parents=True) | ||
| (fresh / "sub" / "new.txt").write_text("new", encoding="utf-8") | ||
| try: | ||
| docker_solver._rmtree(work) | ||
| shutil.copytree(fresh, work, symlinks=True) | ||
| found = sorted(str(p.relative_to(work)) for p in work.rglob("*")) | ||
| assert found == ["sub", os.path.join("sub", "new.txt")] | ||
| finally: | ||
| if work.exists(): | ||
| _unlock(work) | ||
|
|
||
|
|
||
| @_NOT_ROOT | ||
| @_CAN_UNLOCK | ||
| def test_rmtree_leaves_the_target_of_a_link_out_of_the_tree_alone(tmp_path): | ||
| outside = tmp_path / "outside" | ||
| outside.mkdir() | ||
| (outside / "keep.txt").write_text("k", encoding="utf-8") | ||
| outside.chmod(0o555) | ||
| work = _tree(tmp_path, 0o111, nested=False) | ||
| (work / "link").symlink_to(outside, target_is_directory=True) | ||
| try: | ||
| docker_solver._rmtree(work) | ||
| assert not work.exists() | ||
| assert (outside.stat().st_mode & 0o777) == 0o555 | ||
| assert (outside / "keep.txt").read_text(encoding="utf-8") == "k" | ||
| finally: | ||
| outside.chmod(0o700) | ||
| if work.exists(): | ||
| _unlock(work) | ||
|
|
||
|
|
||
| @_NOT_ROOT | ||
| @_CAN_UNLOCK | ||
| def test_rmtree_closes_the_descriptors_it_opens(tmp_path): | ||
| work = _tree(tmp_path, 0o111, nested=True) | ||
| before = set(os.listdir("/proc/self/fd")) | ||
| try: | ||
| docker_solver._rmtree(work) | ||
| assert not work.exists() | ||
| assert set(os.listdir("/proc/self/fd")) == before | ||
| finally: | ||
| if work.exists(): | ||
| _unlock(work) | ||
|
|
||
|
|
||
| def test_rmtree_of_a_path_that_does_not_exist_is_no_error_and_no_warning(tmp_path, caplog): | ||
| with caplog.at_level("WARNING", logger=sandbox._LOG.name): | ||
| docker_solver._rmtree(tmp_path / "absent") | ||
| assert not [r for r in caplog.records if r.name == sandbox._LOG.name] | ||
|
|
||
|
|
||
| def test_rmtree_of_a_dangling_link_logs_that_it_stayed(tmp_path, caplog): | ||
| link = tmp_path / "work" | ||
| link.symlink_to(tmp_path / "nowhere") | ||
| with caplog.at_level("WARNING", logger=sandbox._LOG.name): | ||
| docker_solver._rmtree(link) | ||
| assert link.is_symlink() | ||
| (record,) = [r for r in caplog.records if r.name == sandbox._LOG.name] | ||
| assert str(link) in record.getMessage() | ||
|
|
||
|
|
||
| def test_rmtree_that_cannot_remove_the_tree_logs_its_path_and_does_not_raise(tmp_path, monkeypatch, caplog): | ||
| work = _tree(tmp_path, 0o700, nested=False) | ||
|
|
||
| def refuse(path, *args, **kwargs): | ||
| raise PermissionError(13, "Permission denied", str(path)) | ||
|
|
||
| monkeypatch.setattr(shutil, "rmtree", refuse) | ||
| with caplog.at_level("WARNING", logger=sandbox._LOG.name): | ||
| docker_solver._rmtree(work) | ||
| monkeypatch.undo() | ||
| assert work.exists() | ||
| (record,) = [r for r in caplog.records if r.name == sandbox._LOG.name] | ||
| assert str(work) in record.getMessage() and "Permission denied" in record.getMessage() | ||
|
|
||
|
|
||
| @_NOT_ROOT | ||
| def test_rmtree_without_the_repair_keeps_plain_removal_and_logs_what_stayed(tmp_path, monkeypatch, caplog): | ||
| real_isdir = os.path.isdir | ||
| monkeypatch.setattr(sandbox.os.path, "isdir", lambda p: False if p == "/proc/self/fd" else real_isdir(p)) | ||
| assert not sandbox._can_unlock() | ||
| work = _tree(tmp_path, 0o111, nested=False) | ||
| try: | ||
| with caplog.at_level("WARNING", logger=sandbox._LOG.name): | ||
| docker_solver._rmtree(work) | ||
| assert work.exists() | ||
| finally: | ||
| monkeypatch.undo() | ||
| _unlock(work) | ||
| (record,) = [r for r in caplog.records if r.name == sandbox._LOG.name] | ||
| assert str(work) in record.getMessage() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.