Skip to content

refactor(npm-resolver): move metadata request shapes and mirror header writes into one policy - #17066

Merged
zkochan merged 3 commits into
mainfrom
fix-146-v11-metadata-policy
Oct 17, 2026
Merged

zkochan merged 3 commits into
mainfrom
fix-146-v11-metadata-policy

Conversation

@zkochan

@zkochan zkochan commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

Summary

Part of pnpm/tasks#146, v11 only. This is the v11 counterpart of #17024 and #17028. It is independent of #17064, which handles the route decisions.

src/metadataCachePolicy.ts holds what pnpm v12's metadata_cache_policy decides:

Decision Replaces
documentKindOf: abbreviated, full, fullInAbbreviatedMirror fullEtagOfAbbreviatedMirror
conditionalRequest the request built in both pipelines, fetchConditionally in pickFromRegistry.ts and fetchMetadataCached in fetchFullMetadataCached.ts
unconditionalRefetch the refetch after an unusable 304, in both pipelines
releaseAgeUpgradeRequest revalidationOfHeldFullDocument and the held-tag logic in upgradeFreshMetaForReleaseAge
mirrorWrite, the only writer of a fetched document's header line mirrorEtags and holdsFullMetaInAbbreviatedMirror at all four save sites: two in pickFromRegistry.ts, one in fetchFullMetadataCached.ts, and persistUpgradedMeta
mirrorMayAnswer, the uncacheable gate for picks the body of canServeCachedMeta

One behavior change. Take a mirror that holds the full document, from a registry without complete publish times. Its release-age upgrade sends the same conditional request as its revalidation. memoizeFetchMetadata keys requests by JSON.stringify(opts). The old code built the two requests with their properties in a different order, so they got different keys and v11 sent the same request twice. Both requests are now built by the policy, so the upgrade reuses the revalidation's 304. I updated the two publishedBy tests that counted the second 304, and added a changeset. v12 still sends both requests (upgraded_mirror_revalidation.rs expects four 304s over installs two and three). That is a possible v12 follow-up.

Tests. test/metadataCachePolicy.test.ts has table tests for each decision, including that a written mirror revalidates the document it recorded. To test the tests, I made mirrorWrite record every ETag as etag and conditionalRequest ignore fullEtag. Seven existing publishedBy tests failed, apart from the new table. The full npm-resolver suite passes: 563 tests.

A v11 bug this turned up. I fixed it separately in #17068. A full metadata pick ran the release-age upgrade. With a registry whose full documents have no time, that downloaded the document again after every 304, and the full mirror lost its etag. This PR keeps the current write in persistUpgradedMeta (mirrorWrite(upgradedFrom, false)). After #17068 that write only ever reaches the abbreviated mirror.

Squash Commit Body

`metadataCachePolicy.ts` holds what pnpm v12's `metadata_cache_policy`
decides: the document kind a mirror revalidates as, the conditional
request and the unconditional refetch both request pipelines send, the
release-age upgrade's request, the header line a fetched document is
mirrored with (`mirrorWrite`, now the only writer for fetched
documents), and the uncacheable gate (`mirrorMayAnswer`). `mirrorEtags`,
`holdsFullMetaInAbbreviatedMirror`, `fullEtagOfAbbreviatedMirror`, and
`revalidationOfHeldFullDocument` are gone.

The release-age upgrade of a mirror that holds the full document now
builds the same request as that mirror's revalidation, so
`memoizeFetchMetadata` serves it the revalidation's 304. Before, the two
requests differed only in property order, which gave them different
memo keys and sent the same conditional request twice.

Part of pnpm/tasks#146.

Checklist

  • I checked the referenced issue and verified that none of the PRs
    already linked to it solves it.
  • New features are implemented only in the Rust pnpm v12 CLI. Bug fixes
    are implemented in every affected version.
  • Added a changeset (pnpm changeset) if this PR changes any published
    package.
  • Added or updated tests.

Written by an agent (Claude Code, claude-opus-5-5).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • When minimum release age is enabled and cached package metadata is missing some publish times, pnpm now reuses a metadata revalidation request instead of making a second request for the same information.
    • This reduces redundant registry requests while preserving release-age checks and the use of cached metadata where appropriate.

zkochan and others added 2 commits October 17, 2026 22:18
…r writes into one policy

`metadataCachePolicy.ts` holds what pnpm v12's `metadata_cache_policy`
decides: the document kind a mirror revalidates as, the conditional
request and the unconditional refetch both request pipelines send, the
release-age upgrade's request, the header line a fetched document is
mirrored with (`mirrorWrite`, now the only writer for fetched
documents), and the uncacheable gate (`mirrorMayAnswer`). `mirrorEtags`,
`holdsFullMetaInAbbreviatedMirror`, `fullEtagOfAbbreviatedMirror`, and
`revalidationOfHeldFullDocument` are gone.

The release-age upgrade of a mirror that holds the full document now
builds the same request as that mirror's revalidation, so
`memoizeFetchMetadata` serves it the revalidation's 304. Before, the two
requests differed only in property order, which gave them different
memo keys and sent the same conditional request twice.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: pnpm/pnpm/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2d704e92-83e6-4464-9ac9-e3948145aa7a
📥 Commits

Reviewing files that changed from the base of the PR and between e7cfcbb and 9b20615.

📒 Files selected for processing: 9
  • .changeset/release-age-upgrade-reuses-revalidation.md
  • pnpm11/resolving/npm-resolver/src/fetchFullMetadataCached.ts
  • pnpm11/resolving/npm-resolver/src/metaMirror.ts
  • pnpm11/resolving/npm-resolver/src/metadataCachePolicy.ts
  • pnpm11/resolving/npm-resolver/src/pickFromRegistry.ts
  • pnpm11/resolving/npm-resolver/src/pickPackage.ts
  • pnpm11/resolving/npm-resolver/src/releaseAgeUpgrade.ts
  • pnpm11/resolving/npm-resolver/test/metadataCachePolicy.test.ts
  • pnpm11/resolving/npm-resolver/test/publishedBy.test.ts
💤 Files with no reviewable changes: 1
  • pnpm11/resolving/npm-resolver/src/metaMirror.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 3 remain after this review.

📝 Walkthrough

The npm resolver now centralizes metadata request and mirror-write decisions in metadataCachePolicy. Release-age upgrades reuse conditional metadata revalidation, and tests expect fewer registry requests for later installs.

Changes: NPM metadata revalidation

Layer / File(s) Summary
Metadata cache policy
pnpm11/resolving/npm-resolver/src/metadataCachePolicy.ts, pnpm11/resolving/npm-resolver/src/metaMirror.ts, pnpm11/resolving/npm-resolver/test/metadataCachePolicy.test.ts
Added helpers for document classification, conditional and unconditional requests, release-age upgrade requests, mirror writes, and mirror eligibility. Removed the previous mirror ETag helpers. Added tests for the policy helpers.
Cached metadata requests and mirror writes
pnpm11/resolving/npm-resolver/src/fetchFullMetadataCached.ts, pnpm11/resolving/npm-resolver/src/pickFromRegistry.ts, pnpm11/resolving/npm-resolver/src/pickPackage.ts
Resolver paths now use policy helpers to select request options, write mirror metadata, and decide whether cached metadata can be served.
Release-age upgrade revalidation
pnpm11/resolving/npm-resolver/src/releaseAgeUpgrade.ts, pnpm11/resolving/npm-resolver/src/pickFromRegistry.ts, pnpm11/resolving/npm-resolver/test/publishedBy.test.ts, .changeset/release-age-upgrade-reuses-revalidation.md
Release-age upgrade requests and mirror persistence use the policy helpers. Tests expect the regular metadata revalidation response to be reused by the upgrade. The changeset records patch releases for @pnpm/resolving.npm-resolver and pnpm.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant Resolver
  participant MetadataCachePolicy
  participant Registry
  participant Mirror
  Resolver->>MetadataCachePolicy: Select conditional request options
  MetadataCachePolicy-->>Resolver: Return request options
  Resolver->>Registry: Request metadata
  Registry-->>Resolver: Return metadata response
  Resolver->>MetadataCachePolicy: Select mirror write fields
  MetadataCachePolicy-->>Resolver: Return ETag and body
  Resolver->>Mirror: Persist metadata
Loading

Suggested reviewers: altaywtf

Merge Risk: ⚪ Minimal · up to 9b206

The metadata revalidation change is ready to merge after normal checks; no unresolved behavior issue was established.

✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@zkochan
zkochan marked this pull request as ready for review October 17, 2026 21:00
@zkochan

zkochan commented Oct 17, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

Copilot AI balanced review requested due to automatic review settings October 17, 2026 21:00

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@greptile-apps greptile-apps Bot 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.

Your trial has ended. Reactivate Greptile to resume code reviews.

@qodo-code-review

Copy link
Copy Markdown

PR Summary by Qodo

Centralize v11 npm metadata cache requests and mirror headers

✨ Enhancement 🧪 Tests 📝 Documentation 🕐 40+ Minutes

Grey Divider

AI Description

• Centralizes metadata request shapes, mirror header writes, and uncacheable-mirror decisions in one
 policy.
• Reuses a full-document revalidation response during release-age upgrades, avoiding a duplicate
 conditional request.
• Adds policy tests, adjusts request-count expectations, and records the behavior change in a
 changeset.
Diagram

graph TD
  Pick["Package picks"] --> Policy["Cache policy"] --> Memo["Memoized fetch"] --> Registry["Npm registry"]
  Full["Full metadata fetch"] --> Policy
  Upgrade["Release-age upgrade"] --> Policy
  Policy --> Mirror[("Metadata mirror")]
  Mirror --> Policy
Loading
High-Level Assessment

A separate policy module fits the two existing fetch pipelines without merging their distinct responsibilities. Keeping request construction and mirror-header decisions together also makes the representation-specific ETag rule testable; duplicating those decisions in callers would retain the inconsistency this change removes.

Files changed (9) +282 / -116

Enhancement (1) +105 / -0
metadataCachePolicy.tsDefine metadata request and mirror-write policy +105/-0

Define metadata request and mirror-write policy

• Introduces document-kind classification, conditional and unconditional request shapes, release-age upgrade requests, mirror header writes, and uncacheable-mirror eligibility. A held full document uses its full-document ETag for revalidation.

pnpm11/resolving/npm-resolver/src/metadataCachePolicy.ts

Refactor (5) +37 / -112
fetchFullMetadataCached.tsUse shared policy in the cached full-metadata fetch +9/-13

Use shared policy in the cached full-metadata fetch

• Delegates conditional requests, validator-free refetches, and fetched-document mirror headers to the new policy. Retains condensation of full documents stored in abbreviated mirrors.

pnpm11/resolving/npm-resolver/src/fetchFullMetadataCached.ts

metaMirror.tsRemove displaced mirror representation helpers +0/-36

Remove displaced mirror representation helpers

• Removes the separate ETag-placement and document-kind helpers now superseded by metadataCachePolicy.

pnpm11/resolving/npm-resolver/src/metaMirror.ts

pickFromRegistry.tsApply shared policy throughout registry picks +18/-24

Apply shared policy throughout registry picks

• Uses policy decisions for revalidation, fallback refetches, release-age upgrades, and both fetched-document mirror writes. Upgrade requests now use the same shape as revalidation requests for a mirror holding full metadata.

pnpm11/resolving/npm-resolver/src/pickFromRegistry.ts

pickPackage.tsDelegate uncacheable-mirror eligibility +2/-7

Delegate uncacheable-mirror eligibility

• Routes the existing offline and prefer-offline exception for uncacheable cached metadata through the shared policy.

pnpm11/resolving/npm-resolver/src/pickPackage.ts

releaseAgeUpgrade.tsUse policy for release-age requests and persistence +8/-32

Use policy for release-age requests and persistence

• Replaces local held-document validator logic with the shared upgrade request. Persists upgraded responses using the policy's mirror header write.

pnpm11/resolving/npm-resolver/src/releaseAgeUpgrade.ts

Tests (2) +134 / -4
metadataCachePolicy.test.tsCover metadata cache policy decisions +129/-0

Cover metadata cache policy decisions

• Adds table tests for document kinds, request shapes, mirror header placement, and uncacheable-mirror eligibility. Checks that a written full-document mirror revalidates with the ETag it recorded.

pnpm11/resolving/npm-resolver/test/metadataCachePolicy.test.ts

publishedBy.test.tsExpect shared 304 responses during release-age upgrades +5/-4

Expect shared 304 responses during release-age upgrades

• Adjusts two integration tests to expect one conditional revalidation per later install rather than a second identical request for the upgrade.

pnpm11/resolving/npm-resolver/test/publishedBy.test.ts

Documentation (1) +6 / -0
release-age-upgrade-reuses-revalidation.mdRecord the reduced release-age request count +6/-0

Record the reduced release-age request count

• Adds patch changesets for pnpm and the npm resolver. Describes the conditional request avoided when cached metadata already holds the full document.

.changeset/release-age-upgrade-reuses-revalidation.md

@qodo-code-review

qodo-code-review Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (1) 🔗 Cross-repo conflicts (0) 📜 Skill insights (1)

Grey Divider


Remediation recommended

1. Release notes combine old and new behavior 📘 Rule violation ⚙ Maintainability
Description
The changeset describes the new single-request behavior with the trailing phrase instead of two,
rather than giving the previous two-request behavior its own past-tense sentence. This makes the
release note mix the fix and its historical comparison in one sentence, so readers cannot
distinguish the current behavior from what previously occurred.
Code

.changeset/release-age-upgrade-reuses-revalidation.md[6]

+With `minimumReleaseAge` set, pnpm sends one conditional metadata request instead of two for a package whose cached metadata already holds the full document, but without every version's publish time.
Relevance

●●● Strong

Recent accepted precedent requires separate past-tense behavior; identical changeset wording was
corrected.

PR-#17044
PR-#16407

ⓘ Recommendations generated based on similar findings in past PRs

Evidence
The changeset's only release-note sentence uses instead of two to describe the old behavior as a
tail clause on the new-behavior sentence, which is explicitly prohibited by the checklist rule.

Rule 3058498: Describe previous buggy behavior in a separate past-tense sentence
.changeset/release-age-upgrade-reuses-revalidation.md[6-6]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The changeset combines the corrected behavior with the previous behavior using `instead of two`, which violates the release-note convention requiring prior buggy behavior to be stated separately in the past tense.

## Fix Focus Areas
- .changeset/release-age-upgrade-reuses-revalidation.md[6-6]

## Recommended Fix
Rewrite the entry as separate sentences, for example: `Previously, pnpm sent two conditional metadata requests for this package. With minimumReleaseAge set, pnpm now sends one conditional metadata request when the cached metadata already holds the full document but lacks every version's publish time.`

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗


2. Performance claim lacks measurements 📜 Skill insight ➹ Performance
Description
The changeset claims that release-age upgrades send one conditional request instead of two,
presenting a request-count reduction as a performance improvement without any numeric
before-and-after measurement. The PR description also calls this a speedup, but neither the release
note nor the supplied change provides benchmark results or timing variance for the affected resolver
path.
Code

.changeset/release-age-upgrade-reuses-revalidation.md[6]

+With `minimumReleaseAge` set, pnpm sends one conditional metadata request instead of two for a package whose cached metadata already holds the full document, but without every version's publish time.
Relevance

●●● Strong

Recent accepted precedent required reproducible before-and-after measurements for an unmeasured
metadata performance claim.

PR-#17044

ⓘ Recommendations generated based on similar findings in past PRs

Evidence
The changed release note explicitly claims one request instead of two, and the PR description
characterizes the behavior as a speedup, but the supplied diff contains no numeric benchmark or
before-and-after measurement.

.changeset/release-age-upgrade-reuses-revalidation.md[6-6]
pnpm11/resolving/npm-resolver/src/metadataCachePolicy.ts[47-63]
Skill: review-code

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The changeset and PR description present reduced metadata requests as a speedup, but no numeric before-and-after performance evidence is supplied.

## Fix Focus Areas
- .changeset/release-age-upgrade-reuses-revalidation.md[6-6]
- pnpm11/resolving/npm-resolver/src/metadataCachePolicy.ts[47-63]

## Recommended Fix
Add benchmark results covering the release-age upgrade path, including before and after request counts or elapsed times across multiple runs with variance. If no measurable performance improvement is intended, revise the release note and PR description to state only the behavioral request de-duplication without claiming a speedup.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗


Grey Divider

Context sources
❌ Tickets: failed to load — Ticket collection could not establish complete evidence
✅ Compliance rules (platform): 55 rules
✅ Cross-repo context — repo relationships
Review mode: Auto: 🧠 Deep: Refactors multiple metadata paths with cache, request, mirror, and performance-sensitive behavior.

Grey Divider

Tip of the day
💡 Did you know, you can see how risky a PR is at a glance with blast radius labels

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@qodo-code-review

Copy link
Copy Markdown

PR Code Suggestions ✨

Warning

/improve is deprecated. Use /agentic_review instead (removal date not yet scheduled).

No code suggestions found for the PR.

@github-actions github-actions Bot added the reviewed: coderabbit CodeRabbit submitted an approving review label Oct 17, 2026
@zkochan
zkochan merged commit 6cab147 into main Oct 17, 2026
39 checks passed
@zkochan
zkochan deleted the fix-146-v11-metadata-policy branch October 17, 2026 21:18
@coderabbitai

coderabbitai Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor
⚠️ Action not completed

Pull request is closed.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@jojo0770

Copy link
Copy Markdown

@MT00000002284271

zkochan added a commit that referenced this pull request Oct 17, 2026
… the release-age upgrade (#17071)

An abbreviated mirror that holds the full document, recorded with
`fullEtag` by an earlier release-age upgrade, is revalidated as that
full document. When the document still lacks a complete `time` map, the
upgrade then sent the same conditional request again. The cached fetch
now reports the document kind it asked for, and the pick marks a
packument fetched as `FullInAbbreviatedMirror` as upgrade-checked, so
the upgrade sends nothing. Installs over such a mirror send one
conditional request per package instead of two, as pnpm v11 does since
#17066.

Closes pnpm/tasks#150.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

product: pnpm@11 reviewed: coderabbit CodeRabbit submitted an approving review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants