Skip to content

perf(install): fetch cold github.com deps as one verified archive - #4774

Closed
Daniel Meppiel (danielmeppiel) wants to merge 1 commit into
mainfrom
danielmeppiel-perf-cold-transport
Closed

Daniel Meppiel (danielmeppiel) wants to merge 1 commit into
mainfrom
danielmeppiel-perf-cold-transport

Conversation

@danielmeppiel

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

Copy link
Copy Markdown
Collaborator

Description

A fresh apm install (empty cache) is bound by round trips and git subprocesses, not by the network. On the tree-10 perf fixture (offline mirror, 30 ms RTT), main makes 40 requests / 80 round trips / 20 TLS connects and runs ~14 git subprocesses per dependency. Per dependency that is: 1 commits-API GET on its own TLS connection (L1 ref resolution), then a git clone (info/refs + 2 upload-pack POSTs on a second connection), then a local checkout. The resolver is level-parallel, so about 9 round trips sit on the critical path per dependency level.

This PR replaces the cold clone with one verified archive:

  1. Fetch. A cold, whole-repository github.com dependency is downloaded as one codeload.github.com/<owner>/<repo>/tar.gz/<sha> response. The git/trees/<sha> API call runs in parallel, so it adds no round trip. Both requests go through origin_bounded_request with zero redirect hops and ambient_credentials=False: no credential, netrc entry or redirect is ever followed.
  2. Accept only the commit's exact git tree. The pax comment must name the commit, AND the git tree hash recomputed from the members (path, exec bit, blob bytes) must equal the commit's tree SHA. The following raise ArchiveUnavailable: links, special files, .gitattributes, submodules (missing gitlinks change the tree hash), export-ignored paths, non-canonical or non-portable paths (one check on the raw name of every member: empty/./.. segments, backslash, NUL, : NTFS streams, trailing dot/space, DOS device names such as CON/nul.txt/COM1, and .git/git~1 in any case; the same rules on every OS), the decompression bounds (ArchiveLimits: 64 MiB compressed, 256 MiB uncompressed counting directory members, 10000 members), any HTTP status other than 200, and any network error. When that happens nothing lands and the git transport runs exactly as before. An unreachable codeload disables archives for the rest of the run. Landed file modes come from the verified tree, never from the archive.
  3. Same store, same hit path. The verified tree lands in the GitCache full-tree shard checkouts_v1/<shard>/<sha>/full through a new GitCache-owned miss-side method, land_tree_checkout. It writes a minimal .git: detached HEAD, every materialization pin, apm.materialization=verified-tree and apm.tree=<sha>. Warm and --frozen hits treat it like a cloned shard. The link validator's owner (utils/git_sparse.validate_materialized_symlinks) returns early only for these link-free shards, so a warm restore reaches no network (lifecycle-tested). download_package gains a two-line hook: _cached = try_archive_tree(...) or self._persistent_cache_checkout(...); the existing copy, validate and fallback path is unchanged. There is no second cache. An existing git shard for the SHA is always preferred.

Routing (gate in deps/archive_checkout.py, owner git-transport-selection). The archive is used only when all of these hold:

  • the host is exact github.com over HTTPS (no port, not --insecure, not virtual/subdirectory, not Artifactory/ADO);
  • AuthResolver.uses_public_github_anonymous_first is true, so auth-first keeps git;
  • the downloader's canonical TransportSelector anonymous plan starts with plain HTTPS to the unrewritten URL. Any insteadOf rewrite (e.g. https->ssh) or APM_GIT_PROTOCOL=ssh keeps git; this was a security finding, with a red-first test;
  • Git reports no system/global attributes file with rules (git var GIT_ATTR_SYSTEM / GIT_ATTR_GLOBAL in the git transport's own env), because those could make a checkout differ from the blobs;
  • Git config has no includeIf anywhere (unscoped git config --includes --get-regexp ^includeif\. from a neutral directory, in both the user's and the transport's environment). This follows nested include.path and env-provided config, because a conditional include can activate a rewrite or an attributes file only inside the clone;
  • Git's protocol policy allows HTTPS (GIT_ALLOW_PROTOCOL includes https; protocol.allow / protocol.https.allow unset or always), in both environments;
  • registry-only mode routes source archives DIRECT;
  • APM_ARCHIVE_TRANSPORT is not 0.

Tar members are read and extracted only through the archive owner (utils/archive.py: new read_tar_gz_members, safe_extract_tar_gz_bytes).

Integrity. Accepted bytes are proven to be the commit's git tree. The git path, pinned by core.autocrlf=false, core.eol=lf and no attributes, writes the same blob bytes, so content_hash and the deployed file hashes are byte-identical whichever transport ran. The lifecycle scenario covers archive, then cold git, then a tampered archive.

Floor (tree-10, 5 levels). Level k's dependencies are known only after level k-1 is read, so the floor is (5 levels + 2 connect) RT = 7 x 30 ms = 210 ms. This PR keeps L1 ahead of the archive round (~2 RT per level). Follow-ups, out of scope per #4776: fold L1 into the archive round (1 RT per level), the keep-alive session for L1 (branch danielmeppiel-perf-keepalive-session), authenticated/private archives, GHE, and the synchronous update check (3 RT before the command).

Evidence (GAINS STANDARD)

Journey: apm install, empty cache (install-cold), offline mirror, RTT 30 ms. ABAB from exact-SHA worktrees on the PERF host slot. base c969aba99a6f (main) -> new d3370f8f628b. Cell: wall s / CPU s (user+sys incl. children) [1-min load].

fixture run base c969aba new d3370f8
tree-10 A/B 1 _ _
tree-10 A/B 2 _ _
tree-50 A/B 1 _ _
fixture metric base new
tree-10 HTTPS requests / connects / round trips _ _
tree-10 git processes (GIT_TRACE) _ _
tree-10 lock content hashes identical - _
tree-50 HTTPS requests / connects / round trips _ _
tree-50 git processes (GIT_TRACE) _ _

Gain: _ x CPU, _ x wall; _ % of the cold journey removed. North Star: tree-10 cold now _ s wall / _ s CPU, gap to 1 s _ s.

Issue and approved scope

Fixes #4776
Fixes #4819

Human scope-approval comment: scope record posted on #4776 (regent-ruled, accepted).

Does this PR complete the issue, or what remains? It completes #4776's scope. Follow-ups are listed above as out of scope.

Type of change

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

Testing

  • Tested locally
  • All existing tests pass (inherited reds only, see below)
  • Added tests for new functionality (if applicable)

New and changed tests:

  • tests/integration/test_archive_transport.py (component). A verified archive lands an ordinary cache hit whose compute_package_hash equals a git clone's (real repo with nested dirs, exec bit, CRLF blob and git's a.b < a/ order). Every rejection below asserts that nothing lands in the cache:
    • red-first QL cells: test_archive_digest_mismatch_rejected_before_deployment, tar traversal / absolute path / dot segment, symlink and hardlink members, decompression bounds (bytes, members, compressed size) and directory members counting toward the bound;
    • the exec bit comes from the tree;
    • wrong commit, .gitattributes, unreachable codeload;
    • routing on a real downloader/AuthResolver/TransportSelector: the positive control and a private-repo 404 fall back to git; an insteadOf https->ssh rewrite (real GIT_CONFIG_GLOBAL), ambient attributes, GHE Cloud, GHES, ADO, SSH, custom port, insecure, virtual, auth-first and APM_GIT_PROTOCOL=ssh send no archive request; a codeload 3xx is refused.
  • tests/lifecycle/scenarios/test_transport_parity.py (real CLI, policy on, I1-I9 per step) runs four installs. All four end with the identical lock row (resolved_commit, content_hash, deployed_file_hashes), and the tampered bytes are never deployed or cached:
    • an archive install with a PAT and a ~/.netrc for both hosts, where every codeload and git/trees request carries no Authorization header and no secret;
    • a networkless warm restore;
    • a cold git install (APM_ARCHIVE_TRANSPORT=0);
    • a cold install with a tampered codeload response.
  • tests/lifecycle/world/test_world.py: the git-transport cell is pinned to APM_ARCHIVE_TRANSPORT=0 and keeps its git assertion unchanged. A new sibling test_cold_github_source_installs_through_the_verified_archive asserts the exact egress (tar.gz/<sha> + git/trees/<sha>) and lock rows identical to git.
  • Egress table: github "clone" rows count the codeload archive as package content, and the slice names codeload.github.com as its archive host. So the existing secret-absence evidence now also covers codeload. codeload.github.com was added to the world host sets of test_m_source and test_explicit_host_identity.

Inherited reds (not from this PR):

Lint mirror (local, at head): ruff 0.15.12 check/format, pylint R0801, scripts/lint-auth-signals.sh, the complexity ratchet, scripts/lint_architecture_boundaries.py, tests.quality.taxonomy --changed and scripts/check_test_assertions.py are all clean. The architecture changes: new modules registered under the git-transport-selection owner, a new consumer listed on archive-extraction-limits, and the _checkout_materialization_pins() count guard set to 4 (the verified-tree shard writer).

Changelog

  • One fragment per section, changelog.d/<pr>.<section>.md (changelog.d/4774.changed.md).
  • N/A -- internal-only change (CI, tests, refactor).

Spec conformance (OpenAPM v0.1)

  • N/A -- this PR does not change OpenAPM-observable behaviour (lockfile bytes and content hashes are identical; transport only). Commit carries apm-spec-waiver.

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

Unresolved archive byte-parity, permission, and size-limit issues remain, alongside cache and transport-selection regressions and broken or nonportable tests.

8 open issues
What changed in this PR

This PR adds an archive-based fast path for cold, anonymous GitHub dependency installs, integrating verified repository contents into the existing Git cache while preserving Git fallback.

Changes:

  • Fetch and verify archives against commit and tree identities before cache landing.
  • Reuse a credential-scoped HTTP session across dependency requests.
  • Add transport-parity and rejection tests, architecture registration, and opt-out documentation.
File Description
tests/​unit/​deps/​test_archive_transport.py Tests archive verification, rejection, cache landing, and eligibility.
tests/​lifecycle/​world/​services.py Adds simulated codeload and Git tree endpoints.
tests/​lifecycle/​scenarios/​test_transport_parity.py Compares archive, Git, and tampered-archive installs.
src/​apm_cli/​utils/​archive.py Adds bounded in-memory tar reading and extraction.
src/​apm_cli/​deps/​github_downloader.py Integrates archive fetching and shared cached-package handling.
src/​apm_cli/​deps/​download_strategies.py Introduces pooled, credential-scoped HTTP requests.
src/​apm_cli/​deps/​archive_transport.py Implements archive fetching, tree verification, and staging.
src/​apm_cli/​deps/​archive_checkout.py Gates archive eligibility and handles fallback.
src/​apm_cli/​cache/​git_cache.py Adds verified-tree cache landing and metadata.
scripts/​architecture_linter/​checks/​transport_sparse_and_updates.py Updates the materialization-pin usage guard.
docs/​src/​content/​docs/​reference/​environment-variables.md Documents APM_ARCHIVE_TRANSPORT.
changelog.d/​4774.changed.md Records the cold-install transport change.
.apm/​architecture/​owners/​transport-auth-platform.json Registers archive transport ownership and extraction consumers.

Review effort: Balanced

Comment thread src/apm_cli/deps/archive_transport.py Resolved
Comment thread src/apm_cli/deps/download_strategies.py Resolved
Comment thread src/apm_cli/cache/git_cache.py Resolved
Comment thread src/apm_cli/deps/archive_checkout.py Resolved
Comment thread src/apm_cli/deps/archive_transport.py Resolved
Comment thread src/apm_cli/utils/archive.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.

Comment thread src/apm_cli/utils/archive.py Resolved
Comment thread src/apm_cli/deps/archive_checkout.py Resolved
Comment thread src/apm_cli/deps/archive_checkout.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

Tar extension metadata bypasses decompression limits and can exhaust memory, while explicit HTTPS dependencies can bypass configured Git URL rewrites.

5 open issues

Review effort: Balanced

Comment thread src/apm_cli/utils/archive.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.

🔵 Needs a closer look

PAX/GNU metadata bypasses archive decompression limits, allowing memory exhaustion before verification, while explicit HTTPS dependencies can also bypass Git rewrite routing.

5 open issues

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

Critical archive-handling gaps let PAX/GNU metadata bypass decompression limits during verification and extraction, risking memory exhaustion, and allow unverified directories to alter package detection.

5 open issues
1 resolved since last review

Review effort: Balanced

Comment thread src/apm_cli/deps/archive_transport.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

Archive directory names bypass canonical-path validation, allowing non-canonical members to be extracted instead of triggering the required Git fallback.

1 open issue
2 resolved since last review

Review effort: Balanced

Comment thread src/apm_cli/deps/archive_transport.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

Relative Git attribute paths can bypass required content transformations and break hash parity, while streamed connection failures leave the failing archive transport enabled for subsequent fetches.

2 open issues
1 resolved since last review

Review effort: Balanced

Comment thread src/apm_cli/deps/archive_checkout.py Resolved
Comment thread src/apm_cli/deps/archive_transport.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.

🔵 Needs a closer look

The default-on transport and cache-trust changes require maintainer security/runtime review and exact-SHA tree-10/tree-50 ABAB GAINS validation, as the benchmark tables remain blank.

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

Directory-name normalization hides repeated trailing separators, allowing malformed archives to pass canonical-path validation instead of triggering the required Git fallback.

1 open issue

Review effort: Balanced

Comment thread src/apm_cli/utils/archive.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 archive path can bypass Git’s explicit protocol allow-list by sending an HTTPS codeload request when HTTPS is prohibited, requiring an allow-list check and a routing regression test.

1 open issue
1 resolved since last review

Review effort: Balanced

Comment thread src/apm_cli/deps/archive_checkout.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

Unbounded inherited global PAX metadata can still cause gigabyte-scale memory exhaustion despite the archive byte and member caps.

1 open issue
1 resolved since last review

Review effort: Balanced

Comment thread src/apm_cli/utils/archive.py Resolved
A cold whole-repository dependency on github.com is fetched as one
codeload tar.gz (by commit SHA), with the GitHub git/trees API call in
parallel, instead of a git clone (about 14 git subprocesses and 3 git
HTTP requests per dependency).

The archive is accepted only when the git tree hash recomputed from its
members (path, exec bit, blob bytes) equals the commit's tree and its pax
comment names the commit. Links, special files, .gitattributes,
submodules, non-canonical paths, the size cap and any HTTP failure fall
back to git, and nothing lands. The verified tree lands in the GitCache
full-tree shard through GitCache.land_tree_checkout (minimal .git:
detached HEAD + materialization pins), so warm and frozen hits are
unchanged and the lockfile content hash is the same whichever transport
ran.

Only the anonymous first attempt the git transport itself makes for
github.com is used; auth-first, other hosts, SSH, registry-only mode and
APM_ARCHIVE_TRANSPORT=0 keep git.
The archive is bounded (64 MiB compressed, 256 MiB uncompressed, 10000
members); hitting a bound falls back to git.

apm-spec-waiver: transport-only change; lockfile bytes and content hashes are identical whichever transport fetched the package

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@danielmeppiel

Copy link
Copy Markdown
Collaborator Author

Closed by [PERF] VP v2: lane D KILLED on evidence, per the regent's kill condition (cold bench). CI bench at head-equivalent 094577e vs base 0a31fd7, 3 runners x 3 interleaved pairs, paired CPU:

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 PAX size-override bypass allows archives to evade the guard, exposing both readers to metadata memory amplification before tree verification.

1 open issue
1 resolved since last review

Review effort: Balanced

extended += size
if extended > _MAX_EXTENDED_HEADER_BYTES:
_raise(error_type, "Archive extended metadata exceeds its limit")
offset += block + -(-size // block) * block
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants