Skip to content

fix(config): add --clear flag to uv venv rebuild to prevent Windows bricking #37881#38051

Closed
kyssta-exe wants to merge 1 commit into
NousResearch:mainfrom
kyssta-exe:fix/issue-37881-venv-clear
Closed

fix(config): add --clear flag to uv venv rebuild to prevent Windows bricking #37881#38051
kyssta-exe wants to merge 1 commit into
NousResearch:mainfrom
kyssta-exe:fix/issue-37881-venv-clear

Conversation

@kyssta-exe

Copy link
Copy Markdown
Contributor

Summary

When hermes update triggers a venv rebuild on Windows, the running python.exe inside the venv is locked by the OS, so shutil.rmtree() silently fails to fully remove the directory. The subsequent uv venv command then fails with A directory already exists, leaving the venv in a half-deleted state (pyvenv.cfg gone, python.exe still present) which bricks the CLI with ModuleNotFoundError.

Fix: Adding --clear to the uv venv command makes it handle any leftover directory gracefully, recreating the venv even if the previous shutil.rmtree call was only partially successful.

Changes

  • hermes_cli/managed_uv.py: Add --clear flag to uv venv command in rebuild_venv()

Testing

Verified locally that:

  1. rebuild_venv() returns True with the --clear flag present
  2. rebuild_venv() returns False on uv venv failure (unchanged behavior)
  3. The fix works even when shutil.rmtree fails (Windows scenario)

Related

When shutil.rmtree fails to fully remove the old venv (e.g., on Windows
where the running interpreter locks files), uv venv without --clear would
refuse to overwrite the leftover directory, leaving a half-deleted venv
that bricks the CLI.

Adding --clear to the uv venv command ensures it handles existing
directories gracefully, even if shutil.rmtree partially failed.

Fixes NousResearch#37881
@alt-glitch alt-glitch added type/bug Something isn't working comp/cli CLI entry point, hermes_cli/, setup wizard area/config Config system, migrations, profiles P1 High — major feature broken, no workaround labels Jun 3, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Duplicate of #37895 — same one-line fix passing --clear to uv venv in rebuild_venv() to recover from a partially-deleted venv on Windows (locked python.exe). Both close #37881. #37895 was triaged P1; consolidating on whichever lands first.

lEWFkRAD pushed a commit to lEWFkRAD/hermes-agent that referenced this pull request Jun 3, 2026
`hermes update` can brick a Windows install. cmd_update guards against
concurrent hermes.exe processes and exits, but `hermes update --force` skips
that guard (and the guard's own message tells the user to pass --force).
Forced past it, rebuild_venv runs while the venv is still in use:

    shutil.rmtree(venv_dir, ignore_errors=True)   # deletes site-packages +
                                                  # certifi, fails on the
                                                  # locked python.exe ->
                                                  # half-gutted venv
    uv venv ...            (no --clear)           # aborts "directory already
                                                  # exists" -> never recreated

The venv is left with no pyvenv.cfg; every later HTTPS call dies with
FileNotFoundError (missing cacert.pem) and there is no recovery.

Move the old venv aside atomically with os.replace instead of deleting it in
place, pass --clear, and restore the moved-aside venv if the rebuild fails.
Verified on Windows 11: os.replace of a venv whose interpreter is live
succeeds (Windows tracks a running .exe by handle), so the rebuild actually
completes while the running gateway keeps using the moved-aside copy until it
restarts -- the update succeeds instead of bricking. If the venv genuinely
cannot be moved, the rebuild aborts cleanly and leaves it fully intact.

Root-cause fix for NousResearch#37881. The --clear-only approaches in NousResearch#37895 / NousResearch#38051
don't cover the real lock scenario: when the locked interpreter is inside the
venv being rebuilt, neither rmtree nor `uv venv --clear` can delete it.

- hermes_cli/managed_uv.py: atomic move-aside + --clear + restore-on-failure
- tests/hermes_cli/test_managed_uv.py: in-use venv left intact with no rebuild
  attempted; failed rebuild restored; success path asserts --clear
- website/docs/getting-started/updating.md: document --force venv-rebuild safety

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@teknium1

teknium1 commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Thanks for the sharp diagnosis here — your root-cause writeup (locked python.exeshutil.rmtree swallows the failure → uv venv aborts on the leftover dir → bricked CLI) was spot on, and your --clear fix is exactly right.

This is now fixed on main via #38887, which independently landed the same --clear insight plus post-install dependency verification (the pathspec-missing downstream symptom). Your co-authorship is preserved on the merge commit (Co-authored-by: Kyssta), and the issue #37881 you diagnosed is resolved.

Closing as merged via #38887. Appreciate the contribution!

@teknium1 teknium1 closed this Jun 4, 2026
@teknium1

teknium1 commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Closing as already fixed on main.

The Windows venv brick (#37881) was fixed in c136eb4 ("fix(update): harden venv rebuild + verify core deps after install"), which uses the same --clear mechanism you identified — implemented as a conditional retry (only retries with --clear when uv's stderr reports "already exists", so it doesn't mask real failures like disk-full or interpreter-download errors), plus a post-install dependency-verification pass as belt-and-suspenders.

Thanks for the clean fix and the precise root-cause writeup — you correctly diagnosed the locked-python.exe / rmtree interaction. Credit to you for independently landing on the right mechanism.

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

Labels

area/config Config system, migrations, profiles comp/cli CLI entry point, hermes_cli/, setup wizard P1 High — major feature broken, no workaround type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: hermes update bricks the install on Windows — venv rebuild leaves a venv with no pyvenv.cfg, then ModuleNotFoundError: hermes_cli

3 participants