fix(update): refuse to mutate a venv containing foreign-owned files (#83529) - #99542
Merged
Merged
Conversation
…83529) A venv ever touched by sudo pip / sudo hermes contains root-owned files (classically site-packages/*.dist-info/INSTALLER). A later normal-user 'hermes update' pulls code fine, then 'uv pip install -e .' dies with 'Permission denied (os error 13)' mid-mutation — venv/bin/hermes already deleted, CLI bricked. Add a bounded, pure-stat ownership preflight (_venv_foreign_owned_paths) that runs after the code pull and immediately before the dependency install. If foreign-owned paths are found it refuses up front, names the offending paths + owner uid, prints the exact recovery command (sudo chown -R $(id -un): <root>), and confirms the venv is untouched. Windows (no os.geteuid) and root skip entirely. Never raises, capped at ~2000 stat calls, no subprocess use (update tests mock subprocess.run). Same refuse-before-mutate philosophy as the contended-venv gate (#87331). Fixes #83529 Diagnosis and documented recovery by @eabase.
This was referenced Aug 31, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Root cause
Issue #83529 ("hermes update - destroys hermes", Debian, many +1s): at some point the user ran hermes/pip under
sudo, leaving root-owned files inside the shared venv — classicallysite-packages/hermes_agent-*.dist-info/INSTALLER. A later normal-userhermes updatepulls code fine, thenuv pip install -e .fails:…mid-mutation. By that point
venv/bin/hermeshas already been deleted, so the CLI is bricked (No such file or directory). The recovery issudo chown -R user:user ~/.hermes/hermes-agent, but users have no way to know that — @eabase spent days diagnosing it and documented the fix in the issue.Fix: refuse before mutate
Same philosophy as the contended-venv gate (#87331): a venv we cannot safely mutate is never mutated at all.
_venv_foreign_owned_paths(venv_root, limit=5): a bounded, POSIX-only ownership scan — the venv root, direct entries ofvenv/bin, top-levelsite-packagesentries, and each*.dist-info's direct children. Hard-capped at ~2000stat()calls, per-entryOSErrorswallowed, returns[]on any structural surprise — never raises, never slow. Pureos.stat/os.scandir, no subprocess calls (update tests mocksubprocess.runwith sequenced side effects). Small_path_uid()seam so tests can simulate root-owned files without needing actual root._refuse_update_if_venv_foreign_owned(project_root)called in_cmd_update_implafter the code pull (pulling code is safe) and immediately before the dependency-install block (the first venv mutation).no os.geteuid) and root (euid == 0) skip the preflight entirely.What users see now vs before
Before: update dies halfway with a cryptic uv permission error and a deleted
venv/bin/hermes— Hermes won't start at all.Now, with the venv still fully intact:
Tests
New
tests/hermes_cli/test_update_venv_ownership_preflight.py(10 tests, all passing): all-owned → no-op; foreign-owned dist-info child detected; refusal message includes path, uid, chown command, "Nothing in the venv was modified";limitcap; no-geteuid(Windows) skip; root skip; never raises onPermissionErrorfromscandiror a missing venv. Existing update-path suites exercised locally; the handful of failures observed reproduce identically on pristineorigin/main(local-env preexisting, unrelated to this change).Fixes #83529
Credit: @eabase for the diagnosis and the documented
chownrecovery that this preflight now surfaces automatically.Infographic