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]

### Fixed

- The `UpgradeRoots` pass no longer keeps an answer that moves a root from a final release to a pre-release, older or newer. Before, it could trade `tensorflow` 2.13.1 for 2.13.0rc2 to raise `numpy` a major, and count that as a gain. A root whose first answer was already a pre-release can still move to a newer pre-release or a final. Nothing changes with the pass off (#106).

## [0.26.0] - 2026-11-18

### Added
Expand Down
7 changes: 7 additions & 0 deletions resolver/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,10 @@ func RecordSteps() (got func() []int, restore func()) {
onSteps = func(n int) { all = append(all, n) }
return func() []int { return all }, func() { onSteps = nil }
}

// OnCandidate calls f with each answer an upgrade re-solve found, and whether
// the pass kept it, until restore is called. Not safe with parallel tests.
func OnCandidate(f func(got *Resolution, kept bool)) (restore func()) {
onCandidate = f
return func() { onCandidate = nil }
}
5 changes: 3 additions & 2 deletions resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,9 @@ type Options struct {
// and bisects, which assumes a level that fails means every higher one does
// too; an upgrade that breaks that is missed. Ties are broken by name. An
// answer is kept only if the sum of levels behind, then the sum of versions
// behind, over all roots strictly drops. A failed re-solve still spends
// budget. Every re-solve uses these same Options.
// behind, over all roots strictly drops. An answer that moves any root from
// a final release to a pre-release, older or newer, is never kept. A failed
// re-solve still spends budget. Every re-solve uses these same Options.
//
// It never fails a resolution: if the budget or ctx ends, or no re-solve
// helps, the best complete answer so far is returned.
Expand Down
57 changes: 44 additions & 13 deletions resolver/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ type upRoot struct {
u *usability

// reqs are the root's own requirements, and pre is whether its pre-releases
// count. admit remembers per index of cands whether they admit it (1) or
// not (2). It is filled on demand: the specifier test is costly, and only
// versions above a pin are ever asked about.
// count: only if the base answer pinned one. admit remembers per index of
// cands whether they admit it (1) or not (2). It is filled on demand: the
// specifier test is costly, and only versions above a pin are ever asked
// about.
reqs []requirement.Requirement
pre bool
admit []int8
Expand All @@ -82,6 +83,10 @@ type upRoot struct {
// onSpecTest, when set by a test, is called before each root specifier test.
var onSpecTest func()

// onCandidate, when set by a test, is called with each answer a re-solve
// found and whether the pass kept it.
var onCandidate func(got *Resolution, kept bool)

// admitted reports whether the root's own specifiers admit cands[i].
func (r *upRoot) admitted(i int) bool {
if r.admit[i] == 0 {
Expand Down Expand Up @@ -274,7 +279,7 @@ func upgradeRoots(
// best is scored only once a re-solve has an answer to compare it with:
// scoring tests every version above each root's pin. After a ctx cancel or
// an index error mid-pass, scoring may differ; the answer stays valid.
var bestLv, bestVer int
var bestScore score
scored := false
budget := opts.UpgradeRoots
inner := opts
Expand Down Expand Up @@ -317,11 +322,14 @@ func upgradeRoots(
return nil
}
if !scored {
bestLv, bestVer = objective(roots, best)
bestScore = objective(roots, best)
scored = true
}
lv, ver := objective(roots, got)
if lv > bestLv || (lv == bestLv && ver >= bestVer) {
kept := objective(roots, got).less(bestScore)
if onCandidate != nil {
onCandidate(got, kept)
}
if !kept {
return nil
}
return got
Expand Down Expand Up @@ -361,7 +369,7 @@ func upgradeRoots(
}
accept := func(got *Resolution) int {
best = got
bestLv, bestVer = objective(roots, best)
bestScore = objective(roots, best)
return levelIdx(best.Pinned[pick.name])
}

Expand Down Expand Up @@ -456,16 +464,39 @@ func admitsAll(reqs []requirement.Requirement, v version.Version) bool {
return true
}

// objective is the sum over roots of levels behind, then of versions behind.
func objective(roots []*upRoot, res *Resolution) (levels, versions int) {
// score is how far an answer's roots are from where the pass wants them.
// Lower is better; the fields compare in order.
type score struct {
pre int // roots moved from a final release to a pre-release
levels int // sum of levels behind
versions int // sum of versions behind
}

func (a score) less(b score) bool {
if a.pre != b.pre {
return a.pre < b.pre
}
if a.levels != b.levels {
return a.levels < b.levels
}
return a.versions < b.versions
}

// objective scores res over roots. A root the base answer pinned at a final
// release counts in pre when res pins a pre-release of it, older or newer.
func objective(roots []*upRoot, res *Resolution) score {
var s score
for _, r := range roots {
pin, ok := res.Pinned[r.name]
if !ok {
continue
}
if !r.pre && pin.IsPreRelease() {
s.pre++
}
l, v := r.behind(pin)
levels += l
versions += v
s.levels += l
s.versions += v
}
return levels, versions
return s
}
147 changes: 147 additions & 0 deletions resolver/upgrade_downgrade_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

package resolver_test

import (
"context"
"reflect"
"testing"

"github.com/posit-dev/go-pyresolver/index"
"github.com/posit-dev/go-pyresolver/resolver"
)

// passCandidate is one answer an upgrade re-solve found.
type passCandidate struct {
pins map[string]string
kept bool
}

// resolveCandidates resolves with the pass on and returns the final pins and
// every candidate the pass scored.
func resolveCandidates(t *testing.T, idx index.MetadataIndex, reqs ...string) (map[string]string, []passCandidate) {
t.Helper()
var seen []passCandidate
defer resolver.OnCandidate(func(got *resolver.Resolution, kept bool) {
seen = append(seen, passCandidate{pins(t, got), kept})
})()
opts := testOptions(t)
opts.UpgradeRoots = 12
res, err := resolver.Resolve(context.Background(), mustRequirements(t, reqs...), idx, opts)
if err != nil {
t.Fatal(err)
}
return pins(t, res), seen
}

func TestUpgradeRootsRootMovingDown(t *testing.T) {
cases := []struct {
name string
idx *index.MockIndex
reqs []string
base map[string]string // the answer with the pass off
cand map[string]string // a candidate the pass must score
kept bool // whether the pass keeps cand
}{
{
// #106: n can rise a major only if a leaves its final for an
// older pre-release.
name: "final to an older pre-release is not kept",
idx: index.NewMockIndex("test").
AddVersion("a", "1.0rc1").
AddVersion("a", "1.0", "n<2").AddVersion("a", "1.1", "n<2").
AddVersion("n", "1.0").AddVersion("n", "1.1").
AddVersion("n", "1.2").AddVersion("n", "2.0"),
reqs: []string{"a==1.*", "n"},
base: map[string]string{"a": "1.1", "n": "1.2"},
cand: map[string]string{"a": "1.0rc1", "n": "2.0"},
kept: false,
},
{
// The pre-release is newer than the final, and two majors up.
name: "final to a newer pre-release is not kept",
idx: index.NewMockIndex("test").
AddVersion("a", "1.0", "n<2").AddVersion("a", "3.0rc1").
AddVersion("n", "1.0").AddVersion("n", "2.0"),
reqs: []string{"a", "n"},
base: map[string]string{"a": "1.0", "n": "1.0"},
cand: map[string]string{"a": "3.0rc1", "n": "2.0"},
kept: false,
},
{
// A root may still give up a final for an older final when the
// roots gain more levels than they lose.
name: "final to an older final is kept on a net gain",
idx: index.NewMockIndex("test").
AddVersion("a", "1.0").
AddVersion("a", "1.1", "n<2").
AddVersion("n", "1.0").AddVersion("n", "1.1").AddVersion("n", "2.0"),
reqs: []string{"a", "n"},
base: map[string]string{"a": "1.1", "n": "1.1"},
cand: map[string]string{"a": "1.0", "n": "2.0"},
kept: true,
},
{
name: "pre-release to a newer pre-release is kept",
idx: index.NewMockIndex("test").
AddVersion("p", "1.0rc1").
AddVersion("p", "2.0rc1", "q<2").AddVersion("p", "3.0rc1", "q<2").
AddVersion("q", "1.0").AddVersion("q", "2.0"),
reqs: []string{"p", "q"},
base: map[string]string{"p": "1.0rc1", "q": "2.0"},
cand: map[string]string{"p": "3.0rc1", "q": "1.0"},
kept: true,
},
{
name: "pre-release to a newer final is kept",
idx: index.NewMockIndex("test").
AddVersion("p", "1.0rc1").
AddVersion("p", "2.0", "q<2").AddVersion("p", "3.0", "q<2").
AddVersion("q", "1.0").AddVersion("q", "2.0"),
reqs: []string{"p", "q"},
base: map[string]string{"p": "1.0rc1", "q": "2.0"},
cand: map[string]string{"p": "3.0", "q": "1.0"},
kept: true,
},
{
// c is not a root, so its drop costs nothing.
name: "a package that is not a root may move down",
idx: index.NewMockIndex("test").
AddVersion("a", "1.0").
AddVersion("a", "2.0", "c<2").AddVersion("a", "3.0", "c<2").
AddVersion("b", "1.0", "c").
AddVersion("c", "1.0").AddVersion("c", "2.0"),
reqs: []string{"a", "b"},
base: map[string]string{"a": "1.0", "b": "1.0", "c": "2.0"},
cand: map[string]string{"a": "3.0", "b": "1.0", "c": "1.0"},
kept: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := resolveUp(t, 0, tc.idx, tc.reqs...); !reflect.DeepEqual(got, tc.base) {
t.Fatalf("pass off: %v, want %v", got, tc.base)
}
got, seen := resolveCandidates(t, tc.idx, tc.reqs...)
found := false
for _, c := range seen {
if reflect.DeepEqual(c.pins, tc.cand) {
found = true
if c.kept != tc.kept {
t.Errorf("candidate %v: kept = %v, want %v", c.pins, c.kept, tc.kept)
}
}
}
if !found {
t.Fatalf("the pass never scored %v; it scored %v", tc.cand, seen)
}
want := tc.base
if tc.kept {
want = tc.cand
}
if !reflect.DeepEqual(got, want) {
t.Errorf("pass on: %v, want %v", got, want)
}
})
}
}
Loading