Skip to content

perf(install): resolve each path once in deployment locators - #4761

Merged
Daniel Meppiel (danielmeppiel) merged 4 commits into
mainfrom
danielmeppiel-perf-rt06-noop
Nov 22, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 4 commits into
mainfrom
danielmeppiel-perf-rt06-noop

Conversation

@danielmeppiel

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

Copy link
Copy Markdown
Collaborator

Description

perf(install): resolve each path once in the deployment locators (rt-06, stat storm, L1).

On a no-op install, DeploymentLedgerCodec.locator_for_path resolved the candidate and the project root. ensure_path_within then resolved both again, and portable_relpath resolved them a third time: six realpath walks, each one lstat per path component, where two suffice. The deployment session also re-resolved its own root, which was already resolved at session init, and its per-target deploy roots on every call. On a tree-50 no-op this family drove about 828k lstat calls.

This PR makes two changes:

  • utils/path_security.py gains resolve_for_containment(path) (one resolve, extended-prefix stripped) and resolved_relative_within(resolved, resolved_base). The latter is a pure verdict on a pair already resolved for this check, and it strips the \\?\ prefix on both sides, as ensure_path_within_resolved does (Windows: _get_cache_dir flakes on tmpdir 8.3 short names (PathTraversalError) #886).
  • locator_for_path, _locator, _write_locator and deployed_path_entry resolve the candidate once per check and reuse it (candidate_resolved=True), instead of re-resolving it inside ensure_path_within and again inside portable_relpath.

Security invariants (H8):

  • Every base is resolved live for every check: the project root, skills roots and deploy roots. Nothing is cached across checks.
  • A candidate is resolved once per check and never reused across checks. ensure_path_within and its callers elsewhere are unchanged.
  • Red-first regressions cover two attacks. One plants a symlink under the skills root after a first check. The other retargets the skills root itself and links back into its old target, which the Copilot review found against an earlier per-run root cache that this PR has since dropped.
  • No hashing, policy or commit-gate code is touched. The owner-pinned deployed_path_entry calls stay verbatim (install-deployment-provenance-state).

Issue and approved scope

Issue: Refs #4732

Human scope-approval comment: PERF charter on #4732 (CEO re-charter 16:08: tree-10 no-op <= 1 s; rt-06 GO by PERF VP).

Does this PR complete the issue, or what remains? It is the first L1 slice of the stat storm. Further slices follow: portable_relpath callers in the integrators, security/gate, and deployable_source_plan.

Type of change

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

Testing

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

Gains (GAINS STANDARD)

  • Journey: apm install no-op (second run on an installed project), fixture tree-10 (tests/fixtures/perf, 10 packages, depth 5, 11 primitives per package), offline mirror at rtt 0, Python CLI.
  • SHAs: base 880e055cce7 (main) -> new: the P1 slice of this PR (locator_for_path resolves candidate and root once each). The final head 5a39f90c858c resolves the same or fewer paths per check. ABAB at the final head is running; its numbers follow in a comment.
  • ABAB (A B A B), CPU = user+sys of the CLI process tree (os.wait4):
run side CPU s wall s load1
1 A (base) 3.611 29.39 43.1
1 B (new) 3.408 23.88 41.0
2 A (base) 2.878 9.31 39.4
2 B (new) 2.489 8.85 38.6
  • CPU p50: 3.24 s -> 2.95 s (1.10x, -9.1% of the whole no-op). Wall p50 was taken at load 39-43, above the load-30 validity bar, so it is indicative only: 19.4 s -> 16.4 s.
  • Counts (one counted run per side, same project): lstat 166,380 -> 117,680 (-29%); stat 24,669 -> 20,149 (-18%). Hashes are unchanged (899 -> 899, 1.58 MB). Subprocesses are unchanged (10 -> 10). Project bytes were identical after every run.
  • North Star: the tree-10 no-op now stands at 2.95 s CPU against the 1 s target, a gap of 1.95 s. The no-op fast path (perf(install): replay a verified no-op install instead of re-running the pipeline #4775) and the integrate-plan work (perf(install): skip the receipt lookup when planning an identical file #4771) close the rest.
  • tree-50: from the rt-06 profile on base, this family (locator_for_path, ensure_path_within* and portable_relpath) made 828k lstat on the tree-50 no-op and scales linearly with N. The tree-50 ABAB at the final head follows in a comment.

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: docs/src/content/docs/specs/openapm-v0.1.md updated
    (new/changed <a id="req-XXX"></a> anchor + prose + Appendix C
    row).
  • Manifest edit: docs/src/content/docs/specs/manifests/openapm-v0.1.requirements.yml
    updated.
  • Test edit: a @pytest.mark.req("req-XXX") test under
    tests/spec_conformance/ added or extended.
  • CONFORMANCE.{md,json} regenerated via
    uv run --extra dev python -m tests.spec_conformance.gen_statement
    and committed.
  • N/A -- this PR does not change OpenAPM-observable behaviour.

locator_for_path resolved the candidate and project root, then
ensure_path_within resolved both again and portable_relpath a third time:
six realpath walks (one lstat per component) where two suffice. The
deployment session also re-resolved its own, already resolved, root and its
per-target deploy roots on every call.

Resolve each side once and decide containment on the resolved pair
(resolve_for_containment + resolved_relative_within). The session resolves
its deploy roots once per run; candidate paths are still resolved live for
every check, so a symlink planted mid-run is still refused (a stale cached
base can only refuse, never admit).

tree-10 no-op (ABAB, P1): lstat 166,380 -> 117,680, CPU 3.24 -> 2.95 s.

Co-authored-by: Copilot App <[email protected]>
@danielmeppiel Daniel Meppiel (danielmeppiel) added the performance Legacy classification name; prefer type/performance. Retained for history. label Nov 22, 2026

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

Receipt trust can accept paths outside a retargeted skills root because containment uses a cached base, while inconsistent Windows root normalization can reject valid project paths.

2 open issues
What changed in this PR

This PR reduces repeated filesystem resolution in install deployment locators, targeting the no-op install “stat storm” while preserving live resolution of candidate paths.

Changes:

  • Adds helpers that separate path resolution from lexical containment checks.
  • Reuses resolved inputs and caches deployment roots within a materialization session.
  • Adds containment and late-planted symlink regression tests.
File Description
tests/​unit/​test_path_security_io.py Tests resolved containment and symlink escapes.
tests/​unit/​test_deployment_materialization_receipts.py Tests live candidate resolution after roots are cached.
src/​apm_cli/​utils/​path_security.py Adds single-resolution containment helpers.
src/​apm_cli/​install/​deployed_paths.py Lazily resolves inputs and reuses normalized project roots.
src/​apm_cli/​core/​deployment_ledger.py Adds session root caching and pre-resolved locator inputs.

Review effort: Balanced

Comment thread src/apm_cli/core/deployment_ledger.py Resolved
Comment thread src/apm_cli/core/deployment_ledger.py Resolved
…efix on both sides

- Restore the two deployed_path_entry owner calls the
  install-deployment-provenance-state rule pins verbatim.
- resolved_relative_within strips the Windows \\?\ prefix on both sides,
  as ensure_path_within_resolved does (#886), so a prefixed session root
  never turns an in-project path into an external locator (W4 pre-review).
- Move the per-run root cache into path_security.ResolvedRoots and keep
  deployment_ledger.py within the 2100-line cap.

Co-authored-by: Copilot App <[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.

🟡 Changes recommended

A critical stale-base containment check can admit paths outside a retargeted deployment root in both locator functions, requiring live base resolution and a regression test for root retargeting.

3 open issues

Review effort: Balanced

Comment thread src/apm_cli/utils/path_security.py Resolved
…ndidate

Copilot review: a cached base admits a path when the skills root is
retargeted and a child links back into its old target. Drop the per-run
root cache: every base (project root, skills and deploy roots) is resolved
live for each check. The only saving kept is not re-resolving the same
candidate (and root) two or three times inside one check, which is where
locator_for_path spent most of its lstat calls.

Adds a red-first regression that retargets the skills root itself.

Co-authored-by: Copilot App <[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

The retrieved approval explicitly excludes src optimizations, and the claimed later re-charter was unavailable, so a maintainer must confirm the authorized scope for this security-sensitive refactor.

No open issues 🎉

3 resolved since last review

Review effort: Balanced

@danielmeppiel

Copy link
Copy Markdown
Collaborator Author

Gains at the final head (GAINS STANDARD)

  • Journey: apm install no-op, fixture tree-10, offline mirror at rtt 0, Python CLI.
  • SHAs: base 880e055cce7557d97df1998ea043085e20af0856 -> head 5a39f90c858cdf7e39cc7f74a252df34d3f0e04d.
  • ABAB, CPU = user+sys of the CLI process tree (os.wait4):
run side CPU s wall s load1
1 A (base) 3.989 79.23 59.2
1 B (head) 3.162 43.42 56.6
2 A (base) 3.521 59.40 56.2
2 B (head) 3.483 69.13 50.9
  • CPU p50: 3.755 s -> 3.323 s, a 1.13x speedup that removes 11.5% of the whole no-op.
  • In-process CPU (counted run): 4.10 s -> 3.66 s.
  • Wall p50 is invalid: it was taken at load 51-59, above the load-30 bar.
  • Counts: lstat 166,380 -> 111,644 (-33%) and stat 24,669 -> 19,661 (-20%). Hashes (899) and subprocesses (10) are unchanged.
  • Project bytes were identical after every run.
  • North Star: the tree-10 no-op is at about 3.3 s CPU on this loaded host (2.95 s in the quieter P1 run), a gap of about 2 s to 1 s.
  • tree-50: skipped under the PERF slot's 10-minute rule at load 55. On base, this path family accounts for 828k lstat in the tree-50 no-op and is linear in N, so the same -33% lstat share applies.

@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit eabffdb into main Nov 22, 2026
71 of 75 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the danielmeppiel-perf-rt06-noop branch November 22, 2026 17:14
Jason 'Jay' Jack (jasonjack-jay) pushed a commit to jasonjack-jay/Agent-Package-Manager that referenced this pull request Nov 23, 2026
…ntainment class

CI bench (paired CPU, 3 runners x interleaved pairs): f0dab6c -> 394e93c
tree-10 run 45075725397 paired 1.000-1.009 on no-op/warm/fresh/frozen;
tree-50 run 45076262295 paired 1.002 no-op, 1.010 locked-warm. Below the
PERF landing bar (>= 2x or >= 30%), so the CheckRoot class, its AST guard
and the bypass mutations are removed; microsoft#4761's resolve-once stays.

Refs microsoft#4732

Co-authored-by: Copilot App <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Legacy classification name; prefer type/performance. Retained for history.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants