Repository navigation
perf(install): resolve each path once in deployment locators - #4761
Conversation
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]>
Co-authored-by: Copilot App <[email protected]>
There was a problem hiding this comment.
🟡 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
…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]>
There was a problem hiding this comment.
🟡 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
…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]>
There was a problem hiding this comment.
🔵 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
Gains at the final head (GAINS STANDARD)
|
…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]>


Description
perf(install): resolve each path once in the deployment locators (rt-06, stat storm, L1).
On a no-op install,
DeploymentLedgerCodec.locator_for_pathresolved the candidate and the project root.ensure_path_withinthen resolved both again, andportable_relpathresolved them a third time: sixrealpathwalks, each onelstatper 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 828klstatcalls.This PR makes two changes:
utils/path_security.pygainsresolve_for_containment(path)(one resolve, extended-prefix stripped) andresolved_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, asensure_path_within_resolveddoes (Windows: _get_cache_dir flakes on tmpdir 8.3 short names (PathTraversalError) #886).locator_for_path,_locator,_write_locatoranddeployed_path_entryresolve the candidate once per check and reuse it (candidate_resolved=True), instead of re-resolving it insideensure_path_withinand again insideportable_relpath.Security invariants (H8):
ensure_path_withinand its callers elsewhere are unchanged.deployed_path_entrycalls 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_relpathcallers in the integrators,security/gate, anddeployable_source_plan.Type of change
Testing
Gains (GAINS STANDARD)
apm installno-op (second run on an installed project), fixturetree-10(tests/fixtures/perf, 10 packages, depth 5, 11 primitives per package), offline mirror at rtt 0, Python CLI.880e055cce7(main) -> new: the P1 slice of this PR (locator_for_pathresolves candidate and root once each). The final head5a39f90c858cresolves the same or fewer paths per check. ABAB at the final head is running; its numbers follow in a comment.os.wait4):locator_for_path,ensure_path_within*andportable_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
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)
docs/src/content/docs/specs/openapm-v0.1.mdupdated(new/changed
<a id="req-XXX"></a>anchor + prose + Appendix Crow).
docs/src/content/docs/specs/manifests/openapm-v0.1.requirements.ymlupdated.
@pytest.mark.req("req-XXX")test undertests/spec_conformance/added or extended.CONFORMANCE.{md,json}regenerated viauv run --extra dev python -m tests.spec_conformance.gen_statementand committed.