Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-architecture-mv/patches/efd170c349314a4a # aien-architecture-mv Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers ## GitHub aien-dev/aien-architecture-mv#108 (code only) closed derived `efd170c349314a4a` #108 Imported from GitHub aien-dev/aien-architecture-mv#108 (code only) GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed). Base `79ed1319ce2c83bbd90762cfb14badc68b949c09` · Head `f5b97c774cae0b0be5536ac7a8f09f599f66ad26` ### Apply it ``` git checkout -b patch-efd170c349314a4a 79ed1319ce2c83bbd90762cfb14badc68b949c09 curl -fsSL https://interplane.io/drakestapleton/aien-architecture-mv/patches/efd170c349314a4a.patch | git am ``` Raw patch (mbox) · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/aien-architecture-mv 108 f5b97c774cae0b0be5536ac7a8f09f599f66ad26 ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"aien-architecture-mv","number":108,"url":"https://github.com/aien-dev/aien-architecture-mv/pull/108","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:37:41Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/bootstrap/formal-oracles/lean/AienReceipt.lean b/bootstrap/formal-oracles/lean/AienReceipt.lean new file mode 100644 index 0000000..7cea522 --- /dev/null +++ b/bootstrap/formal-oracles/lean/AienReceipt.lean @@ -0,0 +1,4 @@ +import AienReceipt.Model +import AienReceipt.Theorems +import AienReceipt.Layout +import AienReceipt.Corpus diff --git a/bootstrap/formal-oracles/lean/AienReceipt/Corpus.lean b/bootstrap/formal-oracles/lean/AienReceipt/Corpus.lean new file mode 100644 index 0000000..e2e18a1 --- /dev/null +++ b/bootstrap/formal-oracles/lean/AienReceipt/Corpus.lean @@ -0,0 +1,59 @@ +import AienReceipt.Layout +namespace AienReceipt + +/-! Positive and negative corpus. The nonce function is a concrete stand-in; the theorems in +`Theorems.lean` hold for every `nf`, the corpus only shows the rules fire on concrete records. -/ + +def nf0 (v s : Nat) : Nat := v * 1000 + s + +def admittedR : Receipt := + { pres := ⟨false, false, false⟩, decision := .admitted, tier := .seed0bQemu, sequence := 7, + nonce := nf0 5 7, observed := 0, status := .exited, exitCode := 0, rflags := ⟨true, false, true, true, false, true, true, true⟩, stage := 0, reason := 0, syscalls := 3, reads := 1, denials := 0, frames := 4, digests := [1,2,3,4,5,6,7], + verifier := 5, machineId := 0, generation := 0, contextId := 0 } + +def rejectedR : Receipt := + { admittedR with decision := .rejected, status := .notRun, exitCode := 0, rflags := ⟨false, false, true, false, false, false, false, false⟩, stage := 3, reason := 18, syscalls := 0, reads := 0, denials := 0 } + +/-! Positive: each decision has a valid receipt. -/ +theorem pos_admitted_clean : validate nf0 admittedR = true := by decide +theorem pos_rejected_at_stage : validate nf0 rejectedR = true := by decide +theorem pos_loader_reason : validate nf0 { rejectedR with reason := 0x10a } = true := by decide +theorem pos_present_flags : + validate nf0 { admittedR with pres := ⟨true, true, true⟩, machineId := 9, generation := 1, contextId := 2, observed := 99 } = true := by decide +theorem pos_fault_admitted : + validate nf0 { admittedR with status := .fault, rflags := ⟨false, false, true, false, false, false, false, true⟩ } = true := by decide +theorem pos_roundtrip : decode nf0 (encode rejectedR) = some rejectedR := decode_encode (by decide) + +/-! Negative: one receipt per violated rule, each refused. -/ +theorem neg_rejected_but_ran_status : validate nf0 { rejectedR with status := .exited } = false := by decide +theorem neg_rejected_with_exit_status : validate nf0 { rejectedR with exitCode := 1 } = false := by decide +theorem neg_rejected_with_syscalls : validate nf0 { rejectedR with syscalls := 1 } = false := by decide +theorem neg_rejected_with_reads : validate nf0 { rejectedR with reads := 1 } = false := by decide +theorem neg_rejected_with_denials : validate nf0 { rejectedR with denials := 1 } = false := by decide +theorem neg_rejected_with_result_flag : + validate nf0 { rejectedR with rflags := ⟨true, false, true, false, false, false, false, false⟩ } = false := by decide +theorem neg_rejected_without_stage : validate nf0 { rejectedR with stage := 0 } = false := by decide +theorem neg_rejected_without_reason : validate nf0 { rejectedR with reason := 0 } = false := by decide +theorem neg_stage_out_of_range : validate nf0 { rejectedR with stage := 10 } = false := by decide +theorem neg_reason_out_of_range : validate nf0 { rejectedR with reason := 20 } = false := by decide +theorem neg_admitted_with_reason : validate nf0 { admittedR with reason := 18 } = false := by decide +theorem neg_admitted_with_stage : validate nf0 { admittedR with stage := 3 } = false := by decide +theorem neg_canary_after_fault : validate nf0 { admittedR with status := .fault } = false := by decide +theorem neg_canary_nonzero_exit : validate nf0 { admittedR with exitCode := 3 } = false := by decide +theorem neg_absent_machine_nonzero : validate nf0 { admittedR with machineId := 1 } = false := by decide +theorem neg_absent_context_nonzero : validate nf0 { admittedR with contextId := 1 } = false := by decide +theorem neg_absent_generation_nonzero : validate nf0 { admittedR with generation := 1 } = false := by decide +theorem neg_absent_time_nonzero : validate nf0 { admittedR with observed := 1 } = false := by decide +theorem neg_bad_nonce : validate nf0 { admittedR with nonce := 0 } = false := by decide +theorem neg_refused_after_encode : decode nf0 (encode { rejectedR with status := .exited }) = none := + encode_invalid_refused neg_rejected_but_ran_status + +/-! Finding (CanaryFailed): the code accepts these. Recorded, not endorsed. -/ +theorem finding_canary_decision_with_exited_status : + validate nf0 { admittedR with decision := .canaryFailed } = true := by decide +theorem finding_admitted_with_canary_failed_status : + validate nf0 { admittedR with status := .canaryFailed, rflags := ⟨false, false, true, false, false, false, false, true⟩ } = true := by decide +theorem finding_destroyed_with_exited_status : + validate nf0 { admittedR with decision := .destroyed } = true := by decide + +end AienReceipt diff --git a/bootstrap/formal-oracles/lean/AienReceipt/Layout.lean b/bootstrap/formal-oracles/lean/AienReceipt/Layout.lean new file mode 100644 index 0000000..b411321 --- /dev/null +++ b/bootstrap/formal-oracles/lean/AienReceipt/Layout.lean @@ -0,0 +1,176 @@ +import AienReceipt.Model +import AienReceipt.Theorems +namespace AienReceipt + +/-! ## 512-byte layout and encode/decode (Rust `encode`, `decode`) + +`Layout.fields` is the offset/width table of the Rust `encode` (receipt.rs lines 101-137, header +constants lines 14-18). `layoutContiguous` is checked by `decide`: fields tile 0..512 with no gap or +overlap. `Raw` is the record split into those fields. -/ + +/-- (offset, width) for every field, in order. Includes reserved 56..64, signature block 400..500 +and reserved tail 500..512. -/ +def layout : List (Nat × Nat) := + [(0,8),(8,2),(10,2),(12,4),(16,4),(20,2),(22,2),(24,8),(32,16),(48,8),(56,8), + (64,4),(68,4),(72,4),(76,2),(78,2),(80,4),(84,4),(88,4),(92,4), + (96,32),(128,32),(160,32),(192,32),(224,32),(256,32),(288,32),(320,32),(352,32), + (384,8),(392,8),(400,100),(500,12)] + +def contiguousFrom : Nat → List (Nat × Nat) → Option Nat + | n, [] => some n + | n, (o, w) :: rest => if o == n then contiguousFrom (n + w) rest else none + +/-- the fields tile exactly 512 bytes -/ +theorem layout_tiles_512 : contiguousFrom 0 layout = some 512 := by decide + +/-- `RECEIPT_MAGIC = b"AIENRCP\0"` as bytes -/ +def magic : List Nat := [65, 73, 69, 78, 82, 67, 80, 0] +theorem magic_len : magic.length = 8 := rfl + +def receiptVersion : Nat := 0 +def headerSize : Nat := 96 +def recordSize : Nat := 512 + +structure Raw where + magic : List Nat + version : Nat + hdr : Nat + size : Nat + flags : Nat + decision : Nat + tier : Nat + sequence : Nat + nonce : Nat + reserved1 : Nat -- bytes 56..64 + observed : Nat + status : Nat + exitCode : Int + rflags : Nat + stage : Nat + reason : Nat + syscalls : Nat + reads : Nat + denials : Nat + frames : Nat + digests : List Nat + verifier : Nat + machineId : Nat + generation : Nat + contextId : Nat + sigOk : Bool -- signature block all zero, or algorithm id valid (crypto itself assumed) + reserved2 : Nat -- bytes 500..512 + +def Presence.toNat (p : Presence) : Nat := + (if p.machine then 1 else 0) + (if p.context then 2 else 0) + (if p.time then 4 else 0) +def Presence.ofNat (n : Nat) : Presence := + { machine := n % 2 == 1, context := n / 2 % 2 == 1, time := n / 4 % 2 == 1 } + +def ResultFlags.toNat (f : ResultFlags) : Nat := + (if f.readOk then 1 else 0) + (if f.writeDenied then 2 else 0) + (if f.reclaimed then 4 else 0) + + (if f.canaryPassed then 8 else 0) + (if f.forgedDenied then 16 else 0) + + (if f.mappedMatch then 32 else 0) + (if f.executedMatch then 64 else 0) + + (if f.wxSealed then 128 else 0) +def ResultFlags.ofNat (n : Nat) : ResultFlags := + { readOk := n % 2 == 1, writeDenied := n / 2 % 2 == 1, reclaimed := n / 4 % 2 == 1, + canaryPassed := n / 8 % 2 == 1, forgedDenied := n / 16 % 2 == 1, + mappedMatch := n / 32 % 2 == 1, executedMatch := n / 64 % 2 == 1, + wxSealed := n / 128 % 2 == 1 } + +def Decision.toNat : Decision → Nat | .admitted => 1 | .rejected => 2 | .canaryFailed => 3 | .destroyed => 4 +def Decision.ofNat? : Nat → Option Decision + | 1 => some .admitted | 2 => some .rejected | 3 => some .canaryFailed | 4 => some .destroyed + | _ => none +def Tier.toNat : Tier → Nat | .seed0bQemu => 1 | .seed0bMachine1 => 2 +def Tier.ofNat? : Nat → Option Tier | 1 => some .seed0bQemu | 2 => some .seed0bMachine1 | _ => none +def Status.toNat : Status → Nat + | .notRun => 0 | .exited => 1 | .timeout => 2 | .fault => 3 | .badSyscall => 4 + | .resourceOverrun => 5 | .canaryFailed => 6 +def Status.ofNat? : Nat → Option Status + | 0 => some .notRun | 1 => some .exited | 2 => some .timeout | 3 => some .fault + | 4 => some .badSyscall | 5 => some .resourceOverrun | 6 => some .canaryFailed | _ => none + +/-- Rust `encode`: constants for magic, version, header size, record size; reserved bytes zero; +signature block zero (kernel-emitted record). -/ +def encode (r : Receipt) : Raw := + { magic := magic, version := receiptVersion, hdr := headerSize, size := recordSize, + flags := r.pres.toNat, decision := r.decision.toNat, tier := r.tier.toNat, + sequence := r.sequence, nonce := r.nonce, reserved1 := 0, observed := r.observed, + status := r.status.toNat, exitCode := r.exitCode, rflags := r.rflags.toNat, + stage := r.stage, reason := r.reason, syscalls := r.syscalls, reads := r.reads, + denials := r.denials, frames := r.frames, digests := r.digests, verifier := r.verifier, + machineId := r.machineId, generation := r.generation, contextId := r.contextId, + sigOk := true, reserved2 := 0 } + +/-- Rust `decode`: strict layout checks, enum codes, then `validate`. -/ +def decode (nf : Nat → Nat → Nat) (b : Raw) : Option Receipt := + if b.magic = magic ∧ b.version = receiptVersion ∧ b.hdr = headerSize ∧ b.size = recordSize ∧ + b.flags < 8 ∧ b.rflags < 256 ∧ b.reserved1 = 0 ∧ b.reserved2 = 0 ∧ b.sigOk = true then + match Decision.ofNat? b.decision, Tier.ofNat? b.tier, Status.ofNat? b.status with + | some d, some t, some s => + let r : Receipt := + { pres := Presence.ofNat b.flags, decision := d, tier := t, sequence := b.sequence, + nonce := b.nonce, observed := b.observed, status := s, exitCode := b.exitCode, + rflags := ResultFlags.ofNat b.rflags, stage := b.stage, reason := b.reason, + syscalls := b.syscalls, reads := b.reads, denials := b.denials, frames := b.frames, + digests := b.digests, verifier := b.verifier, machineId := b.machineId, + generation := b.generation, contextId := b.contextId } + if validate nf r = true then some r else none + | _, _, _ => none + else none + +theorem presence_roundtrip (p : Presence) : Presence.ofNat p.toNat = p := by + cases p with | mk a b c => cases a <;> cases b <;> cases c <;> decide +theorem presence_lt (p : Presence) : p.toNat < 8 := by + cases p with | mk a b c => cases a <;> cases b <;> cases c <;> decide +theorem rflags_roundtrip (f : ResultFlags) : ResultFlags.ofNat f.toNat = f := by + cases f with | mk a b c d e g h i => + cases a <;> cases b <;> cases c <;> cases d <;> cases e <;> cases g <;> cases h <;> cases i <;> decide +theorem rflags_lt (f : ResultFlags) : f.toNat < 256 := by + cases f with | mk a b c d e g h i => + cases a <;> cases b <;> cases c <;> cases d <;> cases e <;> cases g <;> cases h <;> cases i <;> decide + +/-- encode then decode returns the receipt exactly when `validate` accepts it, else nothing. +Holds for every nonce function (crypto assumption). -/ +theorem decode_encode_eq (nf : Nat → Nat → Nat) (r : Receipt) : + decode nf (encode r) = if validate nf r = true then some r else none := by + obtain ⟨pres, dec, tier, seq, nonce, obs, st, ex, rf, stage, reason, sc, rd, dn, fr, dg, ver, mid, gen, cid⟩ := r + have hp := presence_roundtrip pres + have hpl := presence_lt pres + have hf := rflags_roundtrip rf + have hfl := rflags_lt rf + simp only [decode, encode, hp, hf, hpl, hfl, receiptVersion, headerSize, recordSize] + cases dec <;> cases tier <;> cases st <;> + simp [Decision.toNat, Decision.ofNat?, Tier.toNat, Tier.ofNat?, Status.toNat, Status.ofNat?] + +/-- encode then decode preserves every valid abstract receipt -/ +theorem decode_encode {nf} {r : Receipt} (h : validate nf r = true) : decode nf (encode r) = some r := by + rw [decode_encode_eq]; simp [h] + +/-- a receipt that `validate` refuses never survives encode then decode -/ +theorem encode_invalid_refused {nf} {r : Receipt} (h : validate nf r = false) : decode nf (encode r) = none := by + rw [decode_encode_eq]; simp [h] + +/-- decode never returns a receipt that `validate` refuses -/ +theorem decode_valid {nf} {b : Raw} {r : Receipt} (h : decode nf b = some r) : validate nf r = true := by + unfold decode at h + split at h + · split at h + · dsimp only at h + split at h + · injection h with h; subst h; assumption + · cases h + · cases h + · cases h + +/-- encode writes the contract constants -/ +theorem encode_constants (r : Receipt) : + (encode r).magic = magic ∧ (encode r).version = 0 ∧ (encode r).hdr = 96 ∧ (encode r).size = 512 := + ⟨rfl, rfl, rfl, rfl⟩ + +/-- a wrong magic or version is refused by decode -/ +theorem decode_bad_magic (nf) (b : Raw) (h : b.magic ≠ magic) : decode nf b = none := by + simp [decode, h] +theorem decode_bad_version (nf) (b : Raw) (h : b.version ≠ 0) : decode nf b = none := by + simp [decode, receiptVersion, h] + +end AienReceipt diff --git a/bootstrap/formal-oracles/lean/AienReceipt/Model.lean b/bootstrap/formal-oracles/lean/AienReceipt/Model.lean new file mode 100644 index 0000000..2218f01 --- /dev/null +++ b/bootstrap/formal-oracles/lean/AienReceipt/Model.lean @@ -0,0 +1,118 @@ +/-! +# Abstract model of the Rust admission receipt (DEVELOPMENT_ORACLE) + +Source modelled: aien-dev/aienos `crates/aienos-artifact/src/receipt.rs` at 2ee61b4 +(`validate`, `decode`, `encode`, `valid_reason`; consumer `build_receipt` in +`crates/aienos-kernel/src/artifact_loader.rs` is read for context only). + +Invariant: AIEN.INV.RECEIPT.REJECTION_CONSISTENCY.V1 (formal/invariants/artifact-receipt-v1.toml). + +Abstractions (each is an ASSUMPTION, not a proof): +* Cryptography. SHA-256 nonce derivation is an arbitrary function `nf`; every theorem holds for + EVERY `nf`. Ed25519 signature checking is not modelled (it lives in `verify`, not `validate`). + The signature block is reduced to one Boolean (`sigOk`: all zero, or well formed algorithm id). +* Digests (32 bytes) are naturals; "all zero" is `= 0`. Fixed-width integer overflow is not modelled. +* Little-endian byte packing is not modelled. `Raw` is the record after splitting into fixed-width + fields at the offsets of `AienReceipt.Layout`. That the byte packing is a bijection per field is assumed. +* `exit_status` is an `Int` (Rust `i32`). +* Presence flags and result flags are structures of Booleans, so unknown bits cannot be written. + `decode` rejects unknown bits on the Raw word first (see `Layout.lean`). +-/ +namespace AienReceipt + +inductive Decision | admitted | rejected | canaryFailed | destroyed + deriving DecidableEq, Repr + +inductive Tier | seed0bQemu | seed0bMachine1 + deriving DecidableEq, Repr + +/-- Rust `ExecutionStatusCode` (offset 64). -/ +inductive Status | notRun | exited | timeout | fault | badSyscall | resourceOverrun | canaryFailed + deriving DecidableEq, Repr + +/-- Presence flags (offset 16): bits 0 machine id, 1 context id, 2 timestamp. -/ +structure Presence where + machine : Bool + context : Bool + time : Bool + deriving DecidableEq, Repr + +/-- Result flags (offset 72): bits 0..7 in the order of the Rust `RESULT_*` constants. -/ +structure ResultFlags where + readOk : Bool + writeDenied : Bool + reclaimed : Bool + canaryPassed : Bool + forgedDenied : Bool + mappedMatch : Bool + executedMatch : Bool + wxSealed : Bool + deriving DecidableEq, Repr + +/-- In-memory receipt. Digest fields that no rule inspects are kept as one opaque list. -/ +structure Receipt where + pres : Presence + decision : Decision + tier : Tier + sequence : Nat + nonce : Nat + observed : Nat + status : Status + exitCode : Int + rflags : ResultFlags + stage : Nat + reason : Nat + syscalls : Nat + reads : Nat + denials : Nat + frames : Nat + digests : List Nat + verifier : Nat + machineId : Nat + generation : Nat + contextId : Nat + deriving DecidableEq, Repr + +/-- Rust `MAX_REJECTION_STAGE`. -/ +def maxStage : Nat := 9 + +/-- Rust `valid_reason`: 1..=19 or 0x101..=0x10a. -/ +def validReason (c : Nat) : Bool := + (decide (1 ≤ c) && decide (c ≤ 19)) || (decide (0x101 ≤ c) && decide (c ≤ 0x10a)) + +/-- `result_flags & !RESULT_RECLAIMED == 0`. -/ +def ResultFlags.onlyReclaimed (f : ResultFlags) : Bool := + !f.readOk && !f.writeDenied && !f.canaryPassed && !f.forgedDenied && + !f.mappedMatch && !f.executedMatch && !f.wxSealed + +/-- Rust lines 322-330: an absent presence flag forces the value to its zero. -/ +def presenceOk (r : Receipt) : Bool := + (r.pres.machine || r.machineId == 0) && + (r.pres.context || (r.generation == 0 && r.contextId == 0)) && + (r.pres.time || r.observed == 0) + +/-- Rust lines 331-335. -/ +def codesOk (r : Receipt) : Bool := + decide (r.stage ≤ maxStage) && (r.reason == 0 || validReason r.reason) + +/-- Rust lines 336-355: the decision match. -/ +def decisionOk (r : Receipt) : Bool := + match r.decision with + | .rejected => + r.stage != 0 && r.reason != 0 && r.status == .notRun && r.exitCode == 0 && + r.syscalls == 0 && r.reads == 0 && r.denials == 0 && r.rflags.onlyReclaimed + | _ => r.stage == 0 && r.reason == 0 + +/-- Rust lines 356-360. -/ +def canaryOk (r : Receipt) : Bool := + !r.rflags.canaryPassed || (r.status == .exited && r.exitCode == 0) + +/-- Rust line 361, with the SHA-256 nonce function `nf` abstracted. -/ +def nonceOk (nf : Nat → Nat → Nat) (r : Receipt) : Bool := + r.nonce == nf r.verifier r.sequence + +/-- Rust `validate`, `Ok` exactly when this is `true`. -/ +def validate (nf : Nat → Nat → Nat) (r : Receipt) : Bool := + presenceOk r && codesOk r && decisionOk r && canaryOk r && nonceOk nf r + +end AienReceipt diff --git a/bootstrap/formal-oracles/lean/AienReceipt/Theorems.lean b/bootstrap/formal-oracles/lean/AienReceipt/Theorems.lean new file mode 100644 index 0000000..6474584 --- /dev/null +++ b/bootstrap/formal-oracles/lean/AienReceipt/Theorems.lean @@ -0,0 +1,125 @@ +import AienReceipt.Model +namespace AienReceipt + +/-! Rules enforced by the code (AIEN.INV.RECEIPT.REJECTION_CONSISTENCY.V1). +Every theorem holds for every nonce function `nf` (crypto is an assumption). -/ + +/-- Unpack `validate = true` into its five groups. -/ +theorem validate_parts {nf : Nat → Nat → Nat} {r : Receipt} (h : validate nf r = true) : + presenceOk r = true ∧ codesOk r = true ∧ decisionOk r = true ∧ canaryOk r = true ∧ + nonceOk nf r = true := by + simp only [validate, Bool.and_eq_true] at h + exact ⟨h.1.1.1.1, h.1.1.1.2, h.1.1.2, h.1.2, h.2⟩ + +/-- the full Rejected arm of `validate`, as propositions -/ +theorem rejected_arm {nf} {r : Receipt} (h : validate nf r = true) (hr : r.decision = .rejected) : + r.stage ≠ 0 ∧ r.reason ≠ 0 ∧ r.status = .notRun ∧ r.exitCode = 0 ∧ r.syscalls = 0 ∧ + r.reads = 0 ∧ r.denials = 0 ∧ r.rflags.onlyReclaimed = true := by + have := (validate_parts h).2.2.1 + simp only [decisionOk, hr, Bool.and_eq_true, bne_iff_ne, ne_eq, beq_iff_eq] at this + obtain ⟨⟨⟨⟨⟨⟨⟨a, b⟩, c⟩, d⟩, e⟩, f⟩, g⟩, i⟩ := this + exact ⟨a, b, c, d, e, f, g, i⟩ + +/-- rejected => execution status NotRun -/ +theorem rejected_not_run {nf} {r : Receipt} (h : validate nf r = true) + (hr : r.decision = .rejected) : r.status = .notRun := + (rejected_arm h hr).2.2.1 + +/-- rejected => no successful execution evidence: not Exited, zero exit status, zero syscalls, +zero object reads, zero denials, and CANARY_PASSED clear (only RECLAIMED may be set). -/ +theorem rejected_no_execution_evidence {nf} {r : Receipt} (h : validate nf r = true) + (hr : r.decision = .rejected) : + r.status ≠ .exited ∧ r.exitCode = 0 ∧ r.syscalls = 0 ∧ r.reads = 0 ∧ r.denials = 0 ∧ + r.rflags.canaryPassed = false := by + obtain ⟨_, _, hs, hx, hc, hr', hd, hf⟩ := rejected_arm h hr + refine ⟨by rw [hs]; decide, hx, hc, hr', hd, ?_⟩ + simp only [ResultFlags.onlyReclaimed, Bool.and_eq_true, Bool.not_eq_true'] at hf + simp [hf] + +/-- rejected => a rejection stage (1..9) and a valid rejection reason are present -/ +theorem rejected_stage_reason_present {nf} {r : Receipt} (h : validate nf r = true) + (hr : r.decision = .rejected) : + (1 ≤ r.stage ∧ r.stage ≤ maxStage) ∧ r.reason ≠ 0 ∧ validReason r.reason = true := by + obtain ⟨hs, hq, _⟩ := rejected_arm h hr + have hc := (validate_parts h).2.1 + simp only [codesOk, Bool.and_eq_true, decide_eq_true_eq, Bool.or_eq_true, beq_iff_eq] at hc + refine ⟨⟨by omega, hc.1⟩, hq, ?_⟩ + rcases hc.2 with h0 | h1 + · exact absurd h0 hq + · exact h1 + +/-- not rejected => the rejection-only fields (stage, reason) are absent -/ +theorem not_rejected_no_rejection_fields {nf} {r : Receipt} (h : validate nf r = true) + (hr : r.decision ≠ .rejected) : r.stage = 0 ∧ r.reason = 0 := by + have := (validate_parts h).2.2.1 + unfold decisionOk at this + cases hd : r.decision <;> simp_all + +/-- CANARY_PASSED => execution succeeded (status Exited, exit status zero) -/ +theorem canary_passed_executed {nf} {r : Receipt} (h : validate nf r = true) + (hc : r.rflags.canaryPassed = true) : r.status = .exited ∧ r.exitCode = 0 := by + have := (validate_parts h).2.2.2.1 + simp [canaryOk, hc] at this + exact this + +/-- absent machine id flag => the machine id digest is zero (not readable as present) -/ +theorem absent_machine_zero {nf} {r : Receipt} (h : validate nf r = true) + (ha : r.pres.machine = false) : r.machineId = 0 := by + have := (validate_parts h).1 + simp [presenceOk, ha] at this + exact this.1.1 + +/-- absent context id flag => generation and context id are zero -/ +theorem absent_context_zero {nf} {r : Receipt} (h : validate nf r = true) + (ha : r.pres.context = false) : r.generation = 0 ∧ r.contextId = 0 := by + have := (validate_parts h).1 + simp [presenceOk, ha] at this + exact this.1.2 + +/-- absent timestamp flag => observed time is zero -/ +theorem absent_time_zero {nf} {r : Receipt} (h : validate nf r = true) + (ha : r.pres.time = false) : r.observed = 0 := by + have := (validate_parts h).1 + simp [presenceOk, ha] at this + exact this.2 + +/-- the nonce is exactly the derived one (for the assumed derivation `nf`) -/ +theorem nonce_derived {nf} {r : Receipt} (h : validate nf r = true) : + r.nonce = nf r.verifier r.sequence := by + have := (validate_parts h).2.2.2.2 + simpa [nonceOk] using this + +/-! ## Finding: CanaryFailed decision is not tied to the CanaryFailed status + +`validate` has one arm for `Rejected` and one arm `_` for every other decision (Rust lines 336-354). +The `_` arm checks only stage = 0 and reason = 0. Nothing relates `ReceiptDecision::CanaryFailed` +(or `Destroyed`) to `ExecutionStatusCode::CanaryFailed`. The two theorems below state the +consequence exactly: for decisions other than Rejected, validity does not depend on which of the +three non-rejected decisions is recorded. -/ + +/-- swapping among the non-rejected decisions never changes validity -/ +theorem validate_decision_irrelevant_when_not_rejected (nf : Nat → Nat → Nat) (r : Receipt) + (d : Decision) (h0 : r.decision ≠ .rejected) (h1 : d ≠ .rejected) : + validate nf { r with decision := d } = validate nf r := by + have key : decisionOk { r with decision := d } = decisionOk r := by + unfold decisionOk + cases hd : r.decision <;> cases d <;> simp_all + simp [validate, presenceOk, codesOk, canaryOk, nonceOk, key] + +/-- a valid receipt with decision CanaryFailed need not have status CanaryFailed: +validity does not force the status either way (it only constrains Rejected and CANARY_PASSED). -/ +theorem canary_decision_status_unconstrained (nf : Nat → Nat → Nat) (r : Receipt) + (h : validate nf r = true) (hd : r.decision = .canaryFailed) (s : Status) + (hs : r.rflags.canaryPassed = false) : + validate nf { r with status := s } = true := by + have hp := validate_parts h + simp only [validate, Bool.and_eq_true] + refine ⟨⟨⟨⟨?_, ?_⟩, ?_⟩, ?_⟩, ?_⟩ + · exact hp.1 + · exact hp.2.1 + · have := hp.2.2.1 + simpa [decisionOk, hd] using this + · simp [canaryOk, hs] + · exact hp.2.2.2.2 + +end AienReceipt diff --git a/bootstrap/formal-oracles/lean/DISCREPANCIES.md b/bootstrap/formal-oracles/lean/DISCREPANCIES.md index 3f85b45..ad9f41c 100644 --- a/bootstrap/formal-oracles/lean/DISCREPANCIES.md +++ b/bootstrap/formal-oracles/lean/DISCREPANCIES.md @@ -14,3 +14,24 @@ Observed on aien-dev/aienos main 2ee61b4. Read only. This note records what each | Holder | `new_holder` is not checked against the parent holder | Authority must equal parent reference (lines 244 to 246) | Consequence for the Lean model: the Rust "child expiry <= parent" and "child depth < parent depth" hold by construction (equality, and minus one). The C rules differ in kind. They are separate takeover items, not assumed equal. The overwrite behaviour affects identity uniqueness, not attenuation, and is not modelled (the model lets a newer token shadow an older one with the same id). + +# Receipt consistency (FORMAL-4) + +Observed on aien-dev/aienos main 2ee61b4, `crates/aienos-artifact/src/receipt.rs`. Read only. The Lean model is `AienReceipt/`; it models `validate`, `decode` and `encode` (field level, offsets in `AienReceipt/Layout.lean`). + +## Finding: CanaryFailed decision is not tied to the CanaryFailed execution status + +- `validate` (lines 320 to 365) matches on the decision at lines 336 to 355. The `Rejected` arm (337 to 349) is strict. The `_` arm (350 to 354) checks only `rejection_stage == 0` and `rejection_reason == 0`. +- No line of `validate` mentions `ExecutionStatusCode::CanaryFailed` (the only places the name appears in this file are the enum variants at lines 45 and 66 and the code tables at 195 and 228). `decode` (244 to 317) adds only layout and enum range checks, then calls `validate`. +- So the code ALLOWS a mismatch in both directions: decision `CanaryFailed` with execution status `Exited` (or anything else), and decision `Admitted` or `Destroyed` with execution status `CanaryFailed`. `decode` accepts such bytes. Lean statements: `validate_decision_irrelevant_when_not_rejected`, `canary_decision_status_unconstrained` (Theorems.lean) and `finding_*` in Corpus.lean. +- The tests do not cover it: `receipt_tests.rs` never names `CanaryFailed`. +- Producer side: `build_receipt` (`crates/aienos-kernel/src/artifact_loader.rs` lines 1460 to 1530) emits only decisions `Admitted` and `Rejected`, and its status mapping (1472 to 1477) never yields `ExecutionStatusCode::CanaryFailed`. So the mismatch is unreachable from this producer today. It is reachable for any other producer, or for hand-built or externally supplied bytes, because validation does not refuse it. +- Not asserted either way whether the missing linkage is intended. Recorded, not fixed (aienos is read only here). + +## Code is stronger than the plan + +As the manifest notes: `Rejected` also forces exit status 0, zero syscalls, zero object reads, zero denials, and result flags limited to RECLAIMED (lines 338 to 345). The Lean theorems prove the code's rules. `rejected_arm` is the full Rejected arm. + +## Abstractions (assumptions, not proofs) + +SHA-256 nonce derivation is an arbitrary function `nf` (every theorem holds for all `nf`). Ed25519 is not modelled (`verify` is out of scope; `validate` does not check signatures). Digests are naturals, zero meaning all zero. Little-endian byte packing is assumed to be a per-field bijection. Integer width overflow is not modelled. Unknown flag bits are unrepresentable in the model; `decode` checks them on the raw word (`flags < 8`, `rflags < 256`). `exit_status` is an `Int`. Opaque digests that no rule reads are kept as one list. diff --git a/bootstrap/formal-oracles/lean/MutantAbsentFlagClaim.lean b/bootstrap/formal-oracles/lean/MutantAbsentFlagClaim.lean new file mode 100644 index 0000000..cbc695c --- /dev/null +++ b/bootstrap/formal-oracles/lean/MutantAbsentFlagClaim.lean @@ -0,0 +1,7 @@ +import MutantReceiptModels +namespace AienReceipt +/-- Real-model theorem restated for mutant validateM5. MUST FAIL to check. -/ +theorem mutant_absent_flag {nf} {r : Receipt} (h : validateM5 nf r = true) (ha : r.pres.machine = false) : r.machineId = 0 := by + simp only [validateM5, Bool.and_eq_true] at h + simp_all +end AienReceipt diff --git a/bootstrap/formal-oracles/lean/MutantCanaryClaim.lean b/bootstrap/formal-oracles/lean/MutantCanaryClaim.lean new file mode 100644 index 0000000..b663f33 --- /dev/null +++ b/bootstrap/formal-oracles/lean/MutantCanaryClaim.lean @@ -0,0 +1,7 @@ +import MutantReceiptModels +namespace AienReceipt +/-- Real-model theorem restated for mutant validateM4. MUST FAIL to check. -/ +theorem mutant_canary {nf} {r : Receipt} (h : validateM4 nf r = true) (hc : r.rflags.canaryPassed = true) : r.status = .exited ∧ r.exitCode = 0 := by + simp only [validateM4, Bool.and_eq_true] at h + simp_all +end AienReceipt diff --git a/bootstrap/formal-oracles/lean/MutantNoReasonClaim.lean b/bootstrap/formal-oracles/lean/MutantNoReasonClaim.lean new file mode 100644 index 0000000..284c72c --- /dev/null +++ b/bootstrap/formal-oracles/lean/MutantNoReasonClaim.lean @@ -0,0 +1,7 @@ +import MutantReceiptModels +namespace AienReceipt +/-- Real-model theorem restated for mutant validateM2. MUST FAIL to check. -/ +theorem mutant_no_reason {nf} {r : Receipt} (h : validateM2 nf r = true) (hr : r.decision = .rejected) : r.stage ≠ 0 ∧ r.reason ≠ 0 := by + simp only [validateM2, decisionOkM2, hr, Bool.and_eq_true] at h + simp_all +end AienReceipt diff --git a/bootstrap/formal-oracles/lean/MutantNonRejectedFieldsClaim.lean b/bootstrap/formal-oracles/lean/MutantNonRejectedFieldsClaim.lean new file mode 100644 index 0000000..8d48cb8 --- /dev/null +++ b/bootstrap/formal-oracles/lean/MutantNonRejectedFieldsClaim.lean @@ -0,0 +1,7 @@ +import MutantReceiptModels +namespace AienReceipt +/-- Real-model theorem restated for mutant validateM3. MUST FAIL to check. -/ +theorem mutant_non_rejected {nf} {r : Receipt} (h : validateM3 nf r = true) (hr : r.decision ≠ .rejected) : r.stage = 0 ∧ r.reason = 0 := by + simp only [validateM3, decisionOkM3, Bool.and_eq_true] at h + cases hd : r.decision <;> simp_all +end AienReceipt diff --git a/bootstrap/formal-oracles/lean/MutantReceiptModels.lean b/bootstrap/formal-oracles/lean/MutantReceiptModels.lean new file mode 100644 index 0000000..f44d07a --- /dev/null +++ b/bootstrap/formal-oracles/lean/MutantReceiptModels.lean @@ -0,0 +1,54 @@ +import AienReceipt.Corpus +/-! +# Deliberate mutants of `validate` (each must break one required rule) + +WRONG on purpose. Each mutant model compiles; the matching claim file must not. +-/ +namespace AienReceipt + +/-- MUTANT 1 (rejected_but_ran): a Rejected receipt is not required to have status NotRun. -/ +def decisionOkM1 (r : Receipt) : Bool := + match r.decision with + | .rejected => + r.stage != 0 && r.reason != 0 && r.exitCode == 0 && + r.syscalls == 0 && r.reads == 0 && r.denials == 0 && r.rflags.onlyReclaimed + | _ => r.stage == 0 && r.reason == 0 +def validateM1 (nf : Nat → Nat → Nat) (r : Receipt) : Bool := + presenceOk r && codesOk r && decisionOkM1 r && canaryOk r && nonceOk nf r + +/-- MUTANT 2 (rejected_without_reason): a Rejected receipt may have stage 0 or reason 0. -/ +def decisionOkM2 (r : Receipt) : Bool := + match r.decision with + | .rejected => + r.status == .notRun && r.exitCode == 0 && + r.syscalls == 0 && r.reads == 0 && r.denials == 0 && r.rflags.onlyReclaimed + | _ => r.stage == 0 && r.reason == 0 +def validateM2 (nf : Nat → Nat → Nat) (r : Receipt) : Bool := + presenceOk r && codesOk r && decisionOkM2 r && canaryOk r && nonceOk nf r + +/-- MUTANT 3 (non_rejected_with_rejection_fields): non-Rejected receipts are not checked. -/ +def decisionOkM3 (r : Receipt) : Bool := + match r.decision with + | .rejected => + r.stage != 0 && r.reason != 0 && r.status == .notRun && r.exitCode == 0 && + r.syscalls == 0 && r.reads == 0 && r.denials == 0 && r.rflags.onlyReclaimed + | _ => true +def validateM3 (nf : Nat → Nat → Nat) (r : Receipt) : Bool := + presenceOk r && codesOk r && decisionOkM3 r && canaryOk r && nonceOk nf r + +/-- MUTANT 4 (canary_passed_without_clean_exit): the CANARY_PASSED rule is removed. -/ +def validateM4 (nf : Nat → Nat → Nat) (r : Receipt) : Bool := + presenceOk r && codesOk r && decisionOk r && nonceOk nf r + +/-- MUTANT 5 (absent_flag_with_nonzero_value): the presence-flag rule is removed. -/ +def validateM5 (nf : Nat → Nat → Nat) (r : Receipt) : Bool := + codesOk r && decisionOk r && canaryOk r && nonceOk nf r + +/-- witness: mutant 1 accepts a Rejected receipt that claims it exited... -/ +theorem m1_accepts_rejected_but_ran : + validateM1 nf0 { rejectedR with status := .exited } = true := by decide +/-- ...which the real model refuses -/ +theorem real_refuses_rejected_but_ran : + validate nf0 { rejectedR with status := .exited } = false := by decide + +end AienReceipt diff --git a/bootstrap/formal-oracles/lean/MutantRejectedRanClaim.lean b/bootstrap/formal-oracles/lean/MutantRejectedRanClaim.lean new file mode 100644 index 0000000..84d3b72 --- /dev/null +++ b/bootstrap/formal-oracles/lean/MutantRejectedRanClaim.lean @@ -0,0 +1,7 @@ +import MutantReceiptModels +namespace AienReceipt +/-- Real-model theorem restated for mutant validateM1. MUST FAIL to check. -/ +theorem mutant_rejected_ran {nf} {r : Receipt} (h : validateM1 nf r = true) (hr : r.decision = .rejected) : r.status = .notRun := by + simp only [validateM1, decisionOkM1, hr, Bool.and_eq_true] at h + simp_all +end AienReceipt diff --git a/bootstrap/formal-oracles/lean/lakefile.toml b/bootstrap/formal-oracles/lean/lakefile.toml index d075c2f..a695e71 100644 --- a/bootstrap/formal-oracles/lean/lakefile.toml +++ b/bootstrap/formal-oracles/lean/lakefile.toml @@ -1,5 +1,5 @@ name = "aien_formal_oracle" -defaultTargets = ["AienCap"] +defaultTargets = ["AienCap", "AienReceipt"] # Core Lean only. No Mathlib, no Batteries, no other dependency: builds offline. @@ -23,3 +23,32 @@ globs = ["MutantExpiryClaim"] [[lean_lib]] name = "MutantDepthClaim" globs = ["MutantDepthClaim"] + +# FORMAL-4: receipt consistency model (AIEN.INV.RECEIPT.REJECTION_CONSISTENCY.V1). +[[lean_lib]] +name = "AienReceipt" + +# FORMAL-4 mutants: models compile, each claim file must FAIL to build. +[[lean_lib]] +name = "MutantReceiptModels" +globs = ["MutantReceiptModels"] + +[[lean_lib]] +name = "MutantRejectedRanClaim" +globs = ["MutantRejectedRanClaim"] + +[[lean_lib]] +name = "MutantNoReasonClaim" +globs = ["MutantNoReasonClaim"] + +[[lean_lib]] +name = "MutantNonRejectedFieldsClaim" +globs = ["MutantNonRejectedFieldsClaim"] + +[[lean_lib]] +name = "MutantCanaryClaim" +globs = ["MutantCanaryClaim"] + +[[lean_lib]] +name = "MutantAbsentFlagClaim" +globs = ["MutantAbsentFlagClaim"] diff --git a/bootstrap/formal-oracles/lean/oracle.toml b/bootstrap/formal-oracles/lean/oracle.toml index 712e344..b8728a3 100644 --- a/bootstrap/formal-oracles/lean/oracle.toml +++ b/bootstrap/formal-oracles/lean/oracle.toml @@ -32,7 +32,16 @@ manifest = "formal/invariants/capability-attenuation-v1.toml" invariant = "AIEN.INV.CAP.REVOCATION_CLOSURE.V1" manifest = "formal/invariants/capability-revocation-v1.toml" +[[covers]] +invariant = "AIEN.INV.RECEIPT.REJECTION_CONSISTENCY.V1" +manifest = "formal/invariants/artifact-receipt-v1.toml" + [run] script = "run-cap-formal.sh" result_line = "CAP_FORMAL_BOOTSTRAP: PASS|FAIL|NOT_RUN" note = "A PASS is one evidence input bound to a source and model revision. It is not authority (ARCH-0027 point 6)." + +[[run_extra]] +script = "run-receipt-formal.sh" +result_line = "RECEIPT_FORMAL_BOOTSTRAP: PASS|FAIL|NOT_RUN" +note = "FORMAL-4. Same rule: PASS is one evidence input, not authority." diff --git a/bootstrap/formal-oracles/lean/run-receipt-formal.sh b/bootstrap/formal-oracles/lean/run-receipt-formal.sh new file mode 100755 index 0000000..04f2108 --- /dev/null +++ b/bootstrap/formal-oracles/lean/run-receipt-formal.sh @@ -0,0 +1,56 @@ +#!/bin/sh +# FORMAL-4: receipt consistency formal bootstrap runner (DEVELOPMENT_ORACLE, not permanent lineage). +# Builds the real model (must PASS) and each deliberate mutant claim (must FAIL for a proof reason). +# Last stdout line: RECEIPT_FORMAL_BOOTSTRAP: PASS | FAIL | NOT_RUN +# No network needed after the toolchain is installed (core Lean only, no dependencies). +set -u +here=$(cd "$(dirname "$0")" && pwd) +cd "$here" || exit 2 +PATH="$HOME/.elan/bin:$PATH"; export PATH + +quiet="${AIEN_SPARK_QUIET_FLAG:-$HOME/workspace/.spark-quiet}" +if [ -e "$quiet" ]; then + echo "quiet flag present ($quiet): not building" + echo "RECEIPT_FORMAL_BOOTSTRAP: NOT_RUN" + exit 3 +fi +command -v lake >/dev/null 2>&1 || { echo "lake not found (install elan)"; echo "RECEIPT_FORMAL_BOOTSTRAP: NOT_RUN"; exit 3; } + +log=$(mktemp); trap 'rm -f "$log"' EXIT +echo "toolchain: $(cat lean-toolchain) / $(lean --version)" +fail=0 + +# Forbidden constructs in the receipt sources (comments excluded by matching code-like uses). +if grep -nE '^[^-]*\b(sorry|admit|native_decide|unsafe)\b|^axiom |^ *axiom ' AienReceipt/*.lean AienReceipt.lean >"$log"; then + echo "forbidden_constructs: FAIL"; cat "$log"; fail=1 +else + echo "forbidden_constructs: PASS" +fi + +lake build AienReceipt >"$log" 2>&1; rc=$? +if [ $rc -eq 0 ] && ! grep -qi "sorry" "$log"; then + echo "real_model: PASS" +else + echo "real_model: FAIL (rc=$rc)"; tail -20 "$log"; fail=1 +fi + +lake build MutantReceiptModels >"$log" 2>&1; rc=$? +if [ $rc -eq 0 ] && ! grep -qi "sorry" "$log"; then + echo "mutant_models_compile: PASS" +else + echo "mutant_models_compile: FAIL (rc=$rc)"; tail -20 "$log"; fail=1 +fi + +# A mutant is killed only by a proof error in its own claim file, not a syntax or import error. +for m in MutantRejectedRanClaim MutantNoReasonClaim MutantNonRejectedFieldsClaim MutantCanaryClaim MutantAbsentFlagClaim; do + lake build "$m" >"$log" 2>&1; rc=$? + if [ $rc -ne 0 ] && grep -qE "^error: $m.lean:[0-9]+:[0-9]+: (unsolved goals|simp_all made no progress|Type mismatch|simp made no progress|Tactic)" "$log" \ + && ! grep -q "unexpected token" "$log" && ! grep -qE "^error: (MutantReceiptModels|AienReceipt)" "$log"; then + echo "$m: FAIL_AS_EXPECTED (mutant killed)" + else + echo "$m: UNEXPECTED_BUILD_SUCCESS_OR_NO_PROOF_ERROR (rc=$rc), mutant survived"; fail=1 + fi +done + +if [ $fail -eq 0 ]; then echo "RECEIPT_FORMAL_BOOTSTRAP: PASS"; else echo "RECEIPT_FORMAL_BOOTSTRAP: FAIL"; fi +exit $fail ```