Repository navigation
perf(policy): read the project git remote once per command run - #4778
Daniel Meppiel (danielmeppiel) wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The memo import eagerly loads the policy stack, including requests and PyYAML, adding startup overhead even for commands that do not need policy.
1 open issue
What changed in this PR
This PR reduces repeated Git remote subprocess calls during org policy discovery by memoizing results within each CLI invocation. It supports the install performance work in #4770 while keeping reads live outside a command scope.
Changes:
- Adds a context-local memo with automatic cleanup after each command.
- Caches remote-read results, including failures, by project root and arguments.
- Adds tests for reuse, scope cleanup, failure caching and CLI integration.
| File | Description |
|---|---|
| tests/unit/policy/test_remote_memo.py | Tests memo behavior and CLI invocation scope. |
| src/apm_cli/policy/remote_memo.py | Defines the command-scoped memo context manager. |
| src/apm_cli/policy/_remote.py | Reuses memoized Git results when a scope is active. |
| src/apm_cli/cli.py | Opens and closes the memo with the Click context. |
Review effort: Balanced
Org policy discovery runs several times in one install (preflight, policy gate, target check, MCP) and each shelled out to git remote: 8 subprocesses on a tree-10 no-op. policy/_remote.py::_git now answers repeats within one CLI invocation from core/invocation_memo (#4786), failures included; outside a command run every read is live. apm-spec-waiver: memoizes an identical subprocess read within one command run; no OpenAPM-observable behaviour changes Co-authored-by: Copilot App <[email protected]>
c2e223f to
7cba455
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Path normalization can resolve Git operations to the wrong repository and organization policy, and the shared memo ownership contract lacks its required guard and mutation regression.
5 open issues
1 resolved since last review
Review effort: Balanced
…s root The per-invocation key is the absolute path without collapsing '..' (which abspath would do before symlinks resolve), and git keeps the caller's own cwd. Lifecycle scripts already drop every invocation memo after they run (core/lifecycle_scripts.py, #4786), so a pre-install script that switches origin is seen by the later policy gate. Tests: component marker; the next-run freshness read happens inside a new scope; a '..' path through a symlink is a distinct key. Co-authored-by: Copilot App <[email protected]>
b583ba9 to
d7924c1
Compare
There was a problem hiding this comment.
🟡 Changes recommended
The memoization key still distinguishes equivalent relative and absolute project roots, causing duplicate Git reads within one scope and requiring path normalization plus a regression test.
1 open issue
5 resolved since last review
Review effort: Balanced
They exercise the per-invocation memo with subprocess mocked and touch no filesystem, so they are pure-logic unit tests; the '..' key case no longer needs a real symlink. Fixes AQ003 on the merge commit with current main. Co-authored-by: Copilot App <[email protected]>
The memo keyed both the absolute root and the caller's Path, so Path('app')
and Path.cwd()/'app' ran git twice in one command run. The caller's root is
still git's cwd but no longer part of the key.
Co-authored-by: Copilot App <[email protected]>
- git_sparse: a full shard materialized with no unresolvable link records a links-clean marker bound to its commit; a cache-hit validation of that commit trusts it instead of running `git ls-tree` per dependency (O(N)). - GitCache.pinned_checkout: a verified hit for a lockfile-pinned SHA is returned before the auth fallback builds a git env, so a hit runs no git. - policy/_remote: one `git remote` read per command (memo over core/invocation_memo; ported from #4778). tree-10 counts (main f0dab6c -> this): no-op 8 -> 2 spawns, frozen 29 -> 2, warm 23 -> 3; tree-50 per-dependency ls-tree 50 -> 0. apm-spec-waiver: cache-hit and policy remote-read process counts only; no OpenAPM-observable behaviour change Co-authored-by: L5 PERF engineer 6ffa7618 (policy remote memo, #4778) Co-authored-by: Copilot App <[email protected]>
…l memo The new component tests assert the recorded marker and the untouched unverified shard (AQ003). policy/_remote.py and its test now match #4778 at fe1bf3b (equivalent relative and absolute roots share one read). Co-authored-by: L5 PERF engineer 6ffa7618 (policy remote memo, #4778) Co-authored-by: Copilot App <[email protected]>



Description
Org policy discovery runs several times in one
apm install(install preflight, policy gate, target check, MCP preflight). Each discovery shelled out togit remote get-url origin(plusgit remotewhen there is no origin), so a tree-10 no-op ran 8git remotesubprocesses (policy/_remote.py:47, about 1.4 s profiled under load).policy/_remote.py::_git, the single owner of these reads, now answers repeats of the same(absolute project root, argv)within one CLI invocation from the shared per-invocation memocore/invocation_memo.py(#4786, one scope opened bycli.py). Stacked on #4786: merge it first. Strictness is unchanged: every command run still reads the remote live once, failures are memoized as failures, and outside a command run (library callers, tests) nothing is cached. The remote cannot change within one run except by a concurrent edit, which the previous code also raced.This buys the no-op fast path (#4775) its 1 s budget and speeds up every install path.
Issue and approved scope
Issue: Refs #4770
Human scope-approval comment: #4770 (comment) (VP ruling: ship the per-run memo as a small separate PR first).
Does this PR complete the issue, or what remains? No; #4775 completes #4770.
Type of change
Testing
tests/unit/policy/test_remote_memo.py: reads stay live outside a command run; one command run reads each remote once and the next run reads it live again; a failed read is not retried within the run; (the per-invocation scope itself is covered in #4786).Changelog
Spec conformance (OpenAPM v0.1)