Skip to content

perf(install): exact Windows path resolution with fewer kernel opens - #5374

Merged
Daniel Meppiel (danielmeppiel) merged 20 commits into
mainfrom
danielmeppiel-perf-engineer-windows-paths
Nov 25, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 20 commits into
mainfrom
danielmeppiel-perf-engineer-windows-paths

Conversation

@danielmeppiel

@danielmeppiel Daniel Meppiel (danielmeppiel) commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Windows path resolution was about a third of Windows cold-install CPU. ntpath.realpath opens every path twice, and Path.resolve adds a stat. This PR gives resolve_path an exact Windows branch in a new module, utils/realpath_win.py. Anything it cannot prove falls back to stock Path.resolve().

  • Form B: one _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, lstat proves 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.
  • Form A (held walk): within one CLI invocation (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 by APMDependencyResolver._activate_validated_package once a package is at its final placement. Each component below the deepest held directory is proven with FindFirstFileExW: the on-disk long name must equal the text and it must not be a reparse point.
  • Volatile paths: .git, staging, .inc., .tmp, backup, migration and refresh paths always take stock Path.resolve. APM deletes there, and a delete-pending entry changes CPython's answer.
  • A7, APM's own operations never blocked: release_held(under) closes the holds at or below a path and unregisters the package that contains it. It runs at robust_rmtree, robust_replace, _retry_on_lock (release-all before any transient retry), atomic_land, every resolution_staging live-path move and rollback, uninstall local refresh/re-anchor, and deps clean. Before user lifecycle scripts and in forget_invocation_memos it releases everything (R4), and invocation scope exit releases everything (R5).
  • R6: two closed-world architecture rules. Only activation registers held roots. Every raw directory rename or delete in the apm_modules-mutating modules releases first, or is allowlisted with a reason.
  • R12: windows-latest cells in 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 (ENABLED is 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

  • Bug fix
  • New feature
  • Documentation
  • Maintenance / refactor

Testing

  • Tested locally
  • All existing tests pass
  • Added tests for new functionality (if applicable)

Validation is CI only: this PR's CI (windows-latest cells in tests/unit/utils/test_realpath_win.py and tests/integration/test_realpath_win.py, and the R6 rules transport-platform-held-root-registration / transport-platform-held-directory-release tested in tests/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 except release_held, which is off the hot path) and at c979581:

cell result run
W anon install-cold t50 / t10 (c109777) 0.964 / 0.987 45472607052
W anon install-cold t50 / t10 (c979581) 0.950 / 0.979 45477799787
W auth install-cold-auth t50 0.985 45472686350
W default schannel (JH non-github host harness) t50 0.992 45473334263
Linux install-cold t50 / t10 (9 pairs) 1.002 / 0.987 45477246629
macOS install-cold t50 (9 pairs) 0.974 45472806158
Differential (_DIFFERENTIAL=True, every fast answer checked against Path.resolve()), install-cold t10/t50 0 errors 45477951083
A7 probe with real holds: uninstall, prune, update, install-warm, install-noop 0 errors 45477869633

Request 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_path is main's function unchanged; the Windows one is bound at import. portable_relpaths uses resolve_path where 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

  • One fragment per section, changelog.d/<pr>.<section>.md, holding
    one line: user-visible effect first, 25 words or fewer, ending with
    (#PR); breaking changes only under upgrade notes; no Highlights; no
    new lines under ## [Unreleased]. See changelog.d/README.md and
    .github/instructions/changelog.instructions.md.
  • N/A -- internal-only change (CI, tests, refactor).

Spec conformance (OpenAPM v0.1)

  • Spec edit
  • Manifest edit
  • Test edit
  • CONFORMANCE.{md,json} regenerated
  • N/A -- this PR does not change OpenAPM-observable behaviour.

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>
Copilot AI balanced review requested due to automatic review settings November 24, 2026 17:18
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

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

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

Comment thread src/apm_cli/utils/realpath_win.py Resolved
Comment thread src/apm_cli/utils/realpath_win.py Resolved
Comment thread src/apm_cli/utils/realpath_win.py Resolved
Comment thread src/apm_cli/utils/realpath_win.py Resolved
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Gated to Windows: on macOS the swap measured slower (#5354 bisect); Linux
is covered by #5372. Adds a Windows call-count cell and states in the R6
rule doc that no APM subprocess mutates registered packages (W3d N3).

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
… 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>
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>
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 24, 2026
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>

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

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

Comment thread src/apm_cli/utils/realpath_win.py Resolved
Comment thread src/apm_cli/utils/paths.py Resolved
Comment thread src/apm_cli/utils/realpath_win.py Resolved
Comment thread src/apm_cli/utils/realpath_win.py Resolved
…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.
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 24, 2026
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>

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

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

Comment thread src/apm_cli/utils/realpath_win.py Resolved
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>
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 24, 2026
…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>

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

Human assessment is needed because a subtle directory-to-junction race can bypass path-containment checks, creating a critical escape risk.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

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.

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

The test module violates the pure-unit boundary by including filesystem operations, and the changelog’s 8% improvement claim is unsupported by the updated measurements.

2 open issues

Review effort: Balanced

Comment thread changelog.d/5374.changed.md Resolved
Comment thread tests/unit/utils/test_realpath_win.py Resolved
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 25, 2026
… (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>

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

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

@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit 7721379 into main Nov 25, 2026
57 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the danielmeppiel-perf-engineer-windows-paths branch November 25, 2026 06:56
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.

2 participants