Repository navigation
perf(install): exact Windows path resolution with fewer kernel opens - #5374
Daniel Meppiel (danielmeppiel) merged 20 commits into
Conversation
Form B answers Path.resolve() with one _getfinalpathname (CPython makes two). Form A walks down from directories held open for the invocation: the project root and package roots registered after activation places them. Volatile paths (staging, .inc., .tmp, .git) always take Path.resolve. APM releases holds before it renames or deletes, before user scripts, and at scope exit. Refs #4939 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Existing installs and direct downloads skip activation, so they were never registered and their paths took form B only. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Containment checks run before the write, so most deploy paths are missing leaves; they fell through to ntpath's non-strict walk. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Share access ignores opens without read, write or delete access, so a hold with attribute access alone never refused a rename or delete. Adds the windows-latest cells for W3d R12. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…3d R6) Only activation registers held roots; every raw directory rename or delete in the apm_modules-mutating modules releases holds first or is allowlisted with a reason. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved Windows handle-lifetime and junction-validation races can undermine containment checks and cause sharing violations during staging and failed-install cleanup.
4 open issues
What changed in this PR
Adds Windows-specific fast paths to APM's shared path resolver to reduce cold-install overhead, with stock Path.resolve() as the fallback.
Changes:
- Introduces direct Windows path lookup and invocation-scoped directory holds.
- Releases holds around filesystem mutations, user scripts, and invocation cleanup.
- Adds Windows regression tests, architecture guards, and a performance changelog entry.
| File | Description |
|---|---|
tests/unit/scripts/test_architecture_runner.py |
Registers the two new architecture rules in the expected inventory. |
tests/quality/promise-catalog.json |
Adds the held-directory release promise. |
tests/integration/test_realpath_win.py |
Tests resolution parity, directory holds, releases, and volatile paths. |
tests/integration/test_architecture_owner_rule_mutations.py |
Adds mutation cases for registration and release guards. |
tests/integration/test_architecture_held_directory_release.py |
Tests guard detection and allowlist behavior. |
src/apm_cli/utils/realpath.py |
Routes Windows paths through the new resolver. |
src/apm_cli/utils/realpath_win.py |
Implements Windows fast paths and directory-hold management. |
src/apm_cli/utils/file_ops.py |
Releases holds before removal, replacement, and lock retries. |
src/apm_cli/install/resolution_staging.py |
Releases holds around staging mutations and rollback. |
src/apm_cli/deps/registry/resolver.py |
Releases holds before clearing installation targets. |
src/apm_cli/deps/plugin_parser.py |
Releases holds before removing stale normalized skills. |
src/apm_cli/deps/apm_resolver.py |
Registers validated package roots after placement. |
src/apm_cli/core/lifecycle_scripts.py |
Releases holds before running user command scripts. |
src/apm_cli/core/invocation_memo.py |
Connects hold lifetime to invocation state and memo invalidation. |
src/apm_cli/commands/uninstall/local_reanchor.py |
Releases holds around materialization moves and cleanup. |
src/apm_cli/commands/uninstall/engine.py |
Releases holds during local refresh and recovery. |
src/apm_cli/commands/deps/cli.py |
Releases holds before dependency-directory cleanup. |
src/apm_cli/cache/locking.py |
Releases destination holds before atomic landing. |
scripts/architecture_linter/groups/transport_platform.py |
Includes the new held-directory rules. |
scripts/architecture_linter/checks/transport_held_directory_release.py |
Guards root registration and release-before-mutation routing. |
changelog.d/5374.changed.md |
Records the reported Windows cold-install CPU improvement. |
.apm/architecture/owners/transport-auth-platform.json |
Declares the canonical hold-management owner and guards. |
Review effort: Balanced
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…gineer-windows-paths
… oracles In-process cells move to tests/unit (runs on windows-latest); the three that need a second process stay in integration. The receipts root-count oracle also counts the Windows fast form's final-path call. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Other platforms run the unchanged function with no dispatch check. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…gineer-windows-paths
Holds open under the table lock, so release never misses one; a directory that cannot be pinned voids the walk; every hold a walk relied on must still be the same hold when it ends; a created root's discard releases first. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Stacked on #5374 so resolve_path has one owner path: proofs first, then the platform form (Linux kernel, Windows forms A/B, else Path.resolve). R1: a directory is proven only if lstat sees it as an existing, non-link directory now; a missing tail never proves its directory ahead of time. R2: forget_proofs drops proofs at or below a path; realpath_win.release_held, which APM already calls before every rename or delete, calls it first on every platform. R3: nothing under a volatile component (.git, staging, .inc., .tmp, backups) is proven. apm-spec-waiver: containment results are unchanged (same resolved paths); no OpenAPM requirement changes. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Directories above a registered root are never held; the registered root's own hold re-proves the chain (its final path must equal the proof). Adds a cell that a held package pins its parent against another process. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
Windows anchor caching can report stale in-project paths after junction mutation redirects resolution outside the project, alongside unresolved handle-lifecycle races and a non-Windows subclass-dispatch regression.
4 open issues
4 resolved since last review
Review effort: Balanced
…hold W3d RP1-RP3: every hold an answer rests on (anchor, pinned intermediates, the exact-anchor answer, the cwd root) is re-read on its own handle (FileAttributeTagInfo); a reparse point or a failed read drops the hold and the answer falls back to Path.resolve. Holds are closed under the table lock. Cell: an empty held package dir converted in place to a junction. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
portable_relpaths keeps both fast forms: the Linux kernel form (#5380) and the Windows forms; Path.resolve elsewhere.
Windows keeps no containment proofs: every resolve takes #5374's exact forms, which measured faster there without them (W cold 1.011 with K on top of #5374). The Windows proof path (_plain_windows, _windows_leaf) is removed; Linux and macOS keep K, default-on with no flag. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
Extended-length Windows paths can bypass hold release, causing APM’s own sharing violations during renames or partial recursive deletion before a held directory blocks removal.
1 open issue
4 resolved since last review
Review effort: Balanced
An extended-length, UNC or 8.3 spelling cannot be matched against the plain keys, so it now releases all holds instead of none. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…y_snapshot.py in budget _is_path_mutation takes the <path>.rename/replace/rmdir/symlink_to cases out of _is_raw_mutation (PLR0911), same verdicts. A docstring rewrap brings deploy_snapshot.py back to 1000 lines after the #5374 merge. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
portable_relpaths takes main's form unchanged (fast forms on Linux and Windows, each path's own resolve elsewhere). The receipts root-count oracle counts the openat2 fsencode and the Windows final-path call.
… (with #5372) #5372's openat2 proves a Linux path link-free in one syscall, so Linux now keeps no proofs either (no faster with them, and their lstats moved the update count budget). K stays default-on on macOS and other POSIX. portable_relpaths keeps resolve_path, which defers to a subclass's own resolve(). K cells force the no-kernel branch with #5372's refused state; the platform cell checks Linux and Windows keep no proofs without patching. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🔵 Needs a closer look
Security-sensitive Windows containment depends on native handle and reparse-point behavior under concurrent mutation, warranting human review of handle-lifetime tradeoffs and Windows CI evidence across supported Python versions.
No open issues 🎉
2 resolved since last review
Review effort: Balanced



Description
Windows path resolution was about a third of Windows cold-install CPU.
ntpath.realpathopens every path twice, andPath.resolveadds a stat. This PR givesresolve_pathan exact Windows branch in a new module,utils/realpath_win.py. Anything it cannot prove falls back to stockPath.resolve()._getfinalpathname, the first call CPython makes. The\\?\strip is proven by construction for plain Win32 paths: drive root, under MAX_PATH, and no 8.3 name, trailing dot or space, ADS, device name or non-ASCII text. When the tail does not exist,lstatproves it is absent (not a dangling link), and the answer is the final path of the nearest existing ancestor plus the tail. That is exactly CPython's non-strict walk.invocation_scope), some directories are held open without FILE_SHARE_DELETE. The holdable set is the cwd root (only if its final path equals its text) and package roots registered byAPMDependencyResolver._activate_validated_packageonce a package is at its final placement. Each component below the deepest held directory is proven withFindFirstFileExW: the on-disk long name must equal the text and it must not be a reparse point..git, staging,.inc.,.tmp, backup, migration and refresh paths always take stockPath.resolve. APM deletes there, and a delete-pending entry changes CPython's answer.release_held(under)closes the holds at or below a path and unregisters the package that contains it. It runs atrobust_rmtree,robust_replace,_retry_on_lock(release-all before any transient retry),atomic_land, everyresolution_staginglive-path move and rollback, uninstall local refresh/re-anchor, anddeps clean. Before user lifecycle scripts and inforget_invocation_memosit releases everything (R4), and invocation scope exit releases everything (R5).tests/integration/test_realpath_win.py. They cover the differential, the held-dir swap refusal, APM ops on held dirs, case variants, the middle junction, the lifecycle rename, a concurrent foreign rmtree, the hold count back to 0, and volatile bypass.Population: every Windows install (anonymous and authenticated, any host). The change is not TLS- or network-dependent. On Linux and macOS the module is inert (
ENABLEDis false). IFP's call-count cuts (#5372) and Bets' containment-once (#5382) stack on top: they remove calls, this makes each remaining Windows call cheaper.Note (W3d R11): while a command runs, an IDE or antivirus that tries to delete or rename a held package directory gets a sharing violation until the command ends.
Issue and approved scope
Issue: Refs #4939
Human scope-approval comment: none yet for this item. Ordered by the PERF lead chain under #4939 (plan rank 2, Windows paths v2); W3d ruled R1-R12 OK with conditions.
This does not complete #4939.
Type of change
Testing
Validation is CI only: this PR's CI (windows-latest cells in
tests/unit/utils/test_realpath_win.pyandtests/integration/test_realpath_win.py, and the R6 rulestransport-platform-held-root-registration/transport-platform-held-directory-releasetested intests/integration/test_architecture_held_directory_release.py), plus the CI perf bench (scripts/perf/bench_ci.sh). Paired CPU p50 against current main 7596d5b, at c109777 (src-identical to this head exceptrelease_held, which is off the hot path) and at c979581:_DIFFERENTIAL=True, every fast answer checked againstPath.resolve()), install-cold t10/t50Request counts are identical on every row; errors are 0/0.
The win is smaller than this PR's earlier 0.911 (vs 7521c35): #5370 and #5380 on main have since removed part of the same Windows path calls. The levers stack. Once #5376 lands, the default-schannel number is expected (not claimed) to converge on the anonymous one.
On Linux and macOS,
resolve_pathis main's function unchanged; the Windows one is bound at import.portable_relpathsusesresolve_pathwhere an exact fast form exists (the Linux kernel form, Windows). The release binary builds on Python 3.12, the same as the bench and differential.Changelog
changelog.d/<pr>.<section>.md, holdingone line: user-visible effect first, 25 words or fewer, ending with
(#PR); breaking changes only under upgrade notes; no Highlights; nonew lines under
## [Unreleased]. Seechangelog.d/README.mdand.github/instructions/changelog.instructions.md.Spec conformance (OpenAPM v0.1)
CONFORMANCE.{md,json}regenerated