Skip to content

perf(install): resolve below a live-resolved root instead of re-walking it - #4802

Merged
Daniel Meppiel (danielmeppiel) merged 8 commits into
mainfrom
danielmeppiel-perf-l1-slice2
Nov 22, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 8 commits into
mainfrom
danielmeppiel-perf-l1-slice2

Conversation

@danielmeppiel

Copy link
Copy Markdown
Collaborator

Description

perf(install): resolve below a live-resolved root instead of re-walking it (rt-06 stat storm, L1 slice 2; follows #4761).

After #4761, pathlib.resolve/realpath is still 19% of the tree-10 no-op CPU and 29% of tree-50 (plain-CPU ITIMER_PROF sampling at main ce977b27). realpath walks every component from /. For a candidate under the project root, that re-walks the root prefix the same check just resolved, about 12 lstat calls per path in a deep checkout.

This PR adds utils/path_security.CheckRoot. It is a trusted root resolved live for ONE containment check and never kept across checks:

  • CheckRoot(root) resolves the root now.
  • CheckRoot.resolve(path) returns exactly resolve_for_containment(path), but walks only the components of path below the root, lstat-ing each one now. It falls back to the full resolve on any symlink, ../. component, missing component, path outside the root, relative path, or non-POSIX platform. So the fast walk only runs when every suffix component exists and is a plain entry, and then realpath(root/suffix) == realpath(root)/suffix on every Python version.
  • CheckRoot.within(candidate, base) resolves base through the same check and applies resolved_relative_within.

DeploymentMaterializationSession._locator/_write_locator, DeploymentLedgerCodec.locator_for_path and deployed_path_entry build one CheckRoot per check and pass it along. The check parameter replaces #4761's candidate_resolved.

Security invariants (H8):

  • Nothing is cached across checks: each check resolves its root live, and every suffix component is lstated at check time. This is the same sequential observation realpath makes, in the same order.
  • Any symlink in the suffix takes the full realpath path. That covers links planted after the root was resolved (pinned by test_suffix_link_planted_after_the_root_was_resolved_is_followed) and the perf(install): resolve each path once in deployment locators #4761 retarget traps (test_locator_resolves_a_retargeted_skills_root_live, unchanged and passing).
  • Equivalence is pinned for every shape: a plain path, a link out of the root, a link inside the root, .., a missing component, a path outside the root, and a relative path (test_matches_full_resolution_for_every_shape).
  • The owner-pinned deployed_path_entry calls stay verbatim. No hashing, policy or commit-gate code is touched.

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 stat-storm lane L1 GO by PERF VP).

Does this PR complete the issue, or what remains? It is the second L1 slice. The remaining resolution callers (deployable_source_plan, filter_authorized_files, security/gate, skill_adoption) follow if the gains hold.

Type of change

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

Testing

  • Tested locally (lint mirror; arch boundaries; assertion-quality and complexity ratchets)
  • All existing tests pass (CI is the validation run under PERF rule 17)
  • Added tests for new functionality (if applicable)

Gains (GAINS STANDARD)

PENDING. PERF rule 17 (CEO 20:32) forbids any local measurement while the 1-min load is above 24 (it is about 48). The one ABAB this body needs (tree-10 no-op, main 6f1e56aa -> this head; CPU p50, lstat counts, % of the whole no-op, vs 1 s) runs in the shared slot as soon as the load allows, and the numbers are added here before H8.
Estimate and method: in #4761's counted no-op, the four functions touched here make about 40k of the remaining 111k lstat calls. Skipping the root-prefix re-walk removes roughly two thirds of those. At the sampled 19% resolve share, that is an estimated 4-7% of tree-10 no-op CPU, and more at tree-50, where resolve is 29%.

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.

…ng it

CheckRoot resolves a trusted root live for one containment check and
resolves candidates by lstat-ing only the components below it; any
symlink, '..', missing component, outside or relative path, or non-POSIX
platform falls back to the full resolve, so the result equals realpath.
The deployment locators build one CheckRoot per check; nothing is kept
across checks.

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 22, 2026 19:34
@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

A critical path-security flaw allows a replaced root or ancestor symlink to bypass the saved-root check and emit a project-relative locator for an outside path.

1 open issue
What changed in this PR

This PR continues #4761’s install-performance work by introducing a per-check CheckRoot helper intended to avoid repeated filesystem walks of the project-root prefix during deployment containment checks.

Changes:

  • Adds suffix-only resolution for plain existing paths, with full-resolution fallbacks.
  • Passes the check context through deployment locators and deployed-path encoding.
  • Adds resolution-equivalence and late-created symlink tests.
File Description
tests/​unit/​test_path_security_io.py Tests path-shape equivalence and suffix symlinks created during a check.
src/​apm_cli/​utils/​path_security.py Introduces CheckRoot resolution and containment helpers.
src/​apm_cli/​install/​deployed_paths.py Shares a lazily created check during target-path encoding.
src/​apm_cli/​core/​deployment_ledger.py Uses check contexts in session and ledger locator methods.

Review effort: Balanced

Comment thread src/apm_cli/utils/path_security.py Resolved
…he parity matrix

- CheckRoot records the resolved root's (dev, inode) at construction and
  re-stats it after every suffix walk; a root or ancestor swapped for a link
  mid-check falls back to the full resolve (Copilot review).
- Docstring: the root is resolved once per check; keep a CheckRoot for one
  call only (W4).
- Tests: leaf link, file-as-dir, symlinked root, '..' in the root, a
  retargeted root link seen by the next check, and a root swapped within one
  check. A mutant that skips the last component's lstat is killed.

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 root-validation gap allows renames or symlink retargeting to preserve inode identity while accepting project-relative locators that full resolution would reject against the saved root.

1 open issue
1 resolved since last review

Review effort: Balanced

Comment thread src/apm_cli/utils/path_security.py Resolved
…ot never outlives a call

- Docstring: the project root and its ancestors are trusted within one
  check; everything below the root is observed live (W4 condition a).
- transport-platform-url-path-security now forbids storing a CheckRoot on
  self/cls, a subscript or a module global (W4 condition b, VP ruling),
  proven by two confirmed-bypass mutations (session attribute, module
  cache). TestCheckRootLifetime also scans src/apm_cli by AST.

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

Both lifetime guards miss indirect and parenthesized multiline CheckRoot assignments, allowing prohibited cross-call caching to pass undetected.

1 open issue
1 resolved since last review

Review effort: Balanced

- New architecture check (scripts/architecture_linter/checks/path_check_root.py,
  under transport-platform-url-path-security): only _locator,
  _write_locator, locator_for_path and deployed_path_entry's _try_target may
  build a CheckRoot; a CheckRoot name (built, aliased or a CheckRoot-annotated
  parameter) may only be passed to _encode/_locator/locator_for_path, read
  through .resolve/.within/.resolved/.root, compared or aliased to a local.
  Returns, yields, containers, attribute/subscript stores and
  global/nonlocal fail. The line regex stays as a backstop.
- deployed_path_entry builds one CheckRoot per target attempt; no helper
  returns one.
- Five confirmed-bypass mutations (session attribute, alias store, multiline
  store, helper return, module cache) are caught; the real tree is clean.
- Merge main (symbol-cited lifecycle rule evidence, #4702).

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

Critical lifetime-guard gaps allow chained aliases and stored bound methods to retain CheckRoot instances across checks without triggering either guard.

2 open issues
1 resolved since last review

Review effort: Balanced

…ds (Copilot)

- Every plain-name target of a chained or annotated CheckRoot assignment is
  tracked, so check = alias = CheckRoot(...); self.kept = alias fails.
- .resolve/.within must be invoked on the spot; storing or returning the
  bound method (which keeps the instance) fails.
- Two more confirmed-bypass mutations (chained-alias store, bound-method
  store); all seven CheckRoot bypass forms are caught, the tree is clean.

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

Human assessment is needed to evaluate critical lifetime-guard bypasses involving escaping closures and aliased CheckRoot constructors.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

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 lifetime guards still allow CheckRoot values to escape through deferred closures and unrelated same-name receiver functions, enabling reuse across checks.

2 open issues

Review effort: Balanced

…ptures (Copilot)

Receivers must be self.<name> or DeploymentLedgerCodec.<name>; shadowing,
rebinding or importing a receiver name is a violation. A tracked CheckRoot
loaded inside a nested function, lambda or generator is a violation.
Adds confirmed-bypass mutations for both forms.

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

Human assessment is needed because critical gaps in import-alias and positional-only parameter tracking allow prohibited CheckRoot retention to evade lifetime enforcement.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit 8710b95 into main Nov 22, 2026
72 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the danielmeppiel-perf-l1-slice2 branch November 22, 2026 22:14
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 22, 2026
Merge main 8710b95 (#4802, folded into the install perf line) and the #4829 head df9490c (internal known-cell snapshot fix, no changelog entry).

Co-authored-by: Copilot App <[email protected]>
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 22, 2026
Drop #4802 from the install perf line: CI bench shows no speed gain, so it is internal.

Co-authored-by: Copilot App <[email protected]>
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 23, 2026
revert: #4802 path CheckRoot (zero measured gain on the CI bench)
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]>
Antonin Rouxel (Antonin-Rouxel-LaPoste-BGPN) pushed a commit to anrouxel/apm that referenced this pull request Nov 23, 2026
The microsoft#4802 revert (microsoft#4855) and microsoft#4818 landed together and took the file to
2101 lines. The restored _encode call takes one formatted line, as it
did before the revert; no behavior change.

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