Repository navigation
perf(install): resolve below a live-resolved root instead of re-walking it - #4802
Conversation
…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]>
There was a problem hiding this comment.
🟡 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
…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]>
There was a problem hiding this comment.
🟡 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
…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]>
- 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]>
…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]>
Co-authored-by: Copilot App <[email protected]>
…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]>
There was a problem hiding this comment.
🔵 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
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]>
Drop #4802 from the install perf line: CI bench shows no speed gain, so it is internal. Co-authored-by: Copilot App <[email protected]>
revert: #4802 path CheckRoot (zero measured gain on the CI bench)
…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]>
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]>


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/realpathis still 19% of the tree-10 no-op CPU and 29% of tree-50 (plain-CPU ITIMER_PROF sampling at maince977b27).realpathwalks every component from/. For a candidate under the project root, that re-walks the root prefix the same check just resolved, about 12lstatcalls 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 exactlyresolve_for_containment(path), but walks only the components ofpathbelow 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 thenrealpath(root/suffix) == realpath(root)/suffixon every Python version.CheckRoot.within(candidate, base)resolvesbasethrough the same check and appliesresolved_relative_within.DeploymentMaterializationSession._locator/_write_locator,DeploymentLedgerCodec.locator_for_pathanddeployed_path_entrybuild oneCheckRootper check and pass it along. Thecheckparameter replaces #4761'scandidate_resolved.Security invariants (H8):
lstated at check time. This is the same sequential observationrealpathmakes, in the same order.realpathpath. That covers links planted after the root was resolved (pinned bytest_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)..., a missing component, a path outside the root, and a relative path (test_matches_full_resolution_for_every_shape).deployed_path_entrycalls 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
Testing
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
lstatcalls. 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
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.