Files
marqdo/verification/replay-policy/Main.lean
T
cflmy 9331af78e2 release(2.8.0): 合入 CHANGELOG/README/发版说明/skills prove 任务;CI 链接指向 doc/releases
- CHANGELOG.md:v2.8.0 条目(中英双语);
- README.md:版本行 2.7.1→2.8.0 + 愿景第 6 条(知识可证明);
- doc/releases/v2.8.0.md:双语发版说明(亮点/用法/已知限制);
- skills/marqdo-dev/index.mq.md:新增 prove 任务(路径/目录模式/
  require_lean/json;参数用 + 行声明升参,check 与 narrative-ratio 通过);
- 设计文档 §12.5 标记已合入;发版链接统一指向 doc/releases/v2.8.0.md。
2026-11-18 10:29:26 +08:00

70 lines
2.2 KiB
Lean4
Executable File

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