Skip to content

fix(tests): refuse test writes under the real home - #4857

Merged
dan-petty merged 7 commits into
mainfrom
fix/4739-guard-real-home-writes
Nov 12, 2026
Merged

dan-petty merged 7 commits into
mainfrom
fix/4739-guard-real-home-writes

Conversation

@dan-petty

@dan-petty dan-petty commented Nov 11, 2026 •

Copy link
Copy Markdown
Owner

link_at_pin, called from plain Python, wrote a stub over 18 real tools under the real home. In pytest, the floor refused a write under the real home only for a config file or a spend ledger.

  • tests/support/home_write_guard.py adds a PEP 578 audit hook. It fails a test that writes, renames onto or removes a file under the real home, outside the run's temporary directory, the checkout and the interpreter, also when the code catches the failure. The hook calls no function that a test can replace, such as os.lstat.
  • isolate_home records the real home for each xdist worker.
  • hold_to_the_run refuses a path outside the active pytest run, or with no run. link_at_pin and global_config call it.
  • The write guards read their audit events from tests/support/file_changes.py.
  • docs/TESTING.md names the guard and the helper rule.

Closes #4739

Change: openspec/changes/archive/2026-11-11-4739-test-helpers-write-to-the-real/

@dan-petty dan-petty added type/bug Type bug: the code does not do what it must scope/tests The test suite, its fixtures, fakes and floor labels Nov 11, 2026
@dan-petty

Copy link
Copy Markdown
Owner Author

Test record

All runs are on the final code (head ef80c716, on origin/main 7f099217), unless the line says otherwise.

Before the fix: the final test file with the support code of origin/main (the base versions of bootstrap.py, config.py, config_write_guard.py, spend_write_guard.py and tools.py, and no home_write_guard.py or file_changes.py). uv run devops ci test tests/invariants/test_hermetic_floor.py -k "real_home or support_helper" gives 3 failed, 24 deselected. Each test fails for the defect:

  • test_a_write_under_the_real_home_fails_at_the_write_naming_the_test: the inner run gives {'passed': 8, 'failed': 1, 'errors': 0}, not 4 passed, 5 failed and 5 errors. Each write under the real home of the inner run goes through (assert 'stub\n' == 'real\n').
  • test_a_support_helper_writes_nothing_outside_the_run: Failed: DID NOT RAISE Failed. link_at_pin writes into a user data root outside the run.
  • test_a_support_helper_called_outside_pytest_writes_nothing: assert 0 != 0. The call from plain Python exits 0.

After the fix:

  • uv run devops ci test on the 12 test files that use the changed support code (tests/invariants/test_hermetic_floor.py, tests/config/test_isolation_tripwire.py, tests/ci/test_ci_cache_fingerprint.py, tests/commands/test_config_audit_keys.py, tests/commands/test_devcontainer.py, tests/commands/test_devcontainer_install_tools_dry_run.py, tests/commands/test_install_tools.py, tests/config/test_setting_sources.py, tests/config/test_settings_cache.py, tests/crypto/test_ssh.py, tests/review/test_review_untrusted_tree.py, tests/security/test_review_tool_runs.py): 469 passed. This run was on the head before the last two rebases. The rebases changed none of these files.
  • The xdist hand-off of the real home: uv run devops ci test -n 2 tests/invariants/test_hermetic_floor.py tests/config/test_isolation_tripwire.py tests/commands/test_install_tools.py: created: 2/2 workers, 93 passed.
  • Static rows: uv run devops ci --check --only lint,format,docs,constructs,openspec,test-settings,test-tree,startup-imports,swallowed-failures: all pass. After the rebase, lint,format,docs,openspec: all pass.
  • mypy: not run. The gate type-checks only src/ (TYPECHECK_PATHS), and this change touches no file under src/.
  • The full gate: not run. The required CI checks of this pull request are the merge gate.

Fail-to-pass

uv run devops ci fail-to-pass --base origin/main exits 1:

0 failed on base (0 failed and 27 passed in tests/invariants/test_hermetic_floor.py; 0 errored).
A fix needs at least one changed test that fails in the call phase on base (...)

The replay puts the tests/ tree of the head on the base. This fix is entirely in tests/support/, so the replay also runs the fixed support code, and the new tests pass. The Fail-to-Pass Check workflow runs only for a change under src/, so CI does not run it for this pull request. The "Before the fix" run above is the proof that the tests fail on the unfixed code. Issue #4554 reports the same limit for a manifest fix. A comment there adds this case.

Other open pull requests

No open pull request changes a file of this change.

Live steps

None. The change touches no cluster, ConfigMap or Secret.

…file system call

The guard resolved each written path with os.path.realpath, which calls
os.lstat. Three worktree tests replace os.lstat with a function that removes
a file, so the removal started the guard again until the recursion limit.
The guard now makes the written path absolute and normalized with no file
system call, and compares it with the given and the resolved form of each
root.
@dan-petty
dan-petty force-pushed the fix/4739-guard-real-home-writes branch from ef80c71 to 9e70a36 Compare November 12, 2026 00:20
@dan-petty

Copy link
Copy Markdown
Owner Author

Fix round: the home write guard and the replaced os.lstat

CI run 43112804828 failed in two shards. The home write guard caused both failures:

  • Shard 1: tests/git/test_repos_worktrees_reclaim.py::test_add_succeeds_when_merged_worktree_file_is_deleted_during_reclaim (assert 1 == 0, the command failed with RecursionError).
  • Shard 4: tests/git/test_repos_worktrees.py::test_directory_size_ignores_file_deleted_during_walk (RecursionError).

Cause: each test replaces os.lstat with a function that removes a file. The removal starts the audit hook, and the hook called os.path.realpath, which calls the replaced os.lstat, which removes the file again. The stack in the shard 4 log: deleting_lstat -> f2.unlink() -> home_write_guard.py:124 guard -> realpath -> deleting_lstat.

Fix (46019d57): the hook makes the written path absolute and normalized with no file system call, and compares it with the given and the resolved form of each root. It calls no function that a test can replace.

Test record

  • Red, on the guard before the fix: uv run devops ci test tests/invariants/test_hermetic_floor.py -k calls_no_function -> FAILED test_inner.py::test_replaces_lstat_with_a_function_that_removes_a_file - RecursionError: maximum recursion depth exceeded, 1 failed, 27 deselected.
  • Green, on the fix: uv run devops ci test tests/invariants/test_hermetic_floor.py tests/git/test_repos_worktrees.py tests/git/test_repos_worktrees_reclaim.py -> 72 passed.
  • Green, after the rebase onto origin/main (92087179): uv run devops ci test tests/invariants/test_hermetic_floor.py -> 27 passed.
  • uv run devops ci --check --only lint,format,docs,constructs,openspec,test-settings,test-tree,startup-imports,swallowed-failures -> all rows pass.
  • uv run ruff check and uv run ruff format --check on the changed files -> pass. MYPYPATH=src uv run mypy --strict tests/support/home_write_guard.py -> no issues. The gate type-checks only src/, and this change has no file in src/.

The fail-to-pass replay cannot prove a fix in tests/support/ (#4554), so the red and green runs above are the proof of this round.

@dan-petty
dan-petty marked this pull request as ready for review November 12, 2026 00:31
@dan-petty
dan-petty merged commit 0f6ac1b into main Nov 12, 2026
29 checks passed
@dan-petty
dan-petty deleted the fix/4739-guard-real-home-writes branch November 12, 2026 00:44
dan-petty added a commit that referenced this pull request Nov 12, 2026
The plain-Python helper test of the hermetic floor (#4857) started its
child process with `python -I`. `-I` drops `PYTHONPATH`. The Service
image test job finds pytest and the test dependencies only on
`PYTHONPATH`, so the child stopped at `import pytest`, and `Service
Image Tests` failed on main and on the release pull request.

The child now starts with `-P -s`: the working directory stays off the
import path, the user site directory stays out, and `PYTHONPATH` stays.
The guard and the helpers do not change.

Closes #4891

Change:
`openspec/changes/archive/2026-11-12-4891-helper-test-keeps-pythonpath/`
dan-petty pushed a commit that referenced this pull request Nov 12, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.115.0](v0.114.1...v0.115.0)
(2026-11-12)


### Features

* **design:** add the design tokens and their build
([#4811](#4811))
([db4b19a](db4b19a))
* **roadmap:** find symbol definitions with tree-sitter
([#4820](#4820))
([3b3fddc](3b3fddc))


### Bug Fixes

* **ai:** the gateway group that answered decides the local label
([#4907](#4907))
([0da637f](0da637f))
* **ci:** replay a changed gate check in fail-to-pass
([#4898](#4898))
([59bfc57](59bfc57))
* **config:** read each config file as YAML 1.2
([#4858](#4858))
([c3130a3](c3130a3))
* **roadmap:** loosen the caps that turn a stage refusal into a loss
([#4869](#4869))
([4492fc3](4492fc3))
* **roadmap:** refine reads a dependency named by title
([#4877](#4877))
([2829f9c](2829f9c))
* **roadmap:** refine sends a judge refusal to an agent round
([#4874](#4874))
([e4fc6a9](e4fc6a9))
* **tests:** refuse test writes under the real home
([#4857](#4857))
([0f6ac1b](0f6ac1b))
* **tests:** the plain-Python helper test keeps PYTHONPATH
([#4914](#4914))
([99c109d](99c109d))


### Performance Improvements

* **ci:** direct pytest runs wait for a gate slot
([#4901](#4901))
([e80db62](e80db62))
* **roadmap:** status lane reads the snapshot, skips idle stops
([#4887](#4887))
([f285cf8](f285cf8))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: dan-petty-release[bot] <346998822+dan-petty-release[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope/tests The test suite, its fixtures, fakes and floor type/bug Type bug: the code does not do what it must

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(tests): test helpers write to the real home outside pytest

1 participant