Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ served it.

## [Unreleased]

### Added

- `ResolveFirst` runs only the first solve, and its `FirstSolve.UpgradePass` runs the `UpgradeRoots` pass later from that answer without solving it again. A caller can finish every first solve before any pass starts, and gets exactly what `Resolve` returns, report and bounds included.

## [0.28.0] - 2026-11-20

### Added
Expand Down
5 changes: 3 additions & 2 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ Phase 3.
> **Status: implemented end to end.** `pypirsf/`, `index/`, `pep440set/`,
> `candidate/`, `provider/` and `resolver/` all carry code. `resolver.Resolve`
> is the entry point; keep its exported surface to `Resolve`, `Options`,
> `Resolution`, `ResolutionError`, `MissingExtra`, `YankedPin`, `UpgradeReport`
> and `Requester`.
> `Resolution`, `ResolutionError`, `MissingExtra`, `YankedPin`, `UpgradeReport`,
> `Requester`, and `ResolveFirst` with `FirstSolve` (methods `Resolution` and
> `UpgradePass`), which split `Resolve` into its first solve and its pass.
>
> ⚠️ `resolver` is **no longer the only package Package Manager imports**. PPM
> implements its own `MetadataIndex` (`src/pyindex`) and imports `index/` for the
Expand Down
80 changes: 80 additions & 0 deletions resolver/firstsolve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

package resolver

import (
"context"
"slices"
"time"

"github.com/posit-dev/go-pyresolver/index"
"github.com/posit-dev/go-python-packaging/requirement"
)

// FirstSolve holds a first answer so the UpgradeRoots pass can run later
// without solving it again. Make one with ResolveFirst. It is safe for concurrent use.
type FirstSolve struct {
reqs []requirement.Requirement
idx index.MetadataIndex
opts Options
res *Resolution

// dur and steps are what the first solve took. The pass's time and step
// bounds are relative to them.
dur time.Duration
steps int
}

// ResolveFirst runs the first solve of Resolve, with no UpgradeRoots pass. It
// fails as Resolve does. FirstSolve.UpgradePass runs the pass later.
// It keeps idx, RankMemo, WheelTags and Policy by reference: do not change them before UpgradePass.
func ResolveFirst(
ctx context.Context,
reqs []requirement.Requirement,
idx index.MetadataIndex,
opts Options,
) (*FirstSolve, error) {
opts.AllowPrerelease = slices.Clone(opts.AllowPrerelease)
return resolveFirst(ctx, slices.Clone(reqs), idx, opts)
}

// resolveFirst is ResolveFirst without the copies; it keeps reqs and opts.
func resolveFirst(
ctx context.Context,
reqs []requirement.Requirement,
idx index.MetadataIndex,
opts Options,
) (*FirstSolve, error) {
start := now()
steps := &stepMeter{}
res, err := resolveOnce(ctx, reqs, idx, opts, steps)
if err != nil {
return nil, err
}
return &FirstSolve{
reqs: reqs, idx: idx, opts: opts, res: res,
dur: now().Sub(start), steps: steps.used,
}, nil
}

// Resolution returns the first answer, the one Resolve returns with the pass
// off. Each call returns a new copy.
func (f *FirstSolve) Resolution() *Resolution {
return cloneResolution(f.res)
}

// UpgradePass runs the UpgradeRoots pass from the first answer and returns what
// Resolve returns for the same inputs. It never fails: with UpgradeRoots at zero
// or ctx already ended, it returns the first answer.
func (f *FirstSolve) UpgradePass(ctx context.Context) *Resolution {
return f.pass(ctx, cloneResolution(f.res))
}

// pass runs the pass from base, an answer equal to f.res that the result may
// hold. With the pass off it returns base.
func (f *FirstSolve) pass(ctx context.Context, base *Resolution) *Resolution {
if f.opts.UpgradeRoots <= 0 {
return base
}
return upgradeRoots(ctx, f.reqs, f.idx, f.opts, base, f.dur, f.steps)
}
Loading