Files
yvgude--lean-ctx/lean/LeanCtxProofs/Policy/ContextGovernance.lean
T
wehub-resource-sync 26382a7ac6
CI / Clippy (push) Failing after 15m13s
CI / Test (ubuntu-latest) (push) Failing after 16m1s
CI / Test (macos-latest) (push) Has been cancelled
CI / Test (windows-latest) (push) Has been cancelled
CI / Build (no embeddings / no ORT) (push) Has been cancelled
CI / Format (push) Has been cancelled
CI / Cookbook (Node) (push) Has been cancelled
CI / Pi Extension (Node) (push) Has been cancelled
CI / Rust SDK (lean-ctx-client) (push) Has been cancelled
CI / Embed SDK (lean-ctx-sdk) (push) Has been cancelled
CI / Python SDK (leanctx) (push) Has been cancelled
CI / Hermes Plugin (Python) (push) Has been cancelled
CI / SDK Conformance Matrix (push) Has been cancelled
CI / Coverage (push) Has been cancelled
CI / cargo-deny (push) Has been cancelled
CI / Adversarial Safety (push) Has been cancelled
CI / Benchmarks (push) Has been cancelled
CI / Output-Quality Gate (eval A/B) (push) Has been cancelled
CI / Documentation (push) Has been cancelled
CI / CI Green (push) Has been cancelled
JetBrains Plugin / Actionlint (push) Has been cancelled
CodeQL / Analyze (actions) (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
CodeQL / Analyze (rust) (push) Has been cancelled
JetBrains Plugin / Validation (push) Has been cancelled
JetBrains Plugin / Build (push) Has been cancelled
JetBrains Plugin / Test (push) Has been cancelled
Security Check / Security Scan (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:35:30 +08:00

97 lines
3.4 KiB
Lean4

/-
LeanCTX Formal Verification — Context Governance Proofs
Proves critical invariants of the context policy engine:
1. excluded_items_never_rendered
2. pinned_items_always_preserved
3. setView preserves state
Mirrors: rust/src/core/context_policies.rs, context_field.rs
Methodology: Verification-Guided Development (arXiv:2407.01688)
-/
import LeanCtxProofs.Basic
namespace LeanCtxProofs.Policy.ContextGovernance
open ContextState PolicyAction
def applyAction (action : PolicyAction) (current : ContextState) (tokenCount : Nat) :
ContextState :=
match action with
| .exclude => .excluded
| .pin => .pinned
| .include =>
match current with
| .candidate => .included
| other => other
| .markOutdated => .stale
| .maxTokens limit =>
if tokenCount > limit then .excluded else current
| .setView _ => current
def isRenderable (s : ContextState) : Bool :=
match s with
| .excluded => false
| .shadowed => false
| _ => true
def compileContext (items : List ContextItem) : CompiledContext :=
items.filter fun item => isRenderable item.state
-- ============================================================================
-- Core Safety Theorems
-- ============================================================================
theorem excluded_items_never_rendered (items : List ContextItem) (item : ContextItem)
(h_excl : item.state = ContextState.excluded) :
item (compileContext items).items := by
unfold compileContext
simp [List.mem_filter]
intro _
simp [isRenderable, h_excl]
theorem pinned_items_always_preserved (items : List ContextItem) (item : ContextItem)
(h_pin : item.state = ContextState.pinned)
(h_mem : item items) :
item (compileContext items).items := by
unfold compileContext
simp [List.mem_filter]
exact h_mem, by simp [isRenderable, h_pin]
theorem included_items_preserved (items : List ContextItem) (item : ContextItem)
(h_incl : item.state = ContextState.included)
(h_mem : item items) :
item (compileContext items).items := by
unfold compileContext
simp [List.mem_filter]
exact h_mem, by simp [isRenderable, h_incl]
theorem exclude_action_always_excludes (state : ContextState) (tokenCount : Nat) :
applyAction .exclude state tokenCount = .excluded := rfl
theorem pin_action_always_pins (state : ContextState) (tokenCount : Nat) :
applyAction .pin state tokenCount = .pinned := rfl
theorem set_view_preserves_state (view : String) (state : ContextState) (tokenCount : Nat) :
applyAction (.setView view) state tokenCount = state := rfl
theorem shadowed_items_never_rendered (items : List ContextItem) (item : ContextItem)
(h_shadow : item.state = ContextState.shadowed) :
item (compileContext items).items := by
unfold compileContext
simp [List.mem_filter]
intro _
simp [isRenderable, h_shadow]
theorem candidate_is_renderable : isRenderable ContextState.candidate = true := rfl
theorem stale_is_renderable : isRenderable ContextState.stale = true := rfl
/-- End-to-end: exclude action + compilation = item not in output. -/
theorem exclude_then_compile_removes (items : List ContextItem) (idx : Nat)
(h_idx : idx < items.length)
(h_excl : (items[idx]).state = ContextState.excluded) :
items[idx] (compileContext items).items :=
excluded_items_never_rendered items (items[idx]) h_excl
end LeanCtxProofs.Policy.ContextGovernance