Skip to content

Split Resolve into ResolveFirst and FirstSolve.UpgradePass - #112

Merged
jonyoder merged 2 commits into
mainfrom
two-phase-upgrade
Nov 21, 2026
Merged

jonyoder merged 2 commits into
mainfrom
two-phase-upgrade

Conversation

@jonyoder

Copy link
Copy Markdown
Collaborator

Adds ResolveFirst, which runs only the first solve, and FirstSolve.UpgradePass, which runs the UpgradeRoots pass later from that answer without solving it again. Package Manager solves about 30 environments per upload and wants every first answer to exist before any pass starts, so a deadline during the passes can fall back to the first answers: https://github.com/rstudio/package-manager/issues/22820.


API

// resolver/firstsolve.go
type FirstSolve struct{ /* unexported */ }

func ResolveFirst(ctx context.Context, reqs []requirement.Requirement, idx index.MetadataIndex, opts Options) (*FirstSolve, error)
func (f *FirstSolve) Resolution() *Resolution            // the first answer, a new copy each call
func (f *FirstSolve) UpgradePass(ctx context.Context) *Resolution
  • FirstSolve keeps reqs, index, Options, the first answer, and the first solve's duration and steps. The pass reads those, so its step and time bounds count from the first solve as in Resolve, and a caller cannot hand it a different answer or inputs.
  • Resolve now runs resolveFirst then the same pass, so the two paths are one code path.
  • UpgradeRoots == 0: UpgradePass returns the first answer with Upgrade nil. A ctx already ended: the first answer, StoppedByTime true, no error.
  • ResolveFirst copies reqs and AllowPrerelease; Resolution and UpgradePass return fresh copies. A FirstSolve is safe to use from several goroutines.
  • Additive only. No existing behavior changes.

Tests

resolver/firstsolve_test.go:

  • TestTwoPhaseMatchesResolve: 14 fixtures (early/late gain, time factor and max time, step factor short/exact, max steps, budget 1, three root-moving-down cases) on a fake clock. Two phases and Resolve are reflect.DeepEqual, report included, with the same steps per solve.
  • TestTwoPhaseStepBoundIsRelativeToFirstSolve, TestTwoPhaseEndedCtxGivesFirst, TestTwoPhasePassOff, TestResolveFirstRunsNoPass, TestTwoPhaseResultsShareNothing, and TestTwoPhaseAcrossGoroutines (first solves and passes on different goroutines, two passes per value).

Red: against a stub that solves but passes no duration or steps to the pass, 4 tests fail: five equivalence cases (time factor 1, 1.5, early 1; step factor short and exact) (steps per solve: two phases [7], Resolve [7 7 8]), the step-bound test (using 0 steps, want ... using 15), the copy test, and the goroutine test. A nil stub panics.

Green: go test ./... passes 1302 tests (7 skipped). go test -race ./resolver/ passes. go vet, gofmt -l and golangci-lint v2.11.2 are clean.

🤖 Generated with Claude Code

jonyoder and others added 2 commits November 21, 2026 07:23
A caller can run every first solve before any upgrade pass. Resolve now
uses the same path, so the two give the same answer and report.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Also list the full supported surface and say what ResolveFirst keeps by
reference.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jonyoder

Copy link
Copy Markdown
Collaborator Author

Merging at 1409dedd43fab43289015899428a59807511ed94 under Jon's standing approval for go-pyresolver. Jon chose this approach on rstudio/package-manager#22820; he has not reviewed this code himself.

It adds a public API, so it had two independent reviews (separate Claude sessions that did not write it):

  • Round 1, at ff434dd: approve with nits. One medium: no test caught time spent between ResolveFirst and UpgradePass leaking into the time bound. Two low: the AllowPrerelease copy was untested, and the supported-surface comment was stale. Plus one doc line on which options are kept by reference. All fixed in 1409ded.
  • Round 2, at this commit: approve with nits. It confirmed each fix by breaking it in a scratch copy, and ran the equivalence tests against the pre-PR Resolve body to show the two-phase path gives the same results.

RoboRev jobs 5734 and 5739 passed with no findings. CI: build (with race) and lint green. Tests at this commit: 1309 passing (the PR description still shows the first round's 1302).

Left as is, all low: one long doc line on UpgradeTimeFactor; "do not change RankMemo" means its settings, since solves fill it by design; the race test skips time-bound cases because they need a real clock.

@jonyoder
jonyoder merged commit 22a318f into main Nov 21, 2026
2 checks passed
@jonyoder
jonyoder deleted the two-phase-upgrade branch November 21, 2026 12:53
@jonyoder jonyoder mentioned this pull request Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant