Ship oracle journal values, bytecode/frontend hardening, and replay-policy Lean.
Host exposes oracle journals as language values with refuse/copy gates; bytecode and foreign session paths get modularized; applications and verification replay-policy sources land with Lake builds ignored. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
e3d9021bb6
commit
8f1010f967
101 files changed
+17962
-1727
No files matched your search
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"policy_sha256": "27b416847d06b130a066e0eb967a9b30c678e953c51d2ebe2bed9a8444ee0238",
|
||||
"source_sha256": {
|
||||
"ReplayPolicy/Cursor.lean": "cbc4c7d6769824148f3612e3fd3c23c0180d87b848fdec045ae2eeef08379048",
|
||||
"Main.lean": "a133dd67e11d0fcd785badfcecb158937aaf484389e0d428af702fa0a4221e4a",
|
||||
"ReplayPolicy.lean": "bd90ab6aed058d8983e7bbf8957d6f809930a11465fff3eac06d3ed257fa1213",
|
||||
"lakefile.lean": "72f74dda45a2acb8c50266d642ceb48bf07669d7d79f73de113aa50d5cb62072",
|
||||
"lean-toolchain": "ca5f1f601d34c49079e38d7e7041eb0f2a9c227fbc1df0f404820069a052330c",
|
||||
"build_policy.py": "3bfb734e848a3aa4cc5fb9592dd86882ec0fd24f6134bceeae5c20aae1ef2fc3"
|
||||
},
|
||||
"binary_sha256": "06843ebb5fc4ae35491b9cc125cb7053122a2b99970555b72cd23eec8129ec8b"
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import Lean
|
||||
import ReplayPolicy.Cursor
|
||||
import ReplayPolicy.Identity
|
||||
|
||||
/-! JSON-lines ABI for the executable proved policy. JSON transport is an
|
||||
explicit adapter; all validation decisions call the proved functions. -/
|
||||
open Lean
|
||||
open Marqdo.ReplayCursor
|
||||
|
||||
structure StepWire where
|
||||
q : String
|
||||
r : String
|
||||
deriving FromJson
|
||||
structure PolicyCommand where
|
||||
op : String
|
||||
steps : List StepWire := []
|
||||
value : String := ""
|
||||
deriving FromJson
|
||||
|
||||
def reasonName : Reason → String
|
||||
| .missing => "missing"
|
||||
| .request => "request"
|
||||
| .reply => "reply"
|
||||
| .surplus => "surplus"
|
||||
| .phase => "phase"
|
||||
|
||||
def report (out : IO.FS.Stream) (c : Cursor String String) (allow : Bool)
|
||||
(reason : Option Reason := none) : IO Unit := do
|
||||
let why := match reason,c.phase with
|
||||
| some r,_ => some r
|
||||
| none,.stopped r => some r
|
||||
| _,_ => none
|
||||
let json := Json.mkObj [
|
||||
("policy",toJson PolicyIdentity.digest), ("allow",toJson allow), ("matched",toJson c.matched),
|
||||
("remaining",toJson c.remaining.length),
|
||||
("reason",toJson (why.map reasonName))]
|
||||
out.putStrLn json.compress
|
||||
out.flush
|
||||
|
||||
def readCommand (input : IO.FS.Stream) : IO (Option PolicyCommand) := do
|
||||
let line ← input.getLine
|
||||
if line.isEmpty then return none
|
||||
let json ← IO.ofExcept (Json.parse line)
|
||||
return some (← IO.ofExcept (fromJson? json))
|
||||
|
||||
partial def loop (input out : IO.FS.Stream) (c : Cursor String String) : IO Unit := do
|
||||
let some command ← readCommand input | return
|
||||
match command.op with
|
||||
| "prepare" =>
|
||||
let (next,allowed) := prepare c command.value
|
||||
report out next allowed
|
||||
loop input out next
|
||||
| "receive" =>
|
||||
let (next,accepted) := receive c command.value
|
||||
report out next accepted
|
||||
loop input out next
|
||||
| "finish" =>
|
||||
let why := finish c
|
||||
report out c why.isNone why
|
||||
| _ => throw (IO.userError "replay policy: unknown command")
|
||||
|
||||
def main : IO Unit := do
|
||||
let input ← IO.getStdin
|
||||
let out ← IO.getStdout
|
||||
let some command ← readCommand input | throw (IO.userError "replay policy: missing initialization")
|
||||
unless command.op == "init" do throw (IO.userError "replay policy: expected initialization")
|
||||
let c : Cursor String String := ⟨command.steps.map (fun e => (e.q,e.r)),0,.ready⟩
|
||||
report out c true
|
||||
loop input out c
|
||||
@@ -0,0 +1 @@
|
||||
27b416847d06b130a066e0eb967a9b30c678e953c51d2ebe2bed9a8444ee0238
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace PolicyIdentity
|
||||
def digest : String := "0cec24b295bfa5856fdb45584afaf317e4b20adef2365705458cdb3c3eba221f"
|
||||
end PolicyIdentity
|
||||
@@ -0,0 +1,30 @@
|
||||
# Executable replay policy
|
||||
|
||||
Strict protocol replay uses this Lean policy to decide request permissions,
|
||||
reply acceptance and final-history exhaustion. The Rust transport adapter does
|
||||
not contain a second implementation of these decisions. Ordinary recording
|
||||
does not start a policy process.
|
||||
|
||||
Build the companion before building or testing strict replay:
|
||||
|
||||
```sh
|
||||
python3 verification/replay-policy/build_policy.py --lake /path/to/lake
|
||||
cargo build --locked
|
||||
```
|
||||
|
||||
The toolchain is pinned in `lean-toolchain`. The build writes `POLICY-SHA256`,
|
||||
the compiled executable and `BUILD-IDENTITY.json`. Compile Rust after generating
|
||||
the identity: its adapter rejects a companion with a different policy identity.
|
||||
Do not modify policy sources during a build.
|
||||
|
||||
For deployment, install the companion next to the marqdo executable as
|
||||
`marqdo-replay-policy`. `MARQDO_REPLAY_POLICY` can select a trusted installation.
|
||||
Development builds also find this package's `.lake/build/bin/replayPolicy`.
|
||||
An unavailable, stale or malformed companion stops strict replay; there is no
|
||||
fallback to the historical handwritten Rust policy.
|
||||
|
||||
`ReplayPolicy/Cursor.lean` contains the executable transitions and their basic
|
||||
invariants. The paper proof project imports this module and proves interaction-
|
||||
program correspondence in `MarqdoProof/ReplayCursor.lean`. The compiler, process
|
||||
transport, JSON/string encoding and host dispatch adapter remain explicit trust
|
||||
interfaces. These files do not establish correctness of the full Rust frontend.
|
||||
@@ -0,0 +1 @@
|
||||
import ReplayPolicy.Cursor
|
||||
@@ -0,0 +1,132 @@
|
||||
import Lean
|
||||
|
||||
namespace Marqdo.ReplayCursor
|
||||
|
||||
inductive Reason where
|
||||
| missing | request | reply | surplus | phase
|
||||
deriving DecidableEq, Repr
|
||||
inductive Phase where
|
||||
| ready | pending | stopped (reason : Reason)
|
||||
deriving DecidableEq, Repr
|
||||
structure Cursor (Q R : Type) where
|
||||
remaining : List (Q × R)
|
||||
matched : Nat
|
||||
phase : Phase
|
||||
deriving Repr
|
||||
|
||||
def halt (c : Cursor Q R) (why : Reason) : Cursor Q R :=
|
||||
match c.phase with
|
||||
| .stopped _ => c
|
||||
| _ => {c with phase := .stopped why}
|
||||
|
||||
variable {Q R S V : Type} [DecidableEq Q] [DecidableEq R]
|
||||
|
||||
def prepare (c : Cursor Q R) (request : Q) : Cursor Q R × Bool :=
|
||||
match c.phase with
|
||||
| .stopped _ => (c,false)
|
||||
| .pending => (halt c .phase,false)
|
||||
| .ready => match c.remaining with
|
||||
| [] => (halt c .missing,false)
|
||||
| (wanted,_)::_ => if request = wanted then ({c with phase := .pending},true)
|
||||
else (halt c .request,false)
|
||||
|
||||
def receive (c : Cursor Q R) (actual : R) : Cursor Q R × Bool :=
|
||||
match c.phase with
|
||||
| .stopped _ => (c,false)
|
||||
| .ready => (halt c .phase,false)
|
||||
| .pending => match c.remaining with
|
||||
| [] => (halt c .phase,false)
|
||||
| (_,wanted)::rest =>
|
||||
let consumed := {c with remaining := rest}
|
||||
if actual = wanted then ({consumed with matched := c.matched+1, phase := .ready},true)
|
||||
else (halt consumed .reply,false)
|
||||
|
||||
def finish (c : Cursor Q R) : Option Reason :=
|
||||
match c.phase with
|
||||
| .stopped why => some why
|
||||
| .pending => some .phase
|
||||
| .ready => if c.remaining.isEmpty then none else some .surplus
|
||||
|
||||
def WellFormed (c : Cursor Q R) : Prop := c.phase = .pending → c.remaining ≠ []
|
||||
|
||||
omit [DecidableEq R] in
|
||||
theorem prepare_wellFormed (c : Cursor Q R) (q : Q) : WellFormed (prepare c q).1 := by
|
||||
cases hp : c.phase with
|
||||
| stopped why => simp [prepare,hp,WellFormed]
|
||||
| pending => simp [prepare,hp,halt,WellFormed]
|
||||
| ready =>
|
||||
cases hs : c.remaining with
|
||||
| nil => simp [prepare,hp,hs,halt,WellFormed]
|
||||
| cons event rest =>
|
||||
rcases event with ⟨wanted,reply⟩
|
||||
by_cases h : q = wanted <;> simp [prepare,hp,hs,h,halt,WellFormed]
|
||||
|
||||
omit [DecidableEq Q] in
|
||||
theorem receive_wellFormed (c : Cursor Q R) (r : R) : WellFormed (receive c r).1 := by
|
||||
cases hp : c.phase with
|
||||
| stopped why => simp [receive,hp,WellFormed]
|
||||
| ready => simp [receive,hp,halt,WellFormed]
|
||||
| pending =>
|
||||
cases hs : c.remaining with
|
||||
| nil => simp [receive,hp,hs,halt,WellFormed]
|
||||
| cons event rest =>
|
||||
rcases event with ⟨q,wanted⟩
|
||||
by_cases h : r = wanted <;> simp [receive,hp,hs,h,halt,WellFormed]
|
||||
|
||||
omit [DecidableEq R] in
|
||||
theorem prepare_stopped (events : List (Q × R)) (n : Nat) (why : Reason) (q : Q) :
|
||||
prepare ⟨events,n,.stopped why⟩ q = (⟨events,n,.stopped why⟩,false) := rfl
|
||||
omit [DecidableEq Q] in
|
||||
theorem receive_stopped (events : List (Q × R)) (n : Nat) (why : Reason) (r : R) :
|
||||
receive ⟨events,n,.stopped why⟩ r = (⟨events,n,.stopped why⟩,false) := rfl
|
||||
|
||||
omit [DecidableEq R] in
|
||||
/-- A permit identifies exactly the head request; no assumption on replies. -/
|
||||
theorem permit_has_expected_request (c : Cursor Q R) (q : Q)
|
||||
(granted : (prepare c q).2 = true) :
|
||||
∃ r rest, c.phase = .ready ∧ c.remaining = (q,r)::rest := by
|
||||
cases hp : c.phase with
|
||||
| stopped why => simp [prepare,hp] at granted
|
||||
| pending => simp [prepare,hp] at granted
|
||||
| ready =>
|
||||
cases hs : c.remaining with
|
||||
| nil => simp [prepare,hp,hs] at granted
|
||||
| cons event rest =>
|
||||
rcases event with ⟨wanted,reply⟩
|
||||
by_cases h : q = wanted
|
||||
· subst wanted; exact ⟨reply,rest,rfl,rfl⟩
|
||||
· simp [prepare,hp,hs,h] at granted
|
||||
|
||||
|
||||
omit [DecidableEq Q] [DecidableEq R] in
|
||||
theorem finish_ready (events : List (Q × R)) (n : Nat) :
|
||||
finish (Cursor.mk events n .ready) = none ↔ events = [] := by
|
||||
cases events <;> simp [finish]
|
||||
|
||||
omit [DecidableEq Q] in
|
||||
theorem matching_reply_consumes_one (q : Q) (r : R) (rest : List (Q × R)) (n : Nat) :
|
||||
receive (Cursor.mk ((q,r)::rest) n .pending) r =
|
||||
(Cursor.mk rest (n+1) .ready,true) := by
|
||||
simp [receive]
|
||||
|
||||
omit [DecidableEq Q] in
|
||||
theorem mismatching_reply_retains_match_count (q : Q) (wanted actual : R)
|
||||
(rest : List (Q × R)) (n : Nat) (different : actual ≠ wanted) :
|
||||
receive (Cursor.mk ((q,wanted)::rest) n .pending) actual =
|
||||
(Cursor.mk rest n (.stopped .reply),false) := by
|
||||
simp [receive,different,halt]
|
||||
|
||||
omit [DecidableEq R] in
|
||||
theorem prepare_preserves_history (c : Cursor Q R) (q : Q) :
|
||||
(prepare c q).1.remaining = c.remaining ∧ (prepare c q).1.matched = c.matched := by
|
||||
cases hp : c.phase with
|
||||
| stopped why => simp [prepare,hp]
|
||||
| pending => simp [prepare,hp,halt]
|
||||
| ready =>
|
||||
cases hs : c.remaining with
|
||||
| nil => simp [prepare,hp,hs,halt]
|
||||
| cons event rest =>
|
||||
rcases event with ⟨wanted,r⟩
|
||||
by_cases h : q = wanted <;> simp [prepare,hp,hs,h,halt]
|
||||
|
||||
end Marqdo.ReplayCursor
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace PolicyIdentity
|
||||
def digest : String := "27b416847d06b130a066e0eb967a9b30c678e953c51d2ebe2bed9a8444ee0238"
|
||||
end PolicyIdentity
|
||||
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Build the executable proved policy and bind its source identity to the ABI."""
|
||||
from pathlib import Path
|
||||
import argparse,hashlib,json,shutil,subprocess
|
||||
ROOT=Path(__file__).resolve().parent
|
||||
p=argparse.ArgumentParser();p.add_argument('--lake',default=shutil.which('lake'));a=p.parse_args()
|
||||
if not a.lake:raise SystemExit('lake is required to build the verified replay policy')
|
||||
files=['ReplayPolicy/Cursor.lean','Main.lean','ReplayPolicy.lean','lakefile.lean','lean-toolchain','build_policy.py']
|
||||
hashes={name:hashlib.sha256((ROOT/name).read_bytes()).hexdigest() for name in files}
|
||||
digest=hashlib.sha256(json.dumps(hashes,sort_keys=True,separators=(',',':')).encode()).hexdigest()
|
||||
(ROOT/'POLICY-SHA256').write_text(digest+'\n')
|
||||
(ROOT/'ReplayPolicy/Identity.lean').write_text('namespace PolicyIdentity\ndef digest : String := "'+digest+'"\nend PolicyIdentity\n')
|
||||
result=subprocess.run([a.lake,'build','replayPolicy'],cwd=ROOT)
|
||||
if result.returncode:raise SystemExit(result.returncode)
|
||||
changed=[name for name,digest_before in hashes.items()
|
||||
if hashlib.sha256((ROOT/name).read_bytes()).hexdigest()!=digest_before]
|
||||
if changed:raise SystemExit('policy sources changed during build: '+', '.join(changed))
|
||||
binary=ROOT/'.lake/build/bin/replayPolicy'
|
||||
(ROOT/'BUILD-IDENTITY.json').write_text(json.dumps(dict(policy_sha256=digest,source_sha256=hashes,binary_sha256=hashlib.sha256(binary.read_bytes()).hexdigest()),indent=2)+'\n')
|
||||
print(binary)
|
||||
@@ -0,0 +1,6 @@
|
||||
{"version": "1.3.0",
|
||||
"packagesDir": ".lake/packages",
|
||||
"packages": [],
|
||||
"name": "replay_policy",
|
||||
"lakeDir": ".lake",
|
||||
"fixedToolchain": false}
|
||||
@@ -0,0 +1,7 @@
|
||||
import Lake
|
||||
open Lake DSL
|
||||
package replay_policy
|
||||
@[default_target]
|
||||
lean_lib ReplayPolicy
|
||||
lean_exe replayPolicy where
|
||||
root := `Main
|
||||
@@ -0,0 +1 @@
|
||||
leanprover/lean4:v4.35.0
|
||||
Reference in new issue
Block a user