interplane.Get Omega
Read in your language

aien-architecture-mv

Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.

Brought by drakestapleton

branch main Branches Tags History

aien-architecture-mv / bootstrap / formal-oracles / lean / AienSeq / Model.lean

89 lines · 4080 bytes Raw Blame History
1/-!
2# Abstract model of the Rust generational sequence arena (DEVELOPMENT_ORACLE)
3
4Source modelled: aien-dev/sovereign-core `crates/aien-runtime/src/sequence.rs` at dd1fe32
5(`SequenceArena::{new, allocate, fork, get, free, active_count}`, `SequenceId`).
6The scheduler arena `crates/aien-scheduler/src/sequence.rs` differs (it retires a slot instead of
7wrapping); its free rule is modelled separately as `retireNext` in this file.
8
9Invariant: AIEN.INV.SEQUENCE.STALE_ID.V1 (formal/invariants/sequence-stale-id-v1.toml).
10
11Abstractions (each is an ASSUMPTION, not a proof):
12* The arena is a total function from slot number to a slot, plus a capacity `cap`; slots at or
13 beyond `cap` are never consulted (Rust `slots.get(i)` returns None there).
14* Record contents (priority, world id, ...) are not modelled. Only presence (`occ`) matters here.
15* `u32` generation arithmetic IS modelled: generations live in [1, 2^32 - 1] (`genMax`).
16* `free_slots` is a stack (head = next pop). Rust pops the back of a reversed Vec, the same order.
17* Single scheduler thread (the arena has no locks, by its own doc comment). No concurrency model.
18-/
19namespace AienSeq
20
21/-- 2^32 (the generation field is a `u32`). -/
22def two32 : Nat := 4294967296
23/-- Largest generation, u32::MAX. -/
24def genMax : Nat := 4294967295
25
26structure Id where
27 slot : Nat
28 gen : Nat
29 deriving DecidableEq, Repr
30
31/-- Rust: `slot.generation.wrapping_add(1).max(1)` on a `u32`. -/
32def nextGen (g : Nat) : Nat := max ((g + 1) % two32) 1
33
34structure Arena where
35 gen : Nat → Nat -- generation counter per slot
36 occ : Nat → Bool -- slot holds a record
37 free : List Nat -- free_slots, head is popped next
38 active : Nat -- active_count
39 cap : Nat -- capacity
40
41def upd {α : Type} (f : Nat → α) (i : Nat) (v : α) : Nat → α :=
42 fun j => if j = i then v else f j
43
44/-- Rust `SequenceArena::new(capacity)`: every generation 1, nothing occupied, all slots free. -/
45def Arena.init (cap : Nat) : Arena :=
46 { gen := fun _ => 1, occ := fun _ => false, free := List.range cap, active := 0, cap := cap }
47
48/-- Rust `get`: the record behind an id, if the slot exists, the generation matches and a record is present. -/
49def Arena.live (a : Arena) (id : Id) : Bool :=
50 decide (id.slot < a.cap) && decide (a.gen id.slot = id.gen) && a.occ id.slot
51
52/-- Rust `allocate`: pop a free slot, hand out (slot, current generation). `none` = capacity exhausted. -/
53def Arena.alloc (a : Arena) : Option (Arena × Id) :=
54 match a.free with
55 | [] => none
56 | s :: rest =>
57 some ({ a with occ := upd a.occ s true, free := rest, active := a.active + 1 },
58 { slot := s, gen := a.gen s })
59
60/-- Rust `fork`: refused when the parent id does not resolve, else allocate. -/
61def Arena.fork (a : Arena) (parent : Id) : Option (Arena × Id) :=
62 if a.live parent then a.alloc else none
63
64/-- Rust `free`: succeeds only for a live id; bumps the generation, returns the slot to the free stack. -/
65def Arena.release (a : Arena) (id : Id) : Arena × Bool :=
66 if a.live id then
67 ({ a with occ := upd a.occ id.slot false, gen := upd a.gen id.slot (nextGen (a.gen id.slot)),
68 free := id.slot :: a.free, active := a.active - 1 }, true)
69 else (a, false)
70
71/-! ### Scheduler arena variant: retire the slot at u32::MAX instead of wrapping -/
72
73/-- `aien-scheduler` `free_sequence`: at `u32::MAX` the slot is retired (never reused); else generation + 1. -/
74def retireNext (g : Nat) : Option Nat := if g = genMax then none else some (g + 1)
75
76/-- Number of occupied slots below `n`. -/
77def occCount (occ : Nat → Bool) : Nat → Nat
78 | 0 => 0
79 | n + 1 => occCount occ n + (if occ n then 1 else 0)
80
81/-- Well-formedness: what `new` establishes and every operation must keep. -/
82structure Arena.WF (a : Arena) : Prop where
83 freeRange : ∀ i ∈ a.free, i < a.cap
84 freeNodup : a.free.Nodup
85 freeIff : ∀ i, i < a.cap → (i ∈ a.free ↔ a.occ i = false)
86 genRange : ∀ i, 1 ≤ a.gen i ∧ a.gen i < two32
87 activeEq : a.active = occCount a.occ a.cap
88
89end AienSeq