feat: Show cooldown report for pinned version installs (#225)

* feat: Show cooldown report for pinned version installs

When a user installs a package with an explicit version (e.g.
npm install foo@1.2.3) and that version falls within the dependency
cooldown window, the cooldown block is now recorded and shown in the
report. Previously, the report only appeared when ALL versions of a
package were in cooldown (remaining == 0), causing pinned version
installs to fail with a confusing "version not found" error from the
package manager instead of a clear cooldown explanation.

Introduces InterceptorContext to carry per-execution data (pinned
versions) from the CLI command through the interceptor layer, keeping
it separate from long-lived dependencies like the analyzer and cache.

* fix: Normalize PyPI pinned version keys for cooldown lookup

CLI-provided package names (e.g. Flask_Cors) don't match the
URL-parsed form (flask-cors). Normalize keys once at construction
time so cooldown lookups match correctly.

* fix: Handle dots in PyPI package name normalization per PEP 503

denormalizePyPIPackageName already documented [-_.] replacement but
only handled underscores. Now also replaces dots with hyphens so
names like zope.interface match the URL-parsed form zope-interface.

* refactor: Extract shared cooldown stats recording into helper

Deduplicate identical stats-recording blocks from npm_cooldown.go
and pypi_cooldown.go into recordCooldownStats in cooldown.go.

* fix: Distinguish explicit version pins from auto-resolved versions

PyPI parsers resolve all packages to concrete versions (even without
a user-specified constraint), so HasVersion() was always true. Add
IsExplicitVersion to PackageInstallTarget, set it only when the user
provided an explicit constraint. Use it in proxy_flow.go to avoid
false pinned-version cooldown reports.
This commit is contained in:
Sahil Bansal
2026-04-23 17:47:14 +05:30
committed by GitHub
parent 996d9aeca0
commit c3a3518c36
17 changed files with 277 additions and 46 deletions
+11 -1
View File
@@ -50,7 +50,16 @@ func NewPypiRegistryInterceptor(
cache AnalysisCache,
statsCollector *AnalysisStatsCollector,
confirmationChan chan *ConfirmationRequest,
execContext InterceptorContext,
) *PypiRegistryInterceptor {
// Re-key pinned versions to the normalized form (lowercase, underscores→hyphens)
// so lookups by URL-parsed package name match correctly.
normalizedPinned := make(map[string]string, len(execContext.PinnedVersions))
for name, version := range execContext.PinnedVersions {
normalizedPinned[denormalizePyPIPackageName(name)] = version
}
execContext.PinnedVersions = normalizedPinned
return &PypiRegistryInterceptor{
baseRegistryInterceptor: baseRegistryInterceptor{
analyzer: analyzer,
@@ -58,6 +67,7 @@ func NewPypiRegistryInterceptor(
statsCollector: statsCollector,
confirmationChan: confirmationChan,
circuitBreaker: newAnalyzerCircuitBreaker("malysis-analyzer-pypi"),
execContext: execContext,
},
cooldownHandler: newPypiCooldownHandler(statsCollector),
}
@@ -116,7 +126,7 @@ func (i *PypiRegistryInterceptor) HandleRequest(ctx *proxy.RequestContext) (*pro
// for version resolution. JSON API requests (/pypi/{pkg}/json) are allowed through;
// they have a different response structure and pip does not use them for installs.
if depCooldownConfig.Enabled && strings.HasPrefix(ctx.URL.Path, "/simple/") {
return i.cooldownHandler.HandleMetadataRequest(ctx, pkgInfo.GetName(), depCooldownConfig.Days)
return i.cooldownHandler.HandleMetadataRequest(ctx, pkgInfo.GetName(), depCooldownConfig.Days, i.execContext.PinnedVersions[pkgInfo.GetName()])
}
log.Debugf("[%s] Skipping analysis for metadata request: %s", ctx.RequestID, pkgInfo.GetName())