proofs: Lean 4 kernel-checked cache-restore correctness + prefix measure tool
proofs/auto_ckpt.lean: core-only Lean 4 model of the auto-ckpt / GEN 0 RESUME rule, kernel PASS (no sorryAx). Proves restore soundness (s <= lcp => first s tokens match, bit-identical recompute), branch invalidation (divergence at p => lcp <= p), and GEN 0 -> RESUME L. Backs the invariants in review/osservazioni-auto-ckpt.md. examples/prefix_measure.rs: measures the real static prefix from live SQLite sessions via build_prefix_ids (no engine): ~1135 tokens/session, 575 tool_declare, ~589 shared cross-session. README Status documents both + the agreed next engine-side --auto-ckpt step. Suite 162 passed + 3 ignored, clippy/fmt clean.
This commit is contained in:
1 parent
db2dee2d2c
commit
862897fdbc
3 files changed
+280
No files matched your search
@@ -0,0 +1,93 @@
|
||||
/-
|
||||
Auto-checkpoint / GEN 0 RESUME — modello formale kernel-checked (Lean 4 core).
|
||||
Backing formale di review/osservazioni-auto-ckpt.md. Token = Nat, prompt/catena = List Nat.
|
||||
|
||||
Proprietà verificate dal kernel:
|
||||
- lcp_sound : ripristino sound (s ≤ lcp ⇒ i primi s token coincidono) → "logit bit-identici"
|
||||
- lcp_le_of_take_ne: invalidazione oltre il branch point (divergenza a p ⇒ lcp ≤ p)
|
||||
- lcp_append_self : dopo GEN 0 su `pre`, il turno reale `pre ++ new` ha lcp = pre.length
|
||||
- tk_append_len : ripristinare L = pre.length token restituisce esattamente `pre` (RESUME L)
|
||||
-/
|
||||
|
||||
namespace AutoCkpt
|
||||
|
||||
/-- Lunghezza del prefisso comune di due liste di token. -/
|
||||
def lcp : List Nat → List Nat → Nat
|
||||
| [], _ => 0
|
||||
| _, [] => 0
|
||||
| a :: as, b :: bs => if a = b then Nat.succ (lcp as bs) else 0
|
||||
|
||||
/-- Primi n token (take proprio, per restare solo core). -/
|
||||
def tk : Nat → List Nat → List Nat
|
||||
| 0, _ => []
|
||||
| _+1, [] => []
|
||||
| n+1, a :: as => a :: tk n as
|
||||
|
||||
/-- RESTORE SOUNDNESS: ogni checkpoint con profondità s ≤ lcp ripristina esattamente
|
||||
i primi s token del nuovo prompt. È il cuore del "zero approssimazioni, logit
|
||||
bit-identici al prefill da zero": ricalcolare solo il delta è corretto. -/
|
||||
theorem lcp_sound : ∀ (P C : List Nat) (s : Nat), s ≤ lcp P C → tk s P = tk s C := by
|
||||
intro P
|
||||
induction P with
|
||||
| nil =>
|
||||
intro C s hs
|
||||
have h0 : lcp ([] : List Nat) C = 0 := rfl
|
||||
have hs0 : s = 0 := by omega
|
||||
subst hs0; rfl
|
||||
| cons a as ih =>
|
||||
intro C s hs
|
||||
cases C with
|
||||
| nil =>
|
||||
have h0 : lcp (a :: as) ([] : List Nat) = 0 := rfl
|
||||
have hs0 : s = 0 := by omega
|
||||
subst hs0; rfl
|
||||
| cons b bs =>
|
||||
by_cases hab : a = b
|
||||
· have hlcp : lcp (a :: as) (b :: bs) = Nat.succ (lcp as bs) := by simp [lcp, hab]
|
||||
cases s with
|
||||
| zero => rfl
|
||||
| succ k =>
|
||||
have hk : k ≤ lcp as bs := by omega
|
||||
have ihk := ih bs k hk
|
||||
simp [tk, hab, ihk]
|
||||
· have hlcp : lcp (a :: as) (b :: bs) = 0 := by simp [lcp, hab]
|
||||
have hs0 : s = 0 := by omega
|
||||
subst hs0; rfl
|
||||
|
||||
/-- INVALIDAZIONE: se i primi p+1 token divergono, lcp ≤ p. Quindi ogni checkpoint
|
||||
con seq_len > p fallisce la guardia s ≤ lcp e non viene selezionato: una scrittura
|
||||
a pos p invalida automaticamente gli snapshot oltre il branch point. -/
|
||||
theorem lcp_le_of_take_ne (P C : List Nat) (p : Nat)
|
||||
(h : tk (p+1) P ≠ tk (p+1) C) : lcp P C ≤ p := by
|
||||
by_cases hle : lcp P C ≤ p
|
||||
· exact hle
|
||||
· have hs : p+1 ≤ lcp P C := by omega
|
||||
exact absurd (lcp_sound P C (p+1) hs) h
|
||||
|
||||
/-- Dopo GEN 0 su `pre` (catena = pre), il turno reale `pre ++ new` condivide
|
||||
esattamente pre.length token: la guardia min(lcp, prompt_len-1) ammette L. -/
|
||||
theorem lcp_append_self : ∀ (pre new : List Nat), lcp (pre ++ new) pre = pre.length := by
|
||||
intro pre
|
||||
induction pre with
|
||||
| nil => intro new; cases new <;> rfl
|
||||
| cons a as ih => intro new; simp [lcp, ih, List.cons_append]
|
||||
|
||||
/-- Ripristinare L = pre.length token dal turno reale restituisce esattamente il
|
||||
prefisso pre-riscaldato `pre`: RESUME L riutilizza tutto il prefisso GEN 0. -/
|
||||
theorem tk_append_len : ∀ (pre new : List Nat), tk pre.length (pre ++ new) = pre := by
|
||||
intro pre
|
||||
induction pre with
|
||||
| nil => intro new; rfl
|
||||
| cons a as ih => intro new; simp [tk, ih, List.cons_append]
|
||||
|
||||
/-- Capstone GEN 0 → RESUME L: lcp = L e il ripristino di L token è esattamente `pre`. -/
|
||||
theorem gen0_resume_full (pre new : List Nat) :
|
||||
lcp (pre ++ new) pre = pre.length ∧ tk pre.length (pre ++ new) = pre :=
|
||||
⟨lcp_append_self pre new, tk_append_len pre new⟩
|
||||
|
||||
end AutoCkpt
|
||||
|
||||
-- Audit kernel: nessun `sorryAx` = nessuna prova ammessa/non verificata.
|
||||
#print axioms AutoCkpt.lcp_sound
|
||||
#print axioms AutoCkpt.lcp_le_of_take_ne
|
||||
#print axioms AutoCkpt.gen0_resume_full
|
||||
Reference in new issue
Block a user