Skip to content

perf(policy): read the project git remote once per command run - #4778

Closed
Daniel Meppiel (danielmeppiel) wants to merge 4 commits into
mainfrom
danielmeppiel-perf-git-remote-memo
Closed

Daniel Meppiel (danielmeppiel) wants to merge 4 commits into
mainfrom
danielmeppiel-perf-git-remote-memo

Conversation

@danielmeppiel

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

Copy link
Copy Markdown
Collaborator

Description

Org policy discovery runs several times in one apm install (install preflight, policy gate, target check, MCP preflight). Each discovery shelled out to git remote get-url origin (plus git remote when there is no origin), so a tree-10 no-op ran 8 git remote subprocesses (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 memo core/invocation_memo.py (#4786, one scope opened by cli.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

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

Testing

  • Tested locally (host slots saturated; CI validates; ABAB evidence follows in a comment)
  • All existing tests pass
  • Added tests for new functionality (if applicable)

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

  • One fragment per section
  • N/A -- internal-only change (CI, tests, refactor).

Spec conformance (OpenAPM v0.1)

  • N/A -- this PR does not change OpenAPM-observable behaviour.

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 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

Comment thread src/apm_cli/cli.py Resolved

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 remote cache can miss an organization’s policy when a pre-install script changes origin, and the root import adds unnecessary policy-stack loading to unrelated commands.

2 open issues

Review effort: Balanced

Comment thread src/apm_cli/policy/_remote.py Resolved

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 remote memo can retain a stale URL or failure after pre-install hooks change origin, causing the subsequent policy gate to evaluate the wrong organization’s policy.

4 open issues

Review effort: Balanced

Comment thread tests/unit/policy/test_remote_memo.py Resolved
Comment thread tests/unit/policy/test_remote_memo.py Resolved
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]>

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

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

Comment thread src/apm_cli/policy/_remote.py Resolved
…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]>

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 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

Comment thread src/apm_cli/policy/_remote.py Resolved
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]>

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 memoization key still includes the original project_root, causing equivalent relative and absolute paths to run duplicate Git subprocesses instead of sharing a cached result.

1 open issue

Review effort: Balanced

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]>

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.

🟢 Approval recommended

The Git remote memoization change has no active review issues.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 22, 2026
- 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]>
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 22, 2026
…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]>
@danielmeppiel

Copy link
Copy Markdown
Collaborator Author

Superseded by #4836, which folds this change (policy/_remote.py at fe1bf3b and tests/unit/policy/test_remote_memo.py, credited to the original author) into the lane B warm-install floor PR, per the PERF VP plan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants