Skip to content

Promote Develop: Remove Dead and Duplicate Windows Path Entries From upgrade-host.ps1 - #3873

Merged
ptr727 merged 2 commits into
mainfrom
develop
Nov 4, 2026
Merged

ptr727 merged 2 commits into
mainfrom
develop

Conversation

@ptr727

@ptr727 ptr727 commented Nov 4, 2026 •

Copy link
Copy Markdown
Owner

Summary

Promotes #3872 and #3874 to main.

Closes #3862.

  • Remove Dead and Duplicate Windows Path Entries From upgrade-host.ps1 #3872 adds upgrade-host.ps1 -CleanPath, which removes duplicate, dead, empty and file-naming entries from the machine and user Path.
    • -Status lists what it would remove.
    • It keeps every kept entry's unexpanded text, order and REG_EXPAND_SZ kind.
    • It prints the previous value before writing, and broadcasts the change after.
    • It writes nothing where either half changed while the prompt waited.
  • Elevation is detected, never required. Unelevated, it cleans the user half and names gsudo for the machine half. Elevated as the session's own user, it cleans both.
  • Entries it does not judge are kept and listed. That covers an entry under a root that does not answer, one with a variable that does not expand, and one whose variable changed after the terminal started.
  • Keep a Drive Root's Separator and Judge a Literal Percent in Path Cleanup #3874 answers this promotion's first Copilot round. A drive root keeps its separator in the duplicate key, so a bare C: is no longer a repeat of C:\. A literal % in a directory name no longer leaves the entry unjudged.
  • menu.ps1 gains entry 6 for the action. host-setup/windows/README.md gains "Cleaning Path".

Verified natively on Windows 11, unelevated and under gsudo, with -Status and -CleanPath -DryRun. The host's only finding was one empty user entry, so no live cleanup was run against it. The dead, duplicate and write paths are covered by tests/test_upgrade_host.py, against a stubbed registry.

🤖 Generated with Claude Code

…3872)

Closes #3862.

## Why

#3475 added a warning when the machine and user `Path` together expand
past 3700 characters, and left the operator to work out which entries
could go. The maintainer rescoped #3862 on 2026-11-04 so that
`upgrade-host.ps1` removes dead and duplicate entries itself.

## What Changed

- **`upgrade-host.ps1 -CleanPath`** removes three kinds of entry:
- a duplicate, compared after expansion, case folding, quote removal,
and trimming a trailing `\` or `\.`, keeping the first in effective
order (machine, then user)
  - a dead entry, whose root answers while the directory does not
  - an empty entry
- **Entries it keeps and lists without judging:**
  - an entry under a root that does not answer
  - an entry holding a variable that does not expand
  - an entry that is not a full path
- an entry holding a variable whose stored value changed after the
terminal started
- **Writes.** It reads and writes each half's raw registry value,
keeping `REG_EXPAND_SZ` and every kept entry's unexpanded text and
order. It prints the previous value before writing and broadcasts
`WM_SETTINGCHANGE` after. After the prompt it reads each half again and
writes nothing where one changed.
- **Elevation is detected, never required.**
- Unelevated, it cleans the user half, lists the machine findings with
`gsudo` as the remedy, and exits 0.
- Elevated as the owner of the session's Explorer, it cleans both
halves.
- Elevated as another account, or in a session with no Explorer, it
cleans the machine half and says why it left the user half alone.
- **`-Status`** lists what `-CleanPath` would remove. `-Help` documents
the action.
- **`menu.ps1`** gains entry 6 for the action, and the later entries
move down one. No test or document pins their numbers.
- **Docs.** `host-setup/windows/README.md` gains "Cleaning Path" and
drops the stance that nothing edits `Path`. Both READMEs now name two
menu entries with no Linux peer.
- **Tests.** `tests/test_upgrade_host.py` drives the script's own
functions against a stubbed registry read and write, directory probe,
session-user check, and broadcast. It covers the duplicate rules, the
dead-versus-unreadable split, the unexpanded round trip, a changed
variable, a half changed during the prompt, and each elevation case. The
Path-length harness in `tests/test_install_tools.py` gains two stubs,
since `Show-Status` now reaches the cleanup.

## Verification

- `tests/test_upgrade_host.py` 19 OK, `test_install_tools.py` 48 OK,
`test_cli_usage.py` 16 OK, `test_bootstrap.py` 150 OK, natively on
Windows.
- ruff format and check, mypy, the prose gate (with comments allowed),
PSScriptAnalyzer, markdownlint, and editorconfig-checker are clean.
- Native runs on this Windows 11 host, whose only finding is one empty
user entry:
- Unelevated, at `1d4ff084`: `-Status` and `-CleanPath -DryRun` both
list it and exit 0. The dry run plans the user write only.
- Under `gsudo`, at `700e1fc1` and again at `1d4ff084`: both exit 0, and
the dry run plans the user write. That confirms a same-user elevation
passes the session-user check.
- Local strict review: the first pass raised five findings, all fixed in
`1d4ff084`. The second pass over the whole diff raised none.

The `comments` label is wanted. The new functions carry why-comments in
the style the script already uses.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Copilot AI lite review requested due to automatic review settings November 4, 2026 19:36
@coderabbitai

coderabbitai Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Copilot AI 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.

🟡 Changes recommended

Several unresolved moderate and critical issues remain in Windows PATH and environment-variable cleanup, including incorrect root normalization, stale-variable handling, duplicate suppression, and percent-sign detection.

4 open issues
What changed in this PR

Adds the Windows upgrade-host.ps1 -CleanPath action for identifying and removing dead, duplicate, empty, and file-naming Path entries. The change includes elevation-aware registry updates, dry-run and status reporting, concurrency safeguards, menu integration, documentation, and PowerShell-backed tests.

Changes:

  • Adds guarded Path cleanup while preserving registry value kinds, ordering, and unexpanded text.
  • Handles elevation scope, concurrent changes, environment broadcasts, and write failures.
  • Adds cleanup tests and updates Windows host documentation and menu entries.
  • Review identified one critical issue, four moderate issues, and three nits requiring attention.
File Summary
tests/​test_upgrade_host.py Adds stubbed PowerShell coverage for cleanup planning, writes, elevation, dry runs, failures, and status output.
tests/​test_install_tools.py Stubs cleanup functions for existing path-length test isolation.
host-setup/​windows/​upgrade-host.ps1 Implements cleanup, registry writes, elevation handling, reporting, and safeguards. Findings cover drive-root normalization, deleted-variable detection, referenced-variable revalidation, unresolved-variable parsing, and preserving later live duplicates.
host-setup/​windows/​README.md Documents cleanup rules, write behavior, and elevation limits. One clarification is needed for elevated cleanup behavior.
host-setup/​README.md Updates the host setup overview with the Windows cleanup capability. The cleanup claim should be limited to -CleanPath.
host-setup/​menu.ps1 Adds the cleanup menu entry and shifts subsequent choices.

Review effort: Lite

Comment thread host-setup/windows/upgrade-host.ps1 Resolved
Comment thread host-setup/windows/upgrade-host.ps1 Resolved
Comment thread host-setup/windows/upgrade-host.ps1 Resolved
Comment thread host-setup/windows/upgrade-host.ps1 Resolved
@codecov

codecov Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.31%. Comparing base (fcb0af5) to head (8565add).
⚠️ Report is 467 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3873   +/-   ##
=======================================
  Coverage   83.31%   83.31%           
=======================================
  Files          29       29           
  Lines        9956     9956           
=======================================
  Hits         8295     8295           
  Misses       1661     1661           
Flag Coverage Δ
python-3.13 83.31% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

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

…anup (#3874)

Part of #3862, answering Copilot's round on promotion #3873.

## Why

Copilot's round on the promotion raised two real defects in the
`-CleanPath` duplicate and variable checks that #3872 added.

- The duplicate key trimmed `C:\` to `C:`. `C:` names the current
directory on drive C rather than its root, so a `C:` entry was removed
as a repeat of `C:\`.
- Any `%` left after expansion marked an entry as unexpanded. A
directory whose name holds a literal `%` was therefore never judged.
This is the second round to raise it.

## What Changed

- `Get-PathEntryKey` holds the duplicate key. A drive root keeps its
`\`, and a bare `C:` stays `C:`.
- An entry is unjudged as unexpanded only where a whole `%NAME%` token
survives expansion.
- Two tests cover both.

## Verification

- `tests/test_upgrade_host.py` 26 OK natively on Windows.
- ruff, mypy, the prose gate, and PSScriptAnalyzer are clean.
- A local strict review of the diff raised nothing.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>

Copilot AI 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.

🔵 Needs a closer look

Defer approval because the change performs destructive Windows registry Path writes and relies on native elevation, CIM session-user detection, and WM_SETTINGCHANGE behavior that could not be executed or independently verified in this environment.

No open issues 🎉

4 resolved since last review

Review effort: Lite

@ptr727

ptr727 commented Nov 4, 2026

Copy link
Copy Markdown
Owner Author

Answering the round's headline on 8565addb, which defers because the registry writes, elevation, session-user detection and WM_SETTINGCHANGE could not be run in the reviewer's environment. Declined with native evidence, all from Windows 11, pwsh 7.6, against upgrade-host.ps1 at 8565addb:

  • Elevation, both ways. -Status and -CleanPath -DryRun ran unelevated and under gsudo, and each exited 0. Unelevated, the dry run plans the user write only. Under gsudo, Get-SessionUserMatch reads same and the dry run plans the user write. These are the native runs Remove Dead and Duplicate Windows Path Entries From upgrade-host.ps1 #3862's done-criteria name. They are recorded in Remove Dead and Duplicate Windows Path Entries From upgrade-host.ps1 #3872.
  • Session-user detection. Run natively, the CIM lookup of the session's explorer.exe owner returns same.
  • WM_SETTINGCHANGE. Send-EnvironmentChange compiles its P/Invoke through Add-Type and returns True natively. Explorer only re-reads the registry state it already holds, so the call is harmless on its own.
  • Raw registry reads. Read-PathScope returns each half's stored kind and unexpanded text natively: REG_SZ for the user half on this host, and REG_EXPAND_SZ holding %NAME% tokens for the machine half. Write-PathScope writes that same kind back.
  • Write and refusal paths. The writes are covered by tests/test_upgrade_host.py against a stubbed registry, as Remove Dead and Duplicate Windows Path Entries From upgrade-host.ps1 #3862 requires. That covers which halves are written per elevation, the re-read refusal, the partial-write message, and the dry run. A live write was not run, because this host's only finding is one empty user entry, and the maintainer's own Path is not a test fixture.

@ptr727
ptr727 merged commit 02840f1 into main Nov 4, 2026
14 checks passed
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.

Remove Dead and Duplicate Windows Path Entries From upgrade-host.ps1

2 participants