Add Pixi-style project environments and SAT dependency solver

- Add orbit.toml project manifests, deterministic orbit.lock files, [tasks], and `orbit run`
- Add per-project .orbit/bin environments; `-g`/`--global` targets the global environment
- Replace greedy global resolution with a lazy CDCL SAT solver that explains conflicts
- Support channel::pkg dependency pins and comma-separated, comparison, and wildcard constraints
- Cache immutable sha256 manifests under ~/.orbit/cache/manifests
- Validate dependency keys and constraints in orbit.json
This commit is contained in:
nandi committed 2026-11-28 01:43:04 -08:00
1 parent 0919ce105c
commit 13aa7d7f77
9 files changed
+4563 -266

No files matched your search

+277 -190
View File
@@ -8,19 +8,11 @@ struct Step {
version : String
digest : String
manifest : Manifest
mut explicit : Bool
mut constraint : String
explicit : Bool
constraint : String
keep : Bool // already installed at this digest; nothing to fetch
}
///|
struct Planner {
ctx : Ctx
steps : Map[String, Step]
order : Array[Step] // dependencies before dependents
upgrade : Array[String] // names allowed to move even if satisfied
}
///|
fn manifest_of_installed(p : Installed) -> Manifest {
Manifest::{
@@ -34,172 +26,217 @@ fn manifest_of_installed(p : Installed) -> Manifest {
}
///|
/// Every constraint that currently applies to `name`: the request itself,
/// the user's own constraint if they installed it explicitly, and what each
/// other package requires (using planned manifests for packages being replaced).
fn constraints_for(
p : Planner,
name : String,
requested : String,
) -> Array[String] {
let cons : Array[String] = [requested]
match p.ctx.state.get(name) {
Some(inst) =>
if inst.explicit && inst.constraint != "" && inst.constraint != requested {
cons.push(inst.constraint)
/// "[channel::]name[@constraint]" -> (channel, name, constraint, has_explicit_constraint)
fn parse_spec_full(
spec : String,
) -> (String?, String, String, Bool) raise OrbitError {
let (channel, rest) = split_channel(spec)
match channel {
Some(ch) =>
if !valid_name(ch) {
raise OrbitError("invalid channel name '\{ch}'")
}
None => ()
}
for other, inst in p.ctx.state {
if other == name {
continue
}
let deps = match p.steps.get(other) {
Some(st) => st.manifest.depends
None => inst.depends
}
match deps.get(name) {
Some(c) => cons.push(c)
None => ()
let (name, c) = split_first(rest, '@')
if !valid_name(name) {
raise OrbitError("invalid package name '\{name}'")
}
if c == "" {
(channel, name, "*", false)
} else {
if !valid_constraint(c) {
raise OrbitError("invalid version constraint '\{c}' for '\{name}'")
}
(channel, name, c, true)
}
for other, st in p.steps {
if other != name && !p.ctx.state.contains(other) {
match st.manifest.depends.get(name) {
Some(c) => cons.push(c)
None => ()
}
}
}
cons
}
///|
async fn visit(
p : Planner,
name : String,
requested : String,
explicit : Bool,
pin : String?, // channel the user asked for
) -> Unit raise OrbitError {
match p.steps.get(name) {
Some(st) => {
if pin is Some(c) && c != st.channel {
raise OrbitError(
"conflict: \{name} is planned from channel '\{st.channel}' but '\{c}' was requested",
)
}
if !satisfies_all(st.version, [requested]) {
raise OrbitError(
"conflict: \{name} \{st.version} is planned but '\{requested}' is required",
)
}
if explicit {
st.explicit = true
st.constraint = requested
}
return
}
None => ()
}
let cons = constraints_for(p, name, requested)
let upgrading = p.upgrade.contains(name)
match p.ctx.state.get(name) {
Some(inst) => {
let same_channel = match pin {
Some(c) => c == inst.channel
None => true
}
if !upgrading && same_channel && satisfies_all(inst.version, cons) {
let st = Step::{
name,
channel: inst.channel,
version: inst.version,
digest: inst.digest,
manifest: manifest_of_installed(inst),
explicit: explicit || inst.explicit,
constraint: if explicit {
requested
} else {
inst.constraint
},
keep: true,
}
p.steps[name] = st
p.order.push(st)
return
}
}
None => ()
}
// An installed package stays on its channel unless the user names another.
let wanted = match pin {
Some(c) => Some(c)
None => p.ctx.state.get(name).map(fn(i) { i.channel })
}
let (ch, tags) = locate(p.ctx, name, wanted)
let tag = match pick_tag(tags, cons) {
Some(t) => t
None => {
let avail = sorted_versions(tags)
raise OrbitError(
"no version of \{name} in channel '\{ch.name}' satisfies [\{join(cons, ", ")}]; available: \{join(avail, ", ")}",
)
}
}
let digest = resolve_digest(ch, name, tag)
let prev = p.ctx.state.get(name)
// An unchanged build with a saved description needs no metadata fetch.
let m = match prev {
Some(i) =>
if i.digest == digest && i.description != "" {
manifest_of_installed(i)
} else {
fetch_manifest(ch, name, digest)
}
None => fetch_manifest(ch, name, digest)
}
let st = Step::{
name,
channel: ch.name,
version: m.version,
digest,
manifest: m,
explicit: explicit ||
(match prev {
Some(i) => i.explicit
None => false
}),
constraint: if explicit {
requested
} else {
match prev {
Some(i) => i.constraint
None => "*"
}
},
keep: match prev {
Some(i) => i.digest == digest
None => false
},
}
// Register before recursing so dependency cycles terminate.
p.steps[name] = st
for dep, dc in m.depends {
visit(p, dep, dc, false, None)
}
p.order.push(st)
}
///|
/// "[channel::]name[@constraint]"
fn parse_spec(spec : String) -> (String?, String, String) raise OrbitError {
let (channel, rest) = split_channel(spec)
let (name, c) = split_first(rest, '@')
if !valid_name(name) {
raise OrbitError("invalid package name '\{name}'")
let (channel, name, c, _) = parse_spec_full(spec)
(channel, name, c)
}
///|
/// Build a SAT-solved install/upgrade plan for global state.
async fn plan_global(
ctx : Ctx,
req_specs : Array[(String?, String, String, String)],
upgrade : Array[String],
explicit_new : Map[String, String],
) -> Array[Step] raise OrbitError {
let ch_by_name : Map[String, Channel] = Map([])
for c in ctx.channels {
ch_by_name[c.name] = c
}
(channel, name, if c == "" { "*" } else { c })
let tags_cache : Map[String, (Channel, Array[String])] = Map([])
let digest_cache : Map[String, String] = Map([])
let manifest_cache : Map[String, Manifest] = Map([])
let locked_bins : Map[String, String] = Map([])
for name, inst in ctx.state {
if !upgrade.contains(name) && !explicit_new.contains(name) {
for b in inst.bins {
locked_bins[basename(b)] = name
}
}
}
let provider = SolverProvider::{
candidates: async fn(name, pin) {
let wanted = match pin {
Some(c) => Some(c)
None =>
match ctx.state.get(name) {
Some(inst) => Some(inst.channel)
None => None
}
}
let cache_key = name + "|" + wanted.unwrap_or("")
let (ch, tags) = match tags_cache.get(cache_key) {
Some(pair) => pair
None => {
let pair = locate(ctx, name, wanted)
tags_cache[cache_key] = pair
ch_by_name[pair.0.name] = pair.0
pair
}
}
let favored = if upgrade.contains(name) {
None
} else {
match ctx.state.get(name) {
Some(inst) =>
if inst.channel == ch.name {
Some((inst.channel, inst.version, inst.digest))
} else {
None
}
None => None
}
}
(ch.name, candidates_from_tags(name, ch.name, tags, favored))
},
details: async fn(cand) {
if false {
@async.sleep(0)
}
if cand.favored && cand.digest != "" {
match ctx.state.get(cand.name) {
Some(inst) =>
if inst.digest == cand.digest && inst.channel == cand.channel {
return CandidateDetail::{
digest: cand.digest,
manifest: manifest_of_installed(inst),
}
}
None => ()
}
}
let ch = match ch_by_name.get(cand.channel) {
Some(c) => c
None => find_channel(ctx, cand.channel)
}
let dkey = cand.channel + "::" + cand.name + "@" + cand.tag
let digest = match digest_cache.get(dkey) {
Some(d) => d
None => {
let d = resolve_digest(ch, cand.name, cand.tag)
digest_cache[dkey] = d
d
}
}
let mkey = cand.channel + "::" + cand.name + "@" + digest
let m = match manifest_cache.get(mkey) {
Some(mf) => mf
None => {
let mf = fetch_manifest_cached(ctx.root, ch, cand.name, digest)
manifest_cache[mkey] = mf
mf
}
}
CandidateDetail::{ digest, manifest: m }
},
extra_reqs: fn(pkg_name) {
let reqs : Array[SolveReq] = []
match ctx.state.get(pkg_name) {
Some(inst) =>
if inst.explicit && !explicit_new.contains(pkg_name) {
reqs.push(SolveReq::{
name: pkg_name,
channel: Some(inst.channel),
constraint: if inst.constraint == "" {
"*"
} else {
inst.constraint
},
label: "installed \{pkg_name}",
})
}
None => ()
}
for other, oinst in ctx.state {
if other != pkg_name &&
!upgrade.contains(other) &&
!explicit_new.contains(other) {
for dk, dc in oinst.depends {
let (dch, dname) = parse_dep_key(dk)
if dname == pkg_name {
reqs.push(SolveReq::{
name: pkg_name,
channel: dch,
constraint: dc,
label: "installed \{other} \{oinst.version}",
})
}
}
}
}
reqs
},
locked_bins,
}
let reqs : Array[SolveReq] = []
for item in req_specs {
let (pin, name, constraint, label) = item
reqs.push(SolveReq::{ name, channel: pin, constraint, label })
}
let solved = solve(reqs, provider)
let steps : Array[Step] = []
for sp in solved {
let prev = ctx.state.get(sp.name)
let explicit = explicit_new.contains(sp.name) ||
(match prev {
Some(i) => i.explicit
None => false
})
let constraint = match explicit_new.get(sp.name) {
Some(c) => c
None =>
match prev {
Some(i) => i.constraint
None => "*"
}
}
let keep = match prev {
Some(i) => i.digest == sp.digest && i.channel == sp.channel
None => false
}
steps.push(Step::{
name: sp.name,
channel: sp.channel,
version: sp.version,
digest: sp.digest,
manifest: sp.manifest,
explicit,
constraint,
keep,
})
}
steps
}
///|
@@ -321,9 +358,9 @@ async fn activate(ctx : Ctx, st : Step) -> Unit raise OrbitError {
}
///|
async fn apply_plan(ctx : Ctx, p : Planner) -> Unit raise OrbitError {
async fn apply_plan(ctx : Ctx, steps : Array[Step]) -> Unit raise OrbitError {
let mut changed = 0
for st in p.order {
for st in steps {
if st.keep {
match ctx.state.get(st.name) {
Some(inst) => {
@@ -345,57 +382,106 @@ async fn apply_plan(ctx : Ctx, p : Planner) -> Unit raise OrbitError {
}
}
///|
fn new_planner(ctx : Ctx, upgrade : Array[String]) -> Planner {
Planner::{ ctx, steps: Map([]), order: [], upgrade, }
}
///|
async fn cmd_install(ctx : Ctx, specs : Array[String]) -> Unit raise OrbitError {
if specs.length() == 0 {
raise OrbitError("usage: orbit install [channel::]<name>[@constraint]...")
}
let p = new_planner(ctx, [])
let reqs : Array[(String?, String, String, String)] = []
let explicit_new : Map[String, String] = Map([])
for s in specs {
let (channel, name, c) = parse_spec(s)
visit(p, name, c, true, channel)
reqs.push((channel, name, c, "command line"))
explicit_new[name] = c
}
apply_plan(ctx, p)
let steps = plan_global(ctx, reqs, [], explicit_new)
apply_plan(ctx, steps)
}
///|
/// Upgrade the named packages (or everything) to the newest versions their
/// constraints allow. Dependencies are moved only when required.
/// Upgrade the named packages (or everything) jointly to the newest versions
/// their constraints allow.
async fn cmd_upgrade(ctx : Ctx, names : Array[String]) -> Unit raise OrbitError {
let targets : Array[String] = []
let reqs : Array[(String?, String, String, String)] = []
if names.length() == 0 {
for name, _ in ctx.state {
if ctx.state.length() == 0 {
println("no packages installed")
return
}
let mut has_explicit = false
for name, inst in ctx.state {
targets.push(name)
if inst.explicit {
has_explicit = true
reqs.push((
Some(inst.channel),
name,
if inst.constraint == "" {
"*"
} else {
inst.constraint
},
"installed \{name}",
))
}
}
if !has_explicit {
for name, inst in ctx.state {
reqs.push((
Some(inst.channel),
name,
if inst.constraint == "" {
"*"
} else {
inst.constraint
},
"installed \{name}",
))
}
}
} else {
for n in names {
if !ctx.state.contains(n) {
raise OrbitError("\{n} is not installed")
let inst = match ctx.state.get(n) {
Some(i) => i
None => raise OrbitError("\{n} is not installed")
}
targets.push(n)
reqs.push((
Some(inst.channel),
n,
if inst.constraint == "" {
"*"
} else {
inst.constraint
},
"installed \{n}",
))
}
}
let p = new_planner(ctx, targets)
for n in targets {
match ctx.state.get(n) {
Some(inst) => visit(p, n, "*", inst.explicit, None)
None => ()
}
let steps = plan_global(ctx, reqs, targets, Map([]))
apply_plan(ctx, steps)
if names.length() == 0 {
autoremove(ctx)
save_state(ctx)
}
apply_plan(ctx, p)
}
///|
fn reverse_deps(ctx : Ctx, name : String) -> Array[String] {
let r : Array[String] = []
for other, p in ctx.state {
if other != name && p.depends.contains(name) {
r.push(other)
if other != name {
let mut uses = false
for dk, _ in p.depends {
if parse_dep_key(dk).1 == name {
uses = true
break
}
}
if uses {
r.push(other)
}
}
}
r
@@ -506,7 +592,8 @@ async fn cmd_rollback(ctx : Ctx, name : String) -> Unit raise OrbitError {
println("\{name}: \{from} -> \{prev.version}")
let missing : Array[String] = []
for dep, c in inst.depends {
match ctx.state.get(dep) {
let (_, dep_name) = parse_dep_key(dep)
match ctx.state.get(dep_name) {
Some(d) =>
if !satisfies_all(d.version, [c]) {
missing.push(dep + "@" + c)