Something went wrong. Try again.
Coding agent with a memory of all things
Something went wrong. Try again.
7.8 kB 333 lines
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334# kernel_local: the kernel setup, the session's tools while the kernel is# missing, and the bootstrap lock's leftovers# (crates/eukhe-core/src/kernel/bootstrap: setup.rs, venv.rs, mod.rs,# dir_lock.rs). The setup's plan, the sweep's verdict on each leftover and# the lock attempt's verdict run as this code (entry# bend/ffi_kernel_local.bend); laws/kernel_local.bend states the laws;# proof/kernel_local/ proves them.
import Base
# Part 1: the setup# -----------------
# what the setup may fetch: a session (Local) or `eukhe kernel install`type Setup is Data: Local{} Install{}
# the kernel venv the setup findstype Found is Data: Current{} Skills{} Stale{}
# one action of a setup, in ordertype Act is Data: # a uv command that may download; hashed: uv checks each file it gets # against a pinned hash before it installs the file Fetch{hashed: Bool} # a uv command that uses local files only (--offline) Offline{} # uv pip check of the built venv Check{} # the kernel starts Run{}
# a build: the Python install, the venv, the hash-locked requirements, the# runtime from its local directory, then the checkdef build(s: Setup) -> List<&2, Act>: match s: case Local{}: [Offline{}, Offline{}, Offline{}, Offline{}, Check{}, Run{}] case Install{}: [Fetch{True{}}, Offline{}, Fetch{True{}}, Offline{}, Check{}, Run{}]
# the actions of a setup that finds fdef plan(f: Found, s: Setup) -> List<&2, Act>: match f: case Current{}: [Run{}] case Skills{}: [Offline{}, Run{}] case Stale{}: build(s)
# does an action of the list fetchdef acts.fetching(xs: List<&2, Act>) -> Bool: match xs: case Nil{}: False{} case Fetch{h} <> rest: True{} case Offline{} <> rest: acts.fetching(rest) case Check{} <> rest: acts.fetching(rest) case Run{} <> rest: acts.fetching(rest)
# does the kernel startdef acts.runs(xs: List<&2, Act>) -> Bool: match xs: case Nil{}: False{} case Fetch{h} <> rest: acts.runs(rest) case Offline{} <> rest: acts.runs(rest) case Check{} <> rest: acts.runs(rest) case Run{} <> rest: True{}
# every fetch is hashed, and the kernel starts only when a check came# after the last fetch (clean: no fetch since the last check)def acts.verified(xs: List<&2, Act>, +clean: Bool) -> Bool: match xs: case Nil{}: True{} case Fetch{h} <> rest: Bool.and(h, acts.verified(rest, False{})) case Offline{} <> rest: acts.verified(rest, clean) case Check{} <> rest: acts.verified(rest, True{}) case Run{} <> rest: Bool.and(clean, acts.verified(rest, clean))
# Part 2: the session's tools# ---------------------------
type Kernel is Data: Up{} # the setup failed: the error names `eukhe kernel install` Missing{}
# the kernel after a setup that finds f; ok: the sync or the build workeddef start(f: Found, ok: Bool) -> Kernel: match f: case Current{}: Up{} case Skills{}: match ok: case True{}: Up{} case False{}: Missing{} case Stale{}: match ok: case True{}: Up{} case False{}: Missing{}
# a tool call: the ipython tool, or any other tool (by its number)type Tool is Data: Python{} Other{n: Nat}
type Answer is Data: # the tool ran Ran{} # the tool answered with the install command NamesInstall{}
def answer(t: Tool, k: Kernel) -> Answer: match t: case Python{}: match k: case Up{}: Ran{} case Missing{}: NamesInstall{} case Other{n}: Ran{}
# Part 3: the bootstrap lock's leftovers# --------------------------------------
type Owner is Data: Alive{} Dead{}
# a file beside the lock: an attempt's candidate, a stale lock renamed# aside, or another filetype FileKind is Data: Cand{} Aside{} Foreign{}
type Left is Data: Left{kind: FileKind, owner: Owner}
# the lock an attempt finds: none, held by a live owner, or by a dead onetype Lock is Data: Free{} Live{} Gone{}
# how an attempt ends; Crash: its process died inside ittype End is Data: Got{} Busy{} Freed{} Crash{}
def attempt(l: Lock) -> End: match l: case Free{}: Got{} case Live{}: Busy{} case Gone{}: Freed{}
# what an attempt sees once its candidate is written: the candidate was# linked to the lock path (the lock is its own), the lock vanished before# it was read, the lock names an owner (alive or not), or the lock names# none (a lock left half written, or a legacy dir lock without a pid# file): then its age in ms, None when its mtime cannot be read or lies in# the futuretype Seen is Data: Linked{} Vanished{} Owner{alive: Bool} NoOwner{age: Maybe<&2, Nat>}
# a lock that names no owner is stale only past this age (ms): its writer# had that long to finish writing the piddef stale_ms() -> Nat: 30000n
def lock.past(old: Bool) -> Lock: match old: case True{}: Gone{} case False{}: Live{}
def lock.age(m: Maybe<&2, Nat>) -> Lock: match m: case None{}: Live{} case Some{a}: lock.past(Nat.is_lt(stale_ms(), a))
# the lock an attempt finds, from what it seesdef lock.of(s: Seen) -> Lock: match s: case Linked{}: Free{} case Vanished{}: Gone{} case Owner{alive}: match alive: case True{}: Live{} case False{}: Gone{} case NoOwner{age}: lock.age(age)
# the attempt's own candidates left after it endsdef own_left(e: End) -> Nat: match e: case Got{}: 0n case Busy{}: 0n case Freed{}: 0n case Crash{}: 1n
# does the sweep keep the file: a live attempt's leftover or another filedef keep(l: Left) -> Bool: match l: case Left{Cand{}, Alive{}}: True{} case Left{Cand{}, Dead{}}: False{} case Left{Aside{}, Alive{}}: True{} case Left{Aside{}, Dead{}}: False{} case Left{Foreign{}, o}: True{}
def keep.cons(b: Bool, l: Left, xs: List<&2, Left>) -> List<&2, Left>: match b: case True{}: l <> xs case False{}: xs
def sweep(ls: List<&2, Left>) -> List<&2, Left>: match ls: case Nil{}: Nil{} case +l <> rest: keep.cons(keep(l), l, sweep(rest))
# 1 for a leftover of a dead attemptdef dead(l: Left) -> Nat: match l: case Left{Cand{}, Alive{}}: 0n case Left{Cand{}, Dead{}}: 1n case Left{Aside{}, Alive{}}: 0n case Left{Aside{}, Dead{}}: 1n case Left{Foreign{}, o}: 0n
# the leftovers of dead attemptsdef lefts.dead(ls: List<&2, Left>) -> Nat: match ls: case Nil{}: 0n case +l <> rest: Nat.add(dead(l), lefts.dead(rest))
# 1 for a file the sweep must keepdef live(l: Left) -> Nat: match l: case Left{Cand{}, Alive{}}: 1n case Left{Cand{}, Dead{}}: 0n case Left{Aside{}, Alive{}}: 1n case Left{Aside{}, Dead{}}: 0n case Left{Foreign{}, o}: 1n
# the files the sweep must keepdef lefts.live(ls: List<&2, Left>) -> Nat: match ls: case Nil{}: 0n case +l <> rest: Nat.add(live(l), lefts.live(rest))
# Requests# --------
# what Rust asks: the plan of a setup that finds f, the sweep's verdict# on each leftover beside the lock, the verdict of a lock attempttype Ask is Data: AskPlan{found: Found, setup: Setup} AskSweep{files: List<&2, Left>} AskLock{seen: Seen}
# the sweep's verdict on each file, in order (True: it stays)def keeps(ls: List<&2, Left>) -> List<&2, Bool>: match ls: case Nil{}: Nil{} case +l <> rest: keep(l) <> keeps(rest)