Skip to content

Make registry metadata cache decisions one explicit state machine #146

Closed

Description

@zkochan

Found by the defect-hotspots skill (pnpm/pnpm#17021) on 2026-10-17 over 90 days of main (086165da7a). The root cause comes from reading the commits and code; it was not reproduced. The design steps are proposals for a maintainer to settle.

Loop: Registry metadata cache (about 10 fixes, one partly reversed)

Property. Whether to serve memory, serve the mirror, revalidate, or fetch depends only on the mirror's recorded headers, the network mode, the document kind, and the release-age need. On an unchanged registry, the second install downloads no body.

Members. pnpm/pnpm#14931 → pnpm/pnpm#15145 → pnpm/pnpm#16507 (the second fix caused a 200 loop the third fixed). pnpm/pnpm#15722, partly reversed by pnpm/pnpm#16533 after it caused 2x memory and 3x time. Also pnpm/pnpm#15879, pnpm/pnpm#15700, pnpm/pnpm#15860 → pnpm/pnpm#16496 (a perf regression), pnpm/pnpm#16647, pnpm/pnpm#16668.

Flaw.

  • No explicit state machine. The decision is spread across pick_package, cached_pick, handle_cache_hit, mirror_fast_paths (four shortcuts), offline_disk_pick, and disk_fallback.
  • Request shape is decided again in metadata_request. uncacheable is read in five places.
  • Three writers of MetaHeaders per stack, with no rule for which validator describes which document kind.
  • v12 has two request pipelines, so fix(resolving-npm-resolver): exclude permit queueing from the slow-request warning pnpm#16647 had to fix both.

Design change.

  • Pure decide(...) -> ServeMemory | ServeMirror | Revalidate | Fetch | FailOffline and on_response(...) -> MirrorWrite. MirrorWrite becomes the only header writer.
  • The release-age upgrade becomes a document kind, not a separate fetch.
  • v11 gets the same two functions over a shared case table.

Tests.

  • Table-driven unit tests of decide and on_response over mirror state × status × Cache-Control × ETag × network mode × document kind × release age.
  • A three-install sequence against a mock registry that keys ETags per representation. Assert the exact request log, with only 304s on installs 2 and 3.
  • A v11/v12 request-log differential.

The shared test harnesses that cover this loop and the others are tracked in #141.


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

Activity

  1. zkochan commented on Oct 17, 2026

    @zkochan
    MemberAuthor

    pnpm/pnpm#17024 is open (draft) as a first, partial step, in v12 only. It adds a pure metadata_cache_policy module:

    • DocumentKind (Abbreviated / Full / FullInAbbreviatedMirror) gives the request shape. A mirror's validators are sent only for the kind that recorded them.
    • MirrorWrite::on_response reads a 2xx once and is the only writer of a fetched document's mirror headers. The release-age upgrade is now a FullInAbbreviatedMirror request.
    • mirror_may_answer is the single uncacheable gate for picks.

    There is no behavior change, and table-driven unit tests cover each function.

    Still open on this task:


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

  2. zkochan commented on Oct 17, 2026

    @zkochan
    MemberAuthor

    Progress, all v12 and all merged:

    Still open:


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

  3. zkochan commented on Oct 17, 2026

    @zkochan
    MemberAuthor

    More progress, all merged:

    The v12 decisions are now pure functions with table tests. Each one answers a single question:

    • metadata_cache_policy: which document to request and with which validators, and which headers to write after the response.
    • route: which mirror reads to try before fetching, whether to read the disk first, whether a failed fetch may fall back, and whether to reuse an in-memory hit.

    Still open:


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

  4. zkochan commented on Oct 17, 2026

    @zkochan
    MemberAuthor

    pnpm/pnpm#17061 (merged) adds the three-install request-log harness for v12. TestRegistry::start_recording_with_own_storage records each request the fixture registry answers. cli/tests/suite/metadata_request_log.rs asserts each install's exact log. Only install 1 downloads a body. Installs 2 and 3 revalidate with the ETag of the same representation, or send no request when the release-age shortcut or --prefer-offline answers.

    Still open:

    • The v11 counterpart of the decision functions and a shared case table.
    • The v11/v12 request-log differential. These Rust tests can only run a released pnpm, so it needs a TypeScript-side harness over the same fixture registry.

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

  5. zkochan commented on Oct 17, 2026

    @zkochan
    MemberAuthor

    pnpm/pnpm#17063 (merged) adds the v11/v12 request-log differential. pnpr/.fixtures/request-logs/metadata-three-installs.json is the shared case table. The v12 CLI suite and a new v11 e2e test (pnpm11/pnpm/test/install/metadataRequestLog.ts) both assert it.

    It found and fixed two v12 bugs:

    • v12 dropped If-Modified-Since for a bare modified date, which v11 sends.
    • A modified before 1970 crashed the revalidation. This has shipped since v12.0.0.

    Still open: the v11 counterpart of the decision functions. v11 has no known defect left in this area, so this is cleanup only. The shared table now guards both stacks against drift, so I suggest closing this task unless that refactor is wanted.


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

  6. zkochan commented on Oct 17, 2026

    @zkochan
    MemberAuthor

    The v11 counterpart is merged:

    Every item in this task has now landed. Two follow-ups are left:

    • v12 still sends the release-age upgrade's conditional request after an identical revalidation. v11 now sends it once.
    • v11 differs from v12 in four places that #17064 left as they are: the stable-range shortcuts under minimumReleaseAge exclusions, the stable-range shortcuts after a missed prefer-offline read, the unvalidated-mirror shortcut on private routes, and the fallback after a failed fetch. The last one matters most. On a private route v12 fails closed on 401/403/private 404, while v11 falls back to the mirror.

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

  7. zkochan commented on Oct 17, 2026

    @zkochan
    MemberAuthor

    Done. Every item of this task has landed in both stacks. The last pieces were pnpm/pnpm#17064, pnpm/pnpm#17066, and pnpm/pnpm#17068.

    Correction to my previous comment. Two of the four v11/v12 differences I listed are not CLI differences: the private-route unvalidated-mirror shortcut and the fail-closed fallback. v12's private metadata scope comes only from pnpr's route hook, so the pnpm CLI behaves as v11 does there. No follow-up is needed for them.

    Follow-ups:

    • v12: release-age upgrade repeats the revalidation's conditional request #150: v12's release-age upgrade repeats the revalidation's conditional request.
    • The two remaining real differences are both cases where v11 is more conservative: the stable-range shortcuts under minimumReleaseAge exclusions, and after a missed prefer-offline read. v11 is maintenance-only, so I left them alone.

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

  8. zkochan commented on Oct 17, 2026

    @zkochan
    MemberAuthor

    Follow-ups landed:

    v11 and v12 now make the same metadata cache decisions.


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

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions