Skip to content

fix(harnesses): docker solver removes a kept work tree with an unlistable directory - #1638

Open
antonwolfy wants to merge 3 commits into
intel:mainfrom
antonwolfy:fix/rmtree-unlistable
Open

antonwolfy wants to merge 3 commits into
intel:mainfrom
antonwolfy:fix/rmtree-unlistable

Conversation

@antonwolfy

@antonwolfy antonwolfy commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

docker_solver._rmtree was shutil.rmtree(path, ignore_errors=True). _solve_claude calls it on output_dir / "work" to clear a previous run's kept agent tree and again before it copies the new tree in. When the agent left a directory at mode 0o111, 0o555 or 0o000, the removal failed silently, the old tree stayed and the shutil.copytree that followed rejected the existing destination with FileExistsError, which the solver logs ([keep work tree] ...), so the old snapshot was kept without the new files. On Linux as a non-root user, with work/sub/a.txt and chmod <mode> work/sub:

mode _rmtree(work) on main
0o111 work left, ['sub']
0o555 work left, ['sub']
0o000 work left, ['sub']

The same holds when the unlistable directory is nested one level down. AgentWorkspace.cleanup already repairs this (#1385, #1512); the issue's second option is taken here: the body of cleanup becomes a module-level remove_tree(path) in agent_sandbox.py, and both cleanup and _rmtree call it, so there is one copy of the logic and no second import path to keep in step.

Closes #1511

The same cause in command.clear_run_dir (harnesses/command.py:226, which raises instead of staying silent) is tracked in #1637. main also fails tests/test_import_architecture.py::test_package_uses_the_new_modules (dokimi.runs.resolve imports dokimi.utils), unrelated to this change (not touched here; filed separately).

Changes

  • harnesses/agent_sandbox.py: remove_tree(path) holds what AgentWorkspace.cleanup did: make each directory searchable and writable through a descriptor opened with O_NOFOLLOW, then shutil.rmtree; it never raises and logs tree <path> was not removed: <reason> when the tree stays. AgentWorkspace.cleanup calls it with self.root. The warning no longer says "work tree", since the staged directories use it too. Behaviour of cleanup is otherwise identical and its tests are unchanged.
  • harnesses/docker_solver.py: _rmtree calls remove_tree. Where the repair cannot run (no O_NOFOLLOW, O_DIRECTORY or /proc/self/fd), removal is plain as before, but a tree that stays is logged instead of silent.
  • tests/test_docker_solver_rmtree.py: the repro for each mode, flat and nested; the new tree copied after the removal holds only the new files (on main the copy fails with FileExistsError); a link out of the tree leaves its target alone; a missing path is no error and no warning; a tree that stays logs its path and the reason; no repair available logs what stayed; the directory descriptors remove_tree opens are all closed afterwards (the entries of /proc/self/fd are the same before and after); a dangling symlink at the path stays and is logged.
  • harnesses/agent_sandbox.py: remove_tree treats FileNotFoundError from shutil.rmtree as "gone" only when the path is not a symlink, so a dangling link is logged instead of silent.
  • Changelog fragment (Fixed).

Risk

A tree _rmtree could not remove used to stay silently; it is now removed, and one that still stays is logged at WARNING. _rmtree still never raises. A path that is a symlink (live or dangling) or otherwise not removable by shutil.rmtree now logs a warning where it was silent. Directories the repair opens are set to 0o700 inside the tree being deleted only; links are not followed.

Testing

On a Linux review host, at 4a99ec57 (on main 01ff2d6):

  • make ci-local: 36 passed, 2 failed, 16 skipped (matrix legs on other Pythons, jobs conditional on a PR, steps needing tools not on the machine). One failure is tests/test_import_architecture.py::test_package_uses_the_new_modules (['dokimi.runs.resolve'] import dokimi.utils, dokimi.runs.resolve imports dokimi.utils, which test_package_uses_the_new_modules refuses #1629), which fails identically on main's tree (not touched here). The other is pip-audit, which reports advisories against filelock 4.2.4 from the lock (Drop the filelock override once a litellm release allows filelock 4 #383), not touched here. The full suite: 35418 passed, 1 failed, 28 skipped; gitleaks: no leaks found.
  • On main's code the new test file gives 12 failed, 1 passed (the one that passes is the missing-path test, which holds on both); the first failure is assert not work.exists() for 0o111.
  • Deleting os.close(root) in remove_tree fails the descriptor test (1 failed); making the dangling-link case always silent fails the dangling-link test (1 failed).
  • Mutants, one per copy, each fails the new tests: dropping _unlock_below from remove_tree (8 failed), _rmtree back to ignore_errors=True (10 failed), dropping the warning (2 failed), logging a path that is gone (1 failed), _can_unlock always true (1 failed), the warning without the reason (1 failed).
  • The link-out test fails only with both O_NOFOLLOW in _open_searchable and is_dir(follow_symlinks=False) in _unlock_below removed; either alone leaves it passing, as the two layers overlap.
  • Merged together with fix/unlistable-root-readers, fix/unlistable-repo-eval-readers, fix/unlistable-skill-docs on main 4024f25: pytest -n auto 35800 passed, 28 skipped, mypy clean, ruff check clean.
  • Not run: Python 3.11, 3.12 and 3.14, and the tests as root (the permission tests skip there).

Open questions

None.

Revert

A plain revert restores shutil.rmtree(path, ignore_errors=True) in _rmtree and the body of cleanup. Nothing is stored.

Checklist

  • Tests added/updated and pytest passes (one failure in the full run, test_import_architecture.py::test_package_uses_the_new_modules, fails the same on main)
  • ruff, mypy, and schema checks pass
  • Docs updated (or N/A) — see the docs-drift checklist in CONTRIBUTING.md
  • Self-reviewed per "Before opening a PR" in AGENTS.md
  • Each new raise/exit and test names the doc, contract or issue it traces to
  • No ad-hoc run artifacts committed
  • New files carry the SPDX license header (or are exempt in .github/.licenserc.yaml)

…able directory (intel#1511)

docker_solver._rmtree was shutil.rmtree(path, ignore_errors=True). A directory the agent left at mode 0o111, 0o555 or 0o000 made the removal fail silently, so the previous run's kept tree under output_dir/work stayed and the shutil.copytree that follows merged the new tree into it. On Linux as a non-root user, a work/sub at 0o111 left ['sub'] after _rmtree(work), for each of the three modes and for a nested directory.

The body of AgentWorkspace.cleanup (make each directory searchable and writable through a descriptor opened with O_NOFOLLOW, then remove, never raise, log the path when the tree stays) moves into a module-level remove_tree(path) in agent_sandbox.py. AgentWorkspace.cleanup calls it with self.root and _rmtree calls it too, so there is one copy of the logic. The warning now reads "tree %s was not removed: %s", since both callers use it. Where the repair cannot run, removal is plain and a tree that stays is logged instead of silent.

The existing cleanup tests pass unchanged: the one that patches shutil.rmtree patches the shared module attribute, which remove_tree calls.

Signed-off-by: Anton Volkov <antonwolfy@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The changelog, test comment, and PR description inaccurately describe a prevented merge rather than a stale snapshot blocking replacement and need correction.

1 open issue
What changed in this PR

1 new finding.

This PR addresses #1511 by reusing sandbox cleanup logic so unlistable kept work trees no longer block Docker solver snapshot replacement.

Changes:

  • Extracts shared remove_tree cleanup and uses it in the Docker solver.
  • Adds regression tests for permissions, symlink safety, and failure logging.
  • Adds a changelog entry.
File Description
dokimi/​tests/​test_docker_solver_rmtree.py Tests removal and snapshot replacement.
dokimi/​dokimi/​harnesses/​docker_solver.py Delegates removal to the shared helper.
dokimi/​dokimi/​harnesses/​agent_sandbox.py Extracts reusable permission-repair cleanup.
dokimi/​changelog.d/​20261204_100000_docker-solver-rmtree-unlistable.md Documents the cleanup fix.

Review effort: Balanced

@exai-bot

exai-bot commented Dec 4, 2026 •

Copy link
Copy Markdown

✅ OpenCodeReview: Review complete: 0 finding(s) across 4 selected item(s).

CI at 4a99ec5: 21 passing, 4 skipped.

Review details
  • Commit reviewed: 4a99ec5 against merge base 01ff2d6
  • Coverage: 4 files in 1 groups
  • Engine: OpenCodeReview v1.13.5-exai.4.8f73eada, us.openai.gpt-6.1-sol
  • Usage: 88k tokens (42k from cache), 20s
  • Tool calls: 9 (5 file_read, 2 code_search, 1 ci_status, 1 local_check_results)
  • Robust sandbox cleanup for unlistable directories: 4 files

@napetrov napetrov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change class: CONTRACT (head 4e4d0ad)

  • Contract: changed (count signal only, no file:line)
  • Responsibility: none
  • Dependency: none
  • Review depth: check the changed surface: callers, docs, tests pin new behaviour
  • Coverage gaps: none

No new blocking findings on 4e4d0adc.

An offline probe confirmed that an old snapshot prevented replacement on the base. The head removes it and saves the fresh snapshot. The description and changelog should say the old snapshot was retained. Earlier discussion

CI was still running for Intel CI; ci.

Reviewed the diff, edge cases and prior threads. Focused tests: 11 passed on head. The full local suite was outside coverage.

@codecov-commenter

codecov-commenter commented Dec 4, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.43%. Comparing base (01ff2d6) to head (4a99ec5).
⚠️ Report is 40 commits behind head on main.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1638      +/-   ##
==========================================
- Coverage   96.46%   96.43%   -0.03%     
==========================================
  Files         561      585      +24     
  Lines       74345    76601    +2256     
  Branches    12840    13176     +336     
==========================================
+ Hits        71716    73872    +2156     
- Misses       1609     1672      +63     
- Partials     1020     1057      +37     
Files with missing lines Coverage Δ
dokimi/dokimi/harnesses/agent_sandbox.py 98.43% <100.00%> (+<0.01%) ⬆️
dokimi/dokimi/harnesses/docker_solver.py 98.32% <100.00%> (ø)

... and 130 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Mutation of changed lines

✅ Tests catch all 13 small breaks of the lines this PR changes

At 4a99ec5: 13 caught · 0 missed

Warning only: a missed break does not fail the job. Updated on every push; the full report is in the ci run summary.

…merge

Settles the review point on the changelog fragment and the test docstring: `shutil.copytree` without `dirs_exist_ok` rejects an existing destination with `FileExistsError`, which the solver logs, so the old snapshot was kept without the new files. The fragment and the test docstring now say that instead of a merge.

Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
@antonwolfy

Copy link
Copy Markdown
Contributor Author

(commented) ### Change class: CONTRACT (head 4e4d0ad)

@napetrov (#1638 (review)): Review body with no change asked beyond its thread: the description and changelog wording about the retained old snapshot is settled in the thread above (cbce4e3). make ci-local at cbce4e3: 1 failed, 37 passed, 16 skipped; the failure is test_import_architecture.py::test_package_uses_the_new_modules (dokimi.runs.resolve imports dokimi.utils), which also fails on main and is tracked in #1629.

@antonwolfy
antonwolfy requested a review from napetrov December 5, 2026 10:16

@napetrov napetrov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change class: CONTRACT (head cbce4e3)

  • Contract: changed (count signal only, no file:line)
  • Responsibility: none
  • Dependency: none
  • Review depth: check the changed surface: callers, docs, tests pin new behaviour
  • Coverage gaps: none

No new actionable findings.

Docker solver cleanup reuses the workspace permission recovery so a kept snapshot can be replaced when its own directories have restrictive modes.

CI on head cbce4e3: 0 failing Actions jobs; merge base 01ff2d6 is behind base tip 5225c2a.

Compared the change and input edges against the merge base and checked existing threads and GitHub Actions. Runtime checks used focused Linux tests and local fixtures. Focused tests on the head: head 11 passed in 0.29s.

@vchamarthi vchamarthi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@antonwolfy, could you fix the small thing I've marked fix before merge (in the line comments)? I'll approve once that's done.

Optional, non-blocking (1)
  • dokimi/changelog.d/20261204_100000_docker-solver-rmtree-unlistable.md:8: The changelog says a tree that stays is logged with its path, but a dangling symlink at the tree's path stays with no warning.

What I checked on cbce4e3: the 11 changed tests pass, and fail on main as expected; the full test suite passes with main merged in (36,229 tests), apart from failures main has too; it merges cleanly onto main; when I changed the code on purpose, the tests still passed for 1 of 10 changes; the line comments say where.

…s a dangling link that stays

Settles two review points. The first: deleting os.close(root) in remove_tree left every test passing, so a new test compares the entries of /proc/self/fd before and after removing a tree with unlistable nested directories.

The second: a dangling symlink at the tree's path stayed with no warning, because shutil.rmtree raises FileNotFoundError for it and that was read as "gone". remove_tree now treats FileNotFoundError as gone only when the path is not a link, and logs the link otherwise; a test covers it.

Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
@antonwolfy

Copy link
Copy Markdown
Contributor Author

(commented) ### Change class: CONTRACT (head cbce4e3)

@napetrov (#1638 (review)): No findings, no change. The head's tests pass here too; the checks I ran are in the description's Testing section.

@antonwolfy

Copy link
Copy Markdown
Contributor Author

(commented) @antonwolfy, could you fix the small thing I've marked fix before merge (in the line comments)? I'll approve once that's done.

@vchamarthi (#1638 (review)): The blocking point is answered in the reply to the thread on agent_sandbox.py:250 (the descriptor test, 4a99ec5). On the optional point: a dangling symlink at the tree's path did stay without a warning (I reproduced it: shutil.rmtree raises FileNotFoundError, which was read as "gone"). remove_tree now treats that error as "gone" only when the path is not a symlink, so the link is logged; test_rmtree_of_a_dangling_link_logs_that_it_stayed fails without it (4a99ec5). The changelog sentence is now true as written.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docker_solver._rmtree leaves the previous work tree behind when the agent made a directory unlistable

6 participants