Move number parsing, version ordering and constraints, version sorting and solver literals into src/kernel, with contracts checked by moon prove under overflow-checked integers. Replace the semver behaviour tests with those proofs, keep tests for string parsing, and run the proofs in CI. Version numbers are now 64-bit: the old parser silently wrapped at 2^31. Drop pick_tag, which only tests called. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1527 lines
37 KiB
MoonBit
1527 lines
37 KiB
MoonBit
// Lazy Resolvo-style CDCL SAT solver for package dependency resolution.
|
|
//
|
|
// Translates package version selection, dependency implications, channel pins,
|
|
// and binary-name ownership conflicts into Boolean clauses lazily as candidate
|
|
// manifests are demanded.
|
|
|
|
///|
|
|
struct SolveReq {
|
|
name : String
|
|
channel : String?
|
|
constraint : String
|
|
label : String
|
|
}
|
|
|
|
///|
|
|
struct CandidateInfo {
|
|
name : String
|
|
channel : String
|
|
tag : String
|
|
version : String
|
|
digest : String
|
|
favored : Bool
|
|
}
|
|
|
|
///|
|
|
struct CandidateDetail {
|
|
digest : String
|
|
manifest : Manifest
|
|
}
|
|
|
|
///|
|
|
struct SolvedPkg {
|
|
name : String
|
|
channel : String
|
|
tag : String
|
|
version : String
|
|
digest : String
|
|
manifest : Manifest
|
|
}
|
|
|
|
///|
|
|
struct SolverProvider {
|
|
candidates : async (String, String?) -> (String, Array[CandidateInfo])
|
|
details : async (CandidateInfo) -> CandidateDetail
|
|
extra_reqs : (String) -> Array[SolveReq]
|
|
locked_bins : Map[String, String]
|
|
}
|
|
|
|
///|
|
|
enum ClauseOrigin {
|
|
Root(String, String, String?, String)
|
|
Dep(Int32, String, String?, String)
|
|
AtMostOne(String)
|
|
BinConflict(String, Int32, Int32)
|
|
LockedBinConflict(String, Int32, String)
|
|
Learned(Array[Int32])
|
|
}
|
|
|
|
///|
|
|
struct Clause {
|
|
lits : Array[Int32]
|
|
origin : ClauseOrigin
|
|
}
|
|
|
|
///|
|
|
struct PkgState {
|
|
name : String
|
|
mut default_channel : String?
|
|
loaded_channels : Map[String, Array[Int32]]
|
|
all_vars : Array[Int32]
|
|
mut extra_reqs_added : Bool
|
|
}
|
|
|
|
///|
|
|
struct SatSolver {
|
|
provider : SolverProvider
|
|
vars : Array[CandidateInfo]
|
|
var_pkg : Array[Int32]
|
|
pkgs : Array[PkgState]
|
|
pkg_index : Map[String, Int32]
|
|
clauses : Array[Clause]
|
|
watches : Array[Array[Int32]]
|
|
assign : Array[@kernel.Val]
|
|
level : Array[Int32]
|
|
reason : Array[Int32]
|
|
trail : Array[Int32]
|
|
trail_lim : Array[Int32]
|
|
mut qhead : Int32
|
|
expanded : Array[Bool]
|
|
details : Array[CandidateDetail?]
|
|
bin_providers : Map[String, Array[Int32]]
|
|
pending_reqs : Array[SolveReq]
|
|
}
|
|
|
|
// ---------------------------------------------------------------- helpers
|
|
|
|
///|
|
|
fn swap_idx(a : Array[Int32], i : Int32, j : Int32) -> Unit {
|
|
if i != j {
|
|
let t = a[i]
|
|
a[i] = a[j]
|
|
a[j] = t
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// Place original indices `i0` and `i1` (with `i0 != i1`) at positions 0 and 1.
|
|
fn put_first_two(a : Array[Int32], i0 : Int32, i1 : Int32) -> Unit {
|
|
let pos1 = if i1 == 0 { i0 } else { i1 }
|
|
swap_idx(a, 0, i0)
|
|
swap_idx(a, 1, pos1)
|
|
}
|
|
|
|
///|
|
|
/// Build candidate list from OCI tags: optional favored build first, then semver
|
|
/// tags highest-first, then non-semver literal tags (excluding "latest").
|
|
fn candidates_from_tags(
|
|
name : String,
|
|
channel : String,
|
|
tags : Array[String],
|
|
favored : (String, String, String)?,
|
|
) -> Array[CandidateInfo] {
|
|
let out : Array[CandidateInfo] = []
|
|
let seen : Array[String] = []
|
|
match favored {
|
|
Some((fch, fver, fdig)) =>
|
|
if fch == channel && fver != "" {
|
|
out.push(CandidateInfo::{
|
|
name,
|
|
channel,
|
|
tag: fver,
|
|
version: fver,
|
|
digest: fdig,
|
|
favored: true,
|
|
})
|
|
seen.push(fver)
|
|
}
|
|
None => ()
|
|
}
|
|
for t in sorted_versions(tags) {
|
|
if !seen.contains(t) {
|
|
out.push(CandidateInfo::{
|
|
name,
|
|
channel,
|
|
tag: t,
|
|
version: t,
|
|
digest: "",
|
|
favored: false,
|
|
})
|
|
seen.push(t)
|
|
}
|
|
}
|
|
for t in tags {
|
|
if t != "latest" && parse_ver(t) is None && !seen.contains(t) {
|
|
out.push(CandidateInfo::{
|
|
name,
|
|
channel,
|
|
tag: t,
|
|
version: t,
|
|
digest: "",
|
|
favored: false,
|
|
})
|
|
seen.push(t)
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
///|
|
|
fn cand_matches(
|
|
cand : CandidateInfo,
|
|
constraint : String,
|
|
has_semver : Bool,
|
|
) -> Bool {
|
|
if parse_ver(cand.tag) is Some(_) {
|
|
satisfies_all(cand.tag, [constraint])
|
|
} else {
|
|
let t = trim(constraint)
|
|
t == cand.tag ||
|
|
t == "=" + cand.tag ||
|
|
t == "==" + cand.tag ||
|
|
(!has_semver && (t == "" || t == "*"))
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn matching_vars(
|
|
s : SatSolver,
|
|
var_ids : Array[Int32],
|
|
constraint : String,
|
|
) -> Array[Int32] {
|
|
let mut has_semver = false
|
|
for vid in var_ids {
|
|
if parse_ver(s.vars[vid].tag) is Some(_) {
|
|
has_semver = true
|
|
break
|
|
}
|
|
}
|
|
let out : Array[Int32] = []
|
|
for vid in var_ids {
|
|
if cand_matches(s.vars[vid], constraint, has_semver) {
|
|
out.push(vid)
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
///|
|
|
fn available_labels(s : SatSolver, var_ids : Array[Int32]) -> String {
|
|
let xs : Array[String] = []
|
|
for vid in var_ids {
|
|
let t = s.vars[vid].tag
|
|
if !xs.contains(t) {
|
|
xs.push(t)
|
|
}
|
|
}
|
|
if xs.length() == 0 {
|
|
"(none)"
|
|
} else {
|
|
join(xs, ", ")
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------- SAT core
|
|
|
|
///|
|
|
fn new_solver(provider : SolverProvider) -> SatSolver {
|
|
SatSolver::{
|
|
provider,
|
|
vars: [],
|
|
var_pkg: [],
|
|
pkgs: [],
|
|
pkg_index: Map([]),
|
|
clauses: [],
|
|
watches: [],
|
|
assign: [],
|
|
level: [],
|
|
reason: [],
|
|
trail: [],
|
|
trail_lim: [],
|
|
qhead: 0,
|
|
expanded: [],
|
|
details: [],
|
|
bin_providers: Map([]),
|
|
pending_reqs: [],
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn current_level(s : SatSolver) -> Int32 {
|
|
s.trail_lim.length()
|
|
}
|
|
|
|
///|
|
|
fn lit_val(s : SatSolver, l : Int32) -> @kernel.Val {
|
|
@kernel.lit_val(s.assign[@kernel.lit_var(l)], l)
|
|
}
|
|
|
|
///|
|
|
fn enqueue(s : SatSolver, l : Int32, reason_cid : Int32) -> Unit {
|
|
let v = @kernel.lit_var(l)
|
|
s.assign[v] = if @kernel.lit_is_neg(l) { False } else { True }
|
|
s.level[v] = current_level(s)
|
|
s.reason[v] = reason_cid
|
|
s.trail.push(l)
|
|
}
|
|
|
|
///|
|
|
fn backtrack(s : SatSolver, target_lvl : Int32) -> Unit {
|
|
if current_level(s) <= target_lvl {
|
|
return
|
|
}
|
|
let stop = s.trail_lim[target_lvl]
|
|
while s.trail.length() > stop {
|
|
let p = s.trail.pop().unwrap()
|
|
let v = @kernel.lit_var(p)
|
|
s.assign[v] = Unassigned
|
|
s.level[v] = -1
|
|
s.reason[v] = -1
|
|
}
|
|
while s.trail_lim.length() > target_lvl {
|
|
s.trail_lim.pop() |> ignore
|
|
}
|
|
if s.qhead > s.trail.length() {
|
|
s.qhead = s.trail.length()
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn attach_watches(s : SatSolver, cid : Int32) -> Unit {
|
|
let lits = s.clauses[cid].lits
|
|
if lits.length() >= 2 {
|
|
s.watches[@kernel.lit_neg(lits[0])].push(cid)
|
|
s.watches[@kernel.lit_neg(lits[1])].push(cid)
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn add_clause_dynamic(
|
|
s : SatSolver,
|
|
raw_lits : Array[Int32],
|
|
origin : ClauseOrigin,
|
|
) -> Int32 {
|
|
let lits : Array[Int32] = []
|
|
for l in raw_lits {
|
|
if lits.contains(@kernel.lit_neg(l)) {
|
|
return -1 // tautology
|
|
}
|
|
if !lits.contains(l) {
|
|
lits.push(l)
|
|
}
|
|
}
|
|
let n = lits.length()
|
|
if n == 0 {
|
|
backtrack(s, 0)
|
|
let cid = s.clauses.length()
|
|
s.clauses.push(Clause::{ lits, origin })
|
|
return cid
|
|
}
|
|
let mut true_idx : Int32 = -1
|
|
let unassigned : Array[Int32] = []
|
|
let mut max_false_lvl : Int32 = 0
|
|
let mut max_false_idx : Int32 = -1
|
|
let mut second_false_idx : Int32 = -1
|
|
let mut second_false_lvl : Int32 = -1
|
|
for i = 0; i < n; i = i + 1 {
|
|
match lit_val(s, lits[i]) {
|
|
True =>
|
|
if true_idx < 0 || s.level[@kernel.lit_var(lits[i])] < s.level[@kernel.lit_var(lits[true_idx])] {
|
|
true_idx = i
|
|
}
|
|
Unassigned => unassigned.push(i)
|
|
False => {
|
|
let lvl = s.level[@kernel.lit_var(lits[i])]
|
|
if max_false_idx < 0 || lvl > max_false_lvl {
|
|
second_false_idx = max_false_idx
|
|
second_false_lvl = max_false_lvl
|
|
max_false_lvl = lvl
|
|
max_false_idx = i
|
|
} else if second_false_idx < 0 || lvl > second_false_lvl {
|
|
second_false_idx = i
|
|
second_false_lvl = lvl
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if true_idx >= 0 {
|
|
// Satisfied under current assignment. If it is the sole non-false lit and
|
|
// was decided above max_false_lvl, avoid leaving an unpropagated watch.
|
|
if n >= 2 {
|
|
let mut second : Int32 = -1
|
|
for i = 0; i < n; i = i + 1 {
|
|
if i != true_idx && !(lit_val(s, lits[i]) is False) {
|
|
second = i
|
|
break
|
|
}
|
|
}
|
|
if second < 0 {
|
|
second = max_false_idx
|
|
}
|
|
put_first_two(lits, true_idx, second)
|
|
}
|
|
let cid = s.clauses.length()
|
|
s.clauses.push(Clause::{ lits, origin })
|
|
attach_watches(s, cid)
|
|
return -1
|
|
}
|
|
if unassigned.length() >= 2 {
|
|
put_first_two(lits, unassigned[0], unassigned[1])
|
|
let cid = s.clauses.length()
|
|
s.clauses.push(Clause::{ lits, origin })
|
|
attach_watches(s, cid)
|
|
return -1
|
|
}
|
|
if unassigned.length() == 1 {
|
|
let u = unassigned[0]
|
|
if n >= 2 {
|
|
put_first_two(lits, u, max_false_idx)
|
|
}
|
|
let cid = s.clauses.length()
|
|
s.clauses.push(Clause::{ lits, origin })
|
|
attach_watches(s, cid)
|
|
if max_false_lvl < current_level(s) {
|
|
backtrack(s, max_false_lvl)
|
|
}
|
|
if lit_val(s, lits[0]) is Unassigned {
|
|
enqueue(s, lits[0], cid)
|
|
}
|
|
return -1
|
|
}
|
|
// Zero unassigned and zero true -> conflict.
|
|
if n >= 2 {
|
|
put_first_two(lits, max_false_idx, second_false_idx)
|
|
}
|
|
let cid = s.clauses.length()
|
|
s.clauses.push(Clause::{ lits, origin })
|
|
attach_watches(s, cid)
|
|
if max_false_lvl < current_level(s) {
|
|
backtrack(s, max_false_lvl)
|
|
}
|
|
cid
|
|
}
|
|
|
|
///|
|
|
fn alloc_candidates(
|
|
s : SatSolver,
|
|
pid : Int32,
|
|
cands : Array[CandidateInfo],
|
|
) -> Array[Int32] {
|
|
let pkg = s.pkgs[pid]
|
|
let ids : Array[Int32] = []
|
|
for c in cands {
|
|
let vid = s.vars.length()
|
|
s.vars.push(c)
|
|
s.var_pkg.push(pid)
|
|
s.watches.push([])
|
|
s.watches.push([])
|
|
s.assign.push(Unassigned)
|
|
s.level.push(-1)
|
|
s.reason.push(-1)
|
|
s.expanded.push(false)
|
|
s.details.push(None)
|
|
pkg.all_vars.push(vid)
|
|
ids.push(vid)
|
|
}
|
|
ids
|
|
}
|
|
|
|
///|
|
|
async fn provider_candidates(
|
|
p : SolverProvider,
|
|
name : String,
|
|
pin : String?,
|
|
) -> (String, Array[CandidateInfo]) raise OrbitError {
|
|
try (p.candidates)(name, pin) catch {
|
|
OrbitError(msg) => raise OrbitError(msg)
|
|
e => raise OrbitError(e.to_string())
|
|
}
|
|
}
|
|
|
|
///|
|
|
async fn provider_details(
|
|
p : SolverProvider,
|
|
cand : CandidateInfo,
|
|
) -> CandidateDetail raise OrbitError {
|
|
try (p.details)(cand) catch {
|
|
OrbitError(msg) => raise OrbitError(msg)
|
|
e => raise OrbitError(e.to_string())
|
|
}
|
|
}
|
|
|
|
///|
|
|
async fn ensure_pkg_channel(
|
|
s : SatSolver,
|
|
name : String,
|
|
pin : String?,
|
|
) -> (String, Array[Int32]) raise OrbitError {
|
|
match s.pkg_index.get(name) {
|
|
None => {
|
|
let (ch_name, cands) = provider_candidates(s.provider, name, pin)
|
|
let pid = s.pkgs.length()
|
|
let pkg = PkgState::{
|
|
name,
|
|
default_channel: if pin is Some(_) {
|
|
None
|
|
} else {
|
|
Some(ch_name)
|
|
},
|
|
loaded_channels: Map([]),
|
|
all_vars: [],
|
|
extra_reqs_added: false,
|
|
}
|
|
s.pkgs.push(pkg)
|
|
s.pkg_index[name] = pid
|
|
let ids = alloc_candidates(s, pid, cands)
|
|
pkg.loaded_channels[ch_name] = ids
|
|
if !pkg.extra_reqs_added {
|
|
pkg.extra_reqs_added = true
|
|
for er in (s.provider.extra_reqs)(name) {
|
|
s.pending_reqs.push(er)
|
|
}
|
|
}
|
|
(ch_name, ids)
|
|
}
|
|
Some(pid) => {
|
|
let pkg = s.pkgs[pid]
|
|
match pin {
|
|
Some(ch_name) =>
|
|
match pkg.loaded_channels.get(ch_name) {
|
|
Some(ids) => return (ch_name, ids)
|
|
None => ()
|
|
}
|
|
None =>
|
|
match pkg.default_channel {
|
|
Some(ch_name) =>
|
|
match pkg.loaded_channels.get(ch_name) {
|
|
Some(ids) => return (ch_name, ids)
|
|
None => ()
|
|
}
|
|
None => ()
|
|
}
|
|
}
|
|
let (ch_name, cands) = provider_candidates(s.provider, name, pin)
|
|
if pin is None {
|
|
pkg.default_channel = Some(ch_name)
|
|
}
|
|
match pkg.loaded_channels.get(ch_name) {
|
|
Some(ids) => (ch_name, ids)
|
|
None => {
|
|
let prev_len = pkg.all_vars.length()
|
|
let ids = alloc_candidates(s, pid, cands)
|
|
pkg.loaded_channels[ch_name] = ids
|
|
// If an earlier candidate of this package is already True, forbid
|
|
// selecting any candidate from the newly loaded channel.
|
|
for i = 0; i < prev_len; i = i + 1 {
|
|
let u = pkg.all_vars[i]
|
|
if s.assign[u] == True {
|
|
for w in ids {
|
|
ignore(
|
|
add_clause_dynamic(
|
|
s,
|
|
[@kernel.neg_lit(w), @kernel.neg_lit(u)],
|
|
AtMostOne(pkg.name),
|
|
),
|
|
)
|
|
}
|
|
}
|
|
}
|
|
(ch_name, ids)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// Propagate unit implications; returns conflicting clause ID or -1.
|
|
fn propagate(s : SatSolver) -> Int32 {
|
|
while s.qhead < s.trail.length() {
|
|
let p = s.trail[s.qhead]
|
|
s.qhead = s.qhead + 1
|
|
// Lazy At-Most-One when candidate `v` is assigned True.
|
|
if !@kernel.lit_is_neg(p) {
|
|
let v = @kernel.lit_var(p)
|
|
let pkg = s.pkgs[s.var_pkg[v]]
|
|
for other in pkg.all_vars {
|
|
if other != v && s.assign[other] != False {
|
|
let cid = s.clauses.length()
|
|
s.clauses.push(Clause::{
|
|
lits: [@kernel.neg_lit(other), @kernel.neg_lit(v)],
|
|
origin: AtMostOne(pkg.name),
|
|
})
|
|
attach_watches(s, cid)
|
|
if s.assign[other] == True {
|
|
return cid
|
|
}
|
|
enqueue(s, @kernel.neg_lit(other), cid)
|
|
}
|
|
}
|
|
}
|
|
// Two-watched-literals scan on watches[p].
|
|
// Watch lists store clauses keyed by `lit_neg(watched_lit)`, so when `p`
|
|
// becomes True, `lit_neg(p)` became False and watches[p] must be updated.
|
|
let ws = s.watches[p]
|
|
let false_lit = @kernel.lit_neg(p)
|
|
let mut i : Int32 = 0
|
|
let mut j : Int32 = 0
|
|
let n = ws.length()
|
|
let mut confl : Int32 = -1
|
|
while i < n {
|
|
let cid = ws[i]
|
|
i = i + 1
|
|
let lits = s.clauses[cid].lits
|
|
if lits[0] == false_lit {
|
|
lits[0] = lits[1]
|
|
lits[1] = false_lit
|
|
}
|
|
if lit_val(s, lits[0]) is True {
|
|
ws[j] = cid
|
|
j = j + 1
|
|
continue
|
|
}
|
|
let mut found = false
|
|
for k = 2; k < lits.length(); k = k + 1 {
|
|
if !(lit_val(s, lits[k]) is False) {
|
|
lits[1] = lits[k]
|
|
lits[k] = false_lit
|
|
s.watches[@kernel.lit_neg(lits[1])].push(cid)
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if found {
|
|
continue
|
|
}
|
|
// Keep watching false_lit on this clause.
|
|
ws[j] = cid
|
|
j = j + 1
|
|
if lit_val(s, lits[0]) is False {
|
|
confl = cid
|
|
while i < n {
|
|
ws[j] = ws[i]
|
|
j = j + 1
|
|
i = i + 1
|
|
}
|
|
break
|
|
} else {
|
|
enqueue(s, lits[0], cid)
|
|
}
|
|
}
|
|
while ws.length() > j {
|
|
ws.pop() |> ignore
|
|
}
|
|
if confl >= 0 {
|
|
return confl
|
|
}
|
|
}
|
|
-1
|
|
}
|
|
|
|
///|
|
|
/// 1-UIP conflict analysis when current_level > 0.
|
|
fn analyze_conflict(
|
|
s : SatSolver,
|
|
confl0 : Int32,
|
|
) -> (Array[Int32], Int32, Array[Int32]) {
|
|
let cur_lvl = current_level(s)
|
|
let seen : Array[Bool] = Array::make(s.vars.length(), false)
|
|
let learned : Array[Int32] = [0]
|
|
let ants : Array[Int32] = [confl0]
|
|
let mut counter : Int32 = 0
|
|
|
|
let process_clause = fn(cid : Int32, skip_var : Int32) {
|
|
for q in s.clauses[cid].lits {
|
|
let qv = @kernel.lit_var(q)
|
|
if qv != skip_var && !seen[qv] {
|
|
seen[qv] = true
|
|
if s.level[qv] == cur_lvl {
|
|
counter = counter + 1
|
|
} else if s.level[qv] > 0 {
|
|
learned.push(q)
|
|
} else if s.reason[qv] >= 0 {
|
|
ants.push(s.reason[qv])
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
process_clause(confl0, -1)
|
|
let mut idx = s.trail.length() - 1
|
|
let mut p : Int32 = -1
|
|
while idx >= 0 {
|
|
let t = s.trail[idx]
|
|
let v = @kernel.lit_var(t)
|
|
if seen[v] {
|
|
seen[v] = false
|
|
counter = counter - 1
|
|
if counter == 0 {
|
|
p = t
|
|
break
|
|
}
|
|
let r = s.reason[v]
|
|
if r >= 0 {
|
|
ants.push(r)
|
|
process_clause(r, v)
|
|
}
|
|
}
|
|
idx = idx - 1
|
|
}
|
|
learned[0] = @kernel.lit_neg(p)
|
|
let mut bt_level : Int32 = 0
|
|
if learned.length() > 1 {
|
|
let mut best : Int32 = 1
|
|
let mut best_lvl = s.level[@kernel.lit_var(learned[1])]
|
|
for k = 2; k < learned.length(); k = k + 1 {
|
|
let lvl = s.level[@kernel.lit_var(learned[k])]
|
|
if lvl > best_lvl {
|
|
best_lvl = lvl
|
|
best = k
|
|
}
|
|
}
|
|
swap_idx(learned, 1, best)
|
|
bt_level = best_lvl
|
|
}
|
|
(learned, bt_level, ants)
|
|
}
|
|
|
|
///|
|
|
async fn add_req_clause(s : SatSolver, req : SolveReq) -> Int32 raise OrbitError {
|
|
let (ch_name, var_ids) = ensure_pkg_channel(s, req.name, req.channel)
|
|
let matches = matching_vars(s, var_ids, req.constraint)
|
|
let lits : Array[Int32] = []
|
|
for vid in matches {
|
|
lits.push(@kernel.pos_lit(vid))
|
|
}
|
|
add_clause_dynamic(
|
|
s,
|
|
lits,
|
|
Root(req.label, req.name, Some(ch_name), req.constraint),
|
|
)
|
|
}
|
|
|
|
///|
|
|
fn first_unexpanded_true(s : SatSolver) -> Int32? {
|
|
for t in s.trail {
|
|
if !@kernel.lit_is_neg(t) {
|
|
let v = @kernel.lit_var(t)
|
|
if !s.expanded[v] {
|
|
return Some(v)
|
|
}
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
///|
|
|
/// Expand the next unassigned-deps candidate on the trail.
|
|
/// Returns (expanded_something, conflict_cid).
|
|
async fn expand_next(s : SatSolver) -> (Bool, Int32) raise OrbitError {
|
|
let v = match first_unexpanded_true(s) {
|
|
Some(vid) => vid
|
|
None => return (false, -1)
|
|
}
|
|
s.expanded[v] = true
|
|
let cand = s.vars[v]
|
|
let det = provider_details(s.provider, cand)
|
|
s.details[v] = Some(det)
|
|
|
|
let saved_lvl = current_level(s)
|
|
let mut confl : Int32 = -1
|
|
let record_confl = fn(c : Int32) {
|
|
if c >= 0 && confl < 0 {
|
|
confl = c
|
|
}
|
|
}
|
|
|
|
// 1. Add dependency implication clauses `[¬v, m1, m2, ...]`.
|
|
for dep_key, dep_con in det.manifest.depends {
|
|
let (dep_pin, dep_name) = parse_dep_key(dep_key)
|
|
let (_, var_ids) = ensure_pkg_channel(s, dep_name, dep_pin)
|
|
let matches = matching_vars(s, var_ids, dep_con)
|
|
let lits : Array[Int32] = [@kernel.neg_lit(v)]
|
|
for mvid in matches {
|
|
lits.push(@kernel.pos_lit(mvid))
|
|
}
|
|
let c = add_clause_dynamic(
|
|
s,
|
|
lits,
|
|
Dep(v, dep_name, dep_pin, dep_con),
|
|
)
|
|
record_confl(c)
|
|
}
|
|
|
|
// 2. Check binary-name collisions against locked global packages (skipped if
|
|
// that package is part of this solve) and against active candidates.
|
|
for b in det.manifest.bins {
|
|
let bn = basename(b)
|
|
match s.provider.locked_bins.get(bn) {
|
|
Some(owner) =>
|
|
if owner != cand.name && !s.pkg_index.contains(owner) {
|
|
let c = add_clause_dynamic(
|
|
s,
|
|
[@kernel.neg_lit(v)],
|
|
LockedBinConflict(bn, v, owner),
|
|
)
|
|
record_confl(c)
|
|
}
|
|
None => ()
|
|
}
|
|
let provs = match s.bin_providers.get(bn) {
|
|
Some(xs) => xs
|
|
None => {
|
|
let xs : Array[Int32] = []
|
|
s.bin_providers[bn] = xs
|
|
xs
|
|
}
|
|
}
|
|
for u in provs {
|
|
if s.var_pkg[u] != s.var_pkg[v] {
|
|
let c = add_clause_dynamic(
|
|
s,
|
|
[@kernel.neg_lit(v), @kernel.neg_lit(u)],
|
|
BinConflict(bn, v, u),
|
|
)
|
|
record_confl(c)
|
|
}
|
|
}
|
|
provs.push(v)
|
|
}
|
|
|
|
// 3. Flush any extra requirements discovered when dependencies first loaded.
|
|
while s.pending_reqs.length() > 0 {
|
|
let req = s.pending_reqs.remove(0)
|
|
let c = add_req_clause(s, req)
|
|
if c >= 0 {
|
|
return (true, c)
|
|
}
|
|
}
|
|
if current_level(s) < saved_lvl {
|
|
return (true, -1)
|
|
}
|
|
(true, confl)
|
|
}
|
|
|
|
///|
|
|
/// Pick the next decision literal using MRV on active positive-choice clauses.
|
|
/// Returns None when every active requirement is satisfied.
|
|
fn pick_decision(s : SatSolver) -> Int32? {
|
|
let mut best_lit : Int32 = -1
|
|
let mut best_count : Int32 = 2147483647
|
|
for cl in s.clauses {
|
|
let mut satisfied = false
|
|
let mut blocked_by_guard = false
|
|
let mut first_pos : Int32 = -1
|
|
let mut pos_count : Int32 = 0
|
|
for l in cl.lits {
|
|
let val = lit_val(s, l)
|
|
if val == True {
|
|
satisfied = true
|
|
break
|
|
}
|
|
if val == Unassigned {
|
|
if @kernel.lit_is_neg(l) {
|
|
blocked_by_guard = true
|
|
break
|
|
}
|
|
if first_pos < 0 {
|
|
first_pos = l
|
|
}
|
|
pos_count = pos_count + 1
|
|
}
|
|
}
|
|
if !satisfied && !blocked_by_guard && pos_count > 0 && pos_count < best_count {
|
|
best_count = pos_count
|
|
best_lit = first_pos
|
|
if best_count == 1 {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if best_lit >= 0 {
|
|
Some(best_lit)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn format_channel_prefix(pin : String?) -> String {
|
|
match pin {
|
|
Some(ch) => ch + "::"
|
|
None => ""
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn explain_unsat(s : SatSolver, confl : Int32) -> String {
|
|
let visited : Array[Bool] = Array::make(s.clauses.length(), false)
|
|
let queue : Array[Int32] = [confl]
|
|
visited[confl] = true
|
|
let lines : Array[String] = []
|
|
let push_line = fn(line : String) {
|
|
if !lines.contains(line) {
|
|
lines.push(line)
|
|
}
|
|
}
|
|
while queue.length() > 0 {
|
|
let cid = queue.remove(0)
|
|
let cl = s.clauses[cid]
|
|
match cl.origin {
|
|
Learned(ants) =>
|
|
for a in ants {
|
|
if a >= 0 && a < visited.length() && !visited[a] {
|
|
visited[a] = true
|
|
queue.push(a)
|
|
}
|
|
}
|
|
Root(label, name, pin, constraint) =>
|
|
push_line(
|
|
" - \{label} requires \{format_channel_prefix(pin)}\{name} \{constraint}",
|
|
)
|
|
Dep(pvid, dep_name, pin, constraint) => {
|
|
let p = s.vars[pvid]
|
|
push_line(
|
|
" - \{p.name} \{p.version} requires \{format_channel_prefix(pin)}\{dep_name} \{constraint}",
|
|
)
|
|
}
|
|
AtMostOne(name) =>
|
|
push_line(" - only one version of \{name} can be installed")
|
|
BinConflict(bin, v1, v2) => {
|
|
let c1 = s.vars[v1]
|
|
let c2 = s.vars[v2]
|
|
push_line(
|
|
" - \{c1.name} \{c1.version} and \{c2.name} \{c2.version} both provide binary '\{bin}'",
|
|
)
|
|
}
|
|
LockedBinConflict(bin, vid, owner) => {
|
|
let c = s.vars[vid]
|
|
push_line(
|
|
" - \{c.name} \{c.version} provides binary '\{bin}', already provided by installed \{owner}",
|
|
)
|
|
}
|
|
}
|
|
for l in cl.lits {
|
|
let v = @kernel.lit_var(l)
|
|
if s.assign[v] != Unassigned && s.level[v] == 0 {
|
|
let r = s.reason[v]
|
|
if r >= 0 && r < visited.length() && !visited[r] {
|
|
visited[r] = true
|
|
queue.push(r)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if lines.length() == 0 {
|
|
return "dependency resolution failed: unsatisfiable constraints"
|
|
}
|
|
"cannot satisfy dependencies:\n" + join(lines, "\n")
|
|
}
|
|
|
|
///|
|
|
fn topo_sort_solution(s : SatSolver) -> Array[SolvedPkg] {
|
|
let chosen : Map[String, SolvedPkg] = Map([])
|
|
let names : Array[String] = []
|
|
for vid = 0; vid < s.vars.length(); vid = vid + 1 {
|
|
if s.assign[vid] == True {
|
|
let c = s.vars[vid]
|
|
let det = s.details[vid].unwrap()
|
|
let pkg = SolvedPkg::{
|
|
name: c.name,
|
|
channel: c.channel,
|
|
tag: c.tag,
|
|
version: det.manifest.version,
|
|
digest: det.digest,
|
|
manifest: det.manifest,
|
|
}
|
|
chosen[c.name] = pkg
|
|
names.push(c.name)
|
|
}
|
|
}
|
|
let state : Map[String, Int32] = Map([]) // 1 = visiting, 2 = done
|
|
let out : Array[SolvedPkg] = []
|
|
fn dfs(name : String) -> Unit {
|
|
match state.get(name) {
|
|
Some(1) | Some(2) => return
|
|
_ => ()
|
|
}
|
|
state[name] = 1
|
|
match chosen.get(name) {
|
|
Some(pkg) => {
|
|
for dep_key, _ in pkg.manifest.depends {
|
|
let (_, dep_name) = parse_dep_key(dep_key)
|
|
if chosen.contains(dep_name) {
|
|
dfs(dep_name)
|
|
}
|
|
}
|
|
state[name] = 2
|
|
out.push(pkg)
|
|
}
|
|
None => state[name] = 2
|
|
}
|
|
}
|
|
for name in names {
|
|
dfs(name)
|
|
}
|
|
out
|
|
}
|
|
|
|
///|
|
|
async fn solve(
|
|
reqs : Array[SolveReq],
|
|
provider : SolverProvider,
|
|
) -> Array[SolvedPkg] raise OrbitError {
|
|
let s = new_solver(provider)
|
|
for req in reqs {
|
|
let (ch_name, var_ids) = ensure_pkg_channel(s, req.name, req.channel)
|
|
let matches = matching_vars(s, var_ids, req.constraint)
|
|
if matches.length() == 0 {
|
|
let avail = available_labels(s, var_ids)
|
|
raise OrbitError(
|
|
"no version of \{req.name} in channel '\{ch_name}' satisfies '\{req.constraint}'; available: \{avail}",
|
|
)
|
|
}
|
|
let lits : Array[Int32] = []
|
|
for vid in matches {
|
|
lits.push(@kernel.pos_lit(vid))
|
|
}
|
|
let c = add_clause_dynamic(
|
|
s,
|
|
lits,
|
|
Root(req.label, req.name, Some(ch_name), req.constraint),
|
|
)
|
|
if c >= 0 {
|
|
raise OrbitError(explain_unsat(s, c))
|
|
}
|
|
}
|
|
while s.pending_reqs.length() > 0 {
|
|
let req = s.pending_reqs.remove(0)
|
|
let c = add_req_clause(s, req)
|
|
if c >= 0 {
|
|
raise OrbitError(explain_unsat(s, c))
|
|
}
|
|
}
|
|
|
|
let mut confl : Int32 = -1
|
|
while true {
|
|
if confl < 0 {
|
|
confl = propagate(s)
|
|
}
|
|
if confl >= 0 {
|
|
if current_level(s) == 0 {
|
|
raise OrbitError(explain_unsat(s, confl))
|
|
}
|
|
let (learned, bt_level, ants) = analyze_conflict(s, confl)
|
|
backtrack(s, bt_level)
|
|
let c = add_clause_dynamic(s, learned, Learned(ants))
|
|
confl = c
|
|
continue
|
|
}
|
|
let (expanded_any, exp_confl) = expand_next(s)
|
|
if exp_confl >= 0 {
|
|
confl = exp_confl
|
|
continue
|
|
}
|
|
if expanded_any {
|
|
continue
|
|
}
|
|
match pick_decision(s) {
|
|
Some(dec_lit) => {
|
|
s.trail_lim.push(s.trail.length())
|
|
enqueue(s, dec_lit, -1)
|
|
}
|
|
None => return topo_sort_solution(s)
|
|
}
|
|
}
|
|
[]
|
|
}
|
|
|
|
// ---------------------------------------------------------------- unit tests
|
|
|
|
///|
|
|
struct MockEntry {
|
|
channel : String
|
|
tag : String
|
|
manifest : Manifest
|
|
favored : Bool
|
|
}
|
|
|
|
///|
|
|
fn mock_manifest(
|
|
name : String,
|
|
version : String,
|
|
depends : Map[String, String],
|
|
bins? : Array[String] = [],
|
|
) -> Manifest {
|
|
Manifest::{
|
|
name,
|
|
version,
|
|
description: "",
|
|
documentation: "",
|
|
depends,
|
|
bins,
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn make_mock_provider(
|
|
repo : Map[String, Array[MockEntry]],
|
|
detail_fetches? : Ref[Int32] = Ref(0),
|
|
extra_reqs_map? : Map[String, Array[SolveReq]] = Map([]),
|
|
locked_bins? : Map[String, String] = Map([]),
|
|
) -> SolverProvider {
|
|
let by_id : Map[String, Manifest] = Map([])
|
|
for _, entries in repo {
|
|
for e in entries {
|
|
by_id[e.channel + "::" + e.manifest.name + "@" + e.tag] = e.manifest
|
|
}
|
|
}
|
|
SolverProvider::{
|
|
candidates: async fn(name, pin) {
|
|
if false {
|
|
@async.sleep(0)
|
|
}
|
|
let entries = match repo.get(name) {
|
|
Some(xs) => xs
|
|
None => raise OrbitError("package '\{name}' not found")
|
|
}
|
|
let ch = match pin {
|
|
Some(c) => c
|
|
None => {
|
|
if entries.length() == 0 {
|
|
raise OrbitError("package '\{name}' has no candidates")
|
|
}
|
|
entries[0].channel
|
|
}
|
|
}
|
|
let tags : Array[String] = []
|
|
let mut fav : (String, String, String)? = None
|
|
for e in entries {
|
|
if e.channel == ch {
|
|
tags.push(e.tag)
|
|
if e.favored {
|
|
fav = Some((ch, e.tag, "sha256:fav-" + name + "-" + e.tag))
|
|
}
|
|
}
|
|
}
|
|
if tags.length() == 0 {
|
|
raise OrbitError("package '\{name}' not found in channel '\{ch}'")
|
|
}
|
|
(ch, candidates_from_tags(name, ch, tags, fav))
|
|
},
|
|
details: async fn(cand) {
|
|
if false {
|
|
@async.sleep(0)
|
|
}
|
|
detail_fetches.val = detail_fetches.val + 1
|
|
let key = cand.channel + "::" + cand.name + "@" + cand.tag
|
|
let m = match by_id.get(key) {
|
|
Some(m) => m
|
|
None => raise OrbitError("missing mock manifest " + key)
|
|
}
|
|
CandidateDetail::{
|
|
digest: "sha256:" + cand.channel + "-" + cand.name + "-" + cand.tag,
|
|
manifest: m,
|
|
}
|
|
},
|
|
extra_reqs: fn(name) {
|
|
match extra_reqs_map.get(name) {
|
|
Some(xs) => xs
|
|
None => []
|
|
}
|
|
},
|
|
locked_bins,
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn solved_map(xs : Array[SolvedPkg]) -> Map[String, String] {
|
|
let m : Map[String, String] = Map([])
|
|
for p in xs {
|
|
m[p.name] = p.version
|
|
}
|
|
m
|
|
}
|
|
|
|
///|
|
|
async test "solver basic transitive order and diamond backtracking" {
|
|
let repo : Map[String, Array[MockEntry]] = Map([
|
|
(
|
|
"lib",
|
|
[
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "2.0.0",
|
|
manifest: mock_manifest("lib", "2.0.0", Map([])),
|
|
favored: false,
|
|
},
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "1.0.0",
|
|
manifest: mock_manifest("lib", "1.0.0", Map([])),
|
|
favored: false,
|
|
},
|
|
],
|
|
),
|
|
(
|
|
"app",
|
|
[
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "2.0.0",
|
|
manifest: mock_manifest("app", "2.0.0", Map([("lib", "^2.0")])),
|
|
favored: false,
|
|
},
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "1.0.0",
|
|
manifest: mock_manifest("app", "1.0.0", Map([("lib", "^1.0")])),
|
|
favored: false,
|
|
},
|
|
],
|
|
),
|
|
(
|
|
"helper",
|
|
[
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "1.0.0",
|
|
manifest: mock_manifest("helper", "1.0.0", Map([("lib", "^1.0")])),
|
|
favored: false,
|
|
},
|
|
],
|
|
),
|
|
])
|
|
let sol = solve(
|
|
[
|
|
SolveReq::{ name: "app", channel: None, constraint: "*", label: "root" },
|
|
SolveReq::{
|
|
name: "helper",
|
|
channel: None,
|
|
constraint: "*",
|
|
label: "root",
|
|
},
|
|
],
|
|
make_mock_provider(repo),
|
|
)
|
|
let m = solved_map(sol)
|
|
assert_eq(m.get("app"), Some("1.0.0"))
|
|
assert_eq(m.get("helper"), Some("1.0.0"))
|
|
assert_eq(m.get("lib"), Some("1.0.0"))
|
|
// Dependencies come before dependents in topological order.
|
|
let names = sol.map(fn(p) { p.name })
|
|
let mut lib_idx : Int32 = -1
|
|
let mut app_idx : Int32 = -1
|
|
for i, n in names {
|
|
if n == "lib" {
|
|
lib_idx = i
|
|
}
|
|
if n == "app" {
|
|
app_idx = i
|
|
}
|
|
}
|
|
assert_true(lib_idx >= 0 && app_idx > lib_idx)
|
|
}
|
|
|
|
///|
|
|
async test "solver deep backtracking, compound constraints, and lazy fetch count" {
|
|
let repo : Map[String, Array[MockEntry]] = Map([
|
|
(
|
|
"top",
|
|
[
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "3.0.0",
|
|
manifest: mock_manifest("top", "3.0.0", Map([("mid", "^3.0")])),
|
|
favored: false,
|
|
},
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "2.0.0",
|
|
manifest: mock_manifest("top", "2.0.0", Map([("mid", "^2.0")])),
|
|
favored: false,
|
|
},
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "1.0.0",
|
|
manifest: mock_manifest("top", "1.0.0", Map([("mid", "^1.0")])),
|
|
favored: false,
|
|
},
|
|
],
|
|
),
|
|
(
|
|
"mid",
|
|
[
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "3.0.0",
|
|
manifest: mock_manifest("mid", "3.0.0", Map([("leaf", "^3.0")])),
|
|
favored: false,
|
|
},
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "2.0.0",
|
|
manifest: mock_manifest("mid", "2.0.0", Map([("leaf", "^2.0")])),
|
|
favored: false,
|
|
},
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "1.5.0",
|
|
manifest: mock_manifest("mid", "1.5.0", Map([("leaf", "^1.0")])),
|
|
favored: false,
|
|
},
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "1.4.0",
|
|
manifest: mock_manifest("mid", "1.4.0", Map([("leaf", "^1.0")])),
|
|
favored: false,
|
|
},
|
|
],
|
|
),
|
|
(
|
|
"leaf",
|
|
[
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "3.0.0",
|
|
manifest: mock_manifest("leaf", "3.0.0", Map([])),
|
|
favored: false,
|
|
},
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "2.0.0",
|
|
manifest: mock_manifest("leaf", "2.0.0", Map([])),
|
|
favored: false,
|
|
},
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "1.2.0",
|
|
manifest: mock_manifest("leaf", "1.2.0", Map([])),
|
|
favored: false,
|
|
},
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "1.1.0",
|
|
manifest: mock_manifest("leaf", "1.1.0", Map([])),
|
|
favored: false,
|
|
},
|
|
],
|
|
),
|
|
(
|
|
"pin",
|
|
[
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "1.0.0",
|
|
manifest: mock_manifest(
|
|
"pin",
|
|
"1.0.0",
|
|
Map([("leaf", ">=1.0.0, <2.0.0, !=1.2.0")]),
|
|
),
|
|
favored: false,
|
|
},
|
|
],
|
|
),
|
|
])
|
|
let fetches : Ref[Int32] = Ref(0)
|
|
let sol = solve(
|
|
[
|
|
SolveReq::{ name: "top", channel: None, constraint: "*", label: "root" },
|
|
SolveReq::{ name: "pin", channel: None, constraint: "*", label: "root" },
|
|
],
|
|
make_mock_provider(repo, detail_fetches=fetches),
|
|
)
|
|
let m = solved_map(sol)
|
|
assert_eq(m.get("top"), Some("1.0.0"))
|
|
assert_eq(m.get("mid"), Some("1.5.0"))
|
|
assert_eq(m.get("leaf"), Some("1.1.0"))
|
|
// Lazy check: mid@1.4.0 and leaf@1.2.0 (if not needed? Actually leaf@1.2.0
|
|
// wasn't even tried because pin restricted leaf to !=1.2.0, and mid@1.4.0 was
|
|
// never fetched because mid@1.5.0 succeeded!). Total entries = 11; fetches < 11.
|
|
assert_true(fetches.val < 11)
|
|
}
|
|
|
|
///|
|
|
async test "solver handles cycles, favored stability, channel pins, and bin conflicts" {
|
|
// Cyclic deps + favored stability
|
|
let cyc_repo : Map[String, Array[MockEntry]] = Map([
|
|
(
|
|
"a",
|
|
[
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "1.2.0",
|
|
manifest: mock_manifest("a", "1.2.0", Map([("b", "^1.0")])),
|
|
favored: false,
|
|
},
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "1.1.0",
|
|
manifest: mock_manifest("a", "1.1.0", Map([("b", "^1.0")])),
|
|
favored: true,
|
|
},
|
|
],
|
|
),
|
|
(
|
|
"b",
|
|
[
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "1.1.0",
|
|
manifest: mock_manifest("b", "1.1.0", Map([("a", "^1.0")])),
|
|
favored: false,
|
|
},
|
|
],
|
|
),
|
|
])
|
|
let sol = solve(
|
|
[SolveReq::{ name: "a", channel: None, constraint: "^1.0", label: "root" }],
|
|
make_mock_provider(cyc_repo),
|
|
)
|
|
let m = solved_map(sol)
|
|
// Favored 1.1.0 stays preferred when ^1.0 is satisfied.
|
|
assert_eq(m.get("a"), Some("1.1.0"))
|
|
assert_eq(m.get("b"), Some("1.1.0"))
|
|
|
|
// Moving constraint forces moving off favored 1.1.0 to 1.2.0.
|
|
let sol2 = solve(
|
|
[SolveReq::{ name: "a", channel: None, constraint: ">=1.2.0", label: "root" }],
|
|
make_mock_provider(cyc_repo),
|
|
)
|
|
assert_eq(solved_map(sol2).get("a"), Some("1.2.0"))
|
|
|
|
// Channel pin + binary conflict backtracking
|
|
let bin_repo : Map[String, Array[MockEntry]] = Map([
|
|
(
|
|
"x",
|
|
[
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "2.0.0",
|
|
manifest: mock_manifest("x", "2.0.0", Map([]), bins=["bin/cli"]),
|
|
favored: false,
|
|
},
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "1.0.0",
|
|
manifest: mock_manifest("x", "1.0.0", Map([]), bins=["bin/x"]),
|
|
favored: false,
|
|
},
|
|
],
|
|
),
|
|
(
|
|
"y",
|
|
[
|
|
MockEntry::{
|
|
channel: "extra",
|
|
tag: "1.0.0",
|
|
manifest: mock_manifest("y", "1.0.0", Map([]), bins=["bin/cli"]),
|
|
favored: false,
|
|
},
|
|
],
|
|
),
|
|
])
|
|
let sol3 = solve(
|
|
[
|
|
SolveReq::{ name: "x", channel: None, constraint: "*", label: "root" },
|
|
SolveReq::{
|
|
name: "y",
|
|
channel: Some("extra"),
|
|
constraint: "*",
|
|
label: "root",
|
|
},
|
|
],
|
|
make_mock_provider(bin_repo),
|
|
)
|
|
let m3 = solved_map(sol3)
|
|
assert_eq(m3.get("x"), Some("1.0.0"))
|
|
assert_eq(m3.get("y"), Some("1.0.0"))
|
|
}
|
|
|
|
///|
|
|
async test "solver UNSAT explanation, channel conflict, and non-semver tags" {
|
|
let repo : Map[String, Array[MockEntry]] = Map([
|
|
(
|
|
"a",
|
|
[
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "1.0.0",
|
|
manifest: mock_manifest("a", "1.0.0", Map([("c", "^1.0")])),
|
|
favored: false,
|
|
},
|
|
],
|
|
),
|
|
(
|
|
"b",
|
|
[
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "1.0.0",
|
|
manifest: mock_manifest("b", "1.0.0", Map([("c", "^2.0")])),
|
|
favored: false,
|
|
},
|
|
],
|
|
),
|
|
(
|
|
"c",
|
|
[
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "2.0.0",
|
|
manifest: mock_manifest("c", "2.0.0", Map([])),
|
|
favored: false,
|
|
},
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "1.0.0",
|
|
manifest: mock_manifest("c", "1.0.0", Map([])),
|
|
favored: false,
|
|
},
|
|
],
|
|
),
|
|
(
|
|
"nightly",
|
|
[
|
|
MockEntry::{
|
|
channel: "main",
|
|
tag: "edge",
|
|
manifest: mock_manifest("nightly", "0.0.1", Map([])),
|
|
favored: false,
|
|
},
|
|
],
|
|
),
|
|
])
|
|
let err_msg = try {
|
|
ignore(
|
|
solve(
|
|
[
|
|
SolveReq::{ name: "a", channel: None, constraint: "*", label: "root" },
|
|
SolveReq::{ name: "b", channel: None, constraint: "*", label: "root" },
|
|
],
|
|
make_mock_provider(repo),
|
|
),
|
|
)
|
|
""
|
|
} catch {
|
|
OrbitError(m) => m
|
|
}
|
|
assert_true(contains_str(err_msg, "a 1.0.0 requires c ^1.0"))
|
|
assert_true(contains_str(err_msg, "b 1.0.0 requires c ^2.0"))
|
|
|
|
// Non-semver literal tag "edge"
|
|
let sol_edge = solve(
|
|
[
|
|
SolveReq::{
|
|
name: "nightly",
|
|
channel: None,
|
|
constraint: "edge",
|
|
label: "root",
|
|
},
|
|
],
|
|
make_mock_provider(repo),
|
|
)
|
|
assert_eq(sol_edge[0].tag, "edge")
|
|
assert_eq(sol_edge[0].version, "0.0.1")
|
|
}
|