Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions docs/decisions/2026-11-24-registry-state-ownership.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: CC-BY-SA-4.0
// Copyright (c) 2026 Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
= Registry state and ownership proof boundary

The executable FFI/API remains pure Zig. `Abi.RegistryState` develops the
authoritative Idris2 transition contract alongside the existing registry and
protocol contracts. Each endpoint is one value containing callback/context
identity, not merely a Boolean registration flag.

The total module proves selected-slot replacement, preservation of the other
slot, busy registration/unregistration refusal, duplicate/absent refusal
framing, exact selected endpoint identity, reentrant call refusal, and endpoint
frame restoration on normal callback return. The existing 1,024-case native
ABI differential campaign remains mandatory. Hosted run
https://github.com/hyperpolymath/unified-api-adapter/actions/runs/45510851199[45510851199]
checked commit `d54b8a3bdb305c63e38f911d32a2fa1b3c3e5881`: the fresh Idris2
totality check and no-hole query passed, alongside the native ABI campaign,
lifetime/reentrancy controls and rejection mutant. This validates the model
and the recorded executable tests, not the pending implementation correspondence.

== Ownership assumptions

The caller owns a live registry exclusively and serializes access. Registered
callback code and context remain live until unregister/destroy, and throughout
any synchronous callback. Input bytes remain readable for the callback's
duration. Foreign code must not mutate registry memory through raw aliases.

An `Invocation` is an ordinary logical snapshot, not an unforgeable runtime
capability and not a linear ownership proof. Busy restoration applies when the
callback returns normally; it establishes neither termination nor safe
non-local jumps, arbitrary foreign corruption, unwinding, or concurrent calls.

Pending implementation correspondence: connect every Zig state transition to
these Idris definitions, bind imported helpers/source bytes, and validate
lease/provenance rules for actual handles and borrowed buffers. Agda then
Lean/Coq state-model witnesses and independent replay must precede any broader
proof claim. No Mathesis admission or proof grade is asserted here.
11 changes: 9 additions & 2 deletions scripts/check-registry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ rm -f "$evidence/result.txt"
work=$(mktemp -d -t uapi-registry-check.XXXXXXXX)
trap 'rm -rf -- "$work"' EXIT
mkdir -p "$work/Abi"
for module in Protocol ProtocolFinite Status Registry; do
for module in Protocol ProtocolFinite Status Registry RegistryState; do
cp "$root/src/interface/Abi/$module.idr" "$work/Abi/"
done
if ! (cd "$work"; "$idris" --total -Werror --check Abi/RegistryState.idr) > "$evidence/state-build.log" 2>&1; then
cat "$evidence/state-build.log" >&2
exit 1
fi
if grep -q '^Error:' "$evidence/state-build.log"; then cat "$evidence/state-build.log" >&2; exit 1; fi
(cd "$work"; printf ':m\n:q\n' | "$idris" --no-banner Abi/RegistryState.idr) > "$evidence/state-holes.log" 2>&1
grep -q 'No holes' "$evidence/state-holes.log"
if grep -q '^Error:' "$evidence/state-holes.log"; then cat "$evidence/state-holes.log" >&2; exit 1; fi
cp "$root/src/interface/RegistryCheck.idr" "$work/"
cp "$root/src/interface/ProtocolTagOracle.idr" "$work/Main.idr"
(cd "$work"; sha256sum Abi/*.idr RegistryCheck.idr Main.idr) > "$evidence/inputs.sha256"
Expand Down Expand Up @@ -52,4 +60,3 @@ grep -q 'finiteDecodeSound' "$evidence/reject.log"
} > "$evidence/identity.log"
echo PASS > "$evidence/result.txt"
printf 'PASS: total proof modules, no holes, executable matrix, rejecting decoder mutation.\nEvidence: %s\n' "$evidence"

10 changes: 8 additions & 2 deletions scripts/check-runtime-registry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ evidence="$root/build/runtime-registry-evidence"
mkdir -p "$evidence"
echo INCOMPLETE > "$evidence/result.txt"
work=$(mktemp -d -t uaa-runtime-registry.XXXXXXXX)
trap 'status=$?; rm -rf -- "$work"; if ((status)); then echo FAIL > "$evidence/result.txt"; fi' EXIT
finish() {
local status=$?
# Preserve a failed fresh oracle's diagnostics before deleting its directory.
if [[ -d "$work/idris-evidence" ]]; then cp -R "$work/idris-evidence" "$evidence/"; fi
rm -rf -- "$work"
if ((status)); then echo FAIL > "$evidence/result.txt"; fi
}
trap finish EXIT
zig=$(command -v "${ZIG:-zig}")
zig=$(cd "$(dirname "$zig")" && pwd)/$(basename "$zig")
test "$("$zig" version)" = 0.15.2
Expand Down Expand Up @@ -47,4 +54,3 @@ if "$work/caller" > "$evidence/mutant.csv" 2> "$evidence/mutant.log"; then
fi
echo PASS > "$evidence/result.txt"
echo 'PASS: real C ABI, Idris differential matrix, lifetime/refusal regressions and rejecting Zig mutant'

146 changes: 146 additions & 0 deletions src/interface/Abi/RegistryState.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
-- SPDX-License-Identifier: MPL-2.0
-- Copyright (c) 2026 Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
||| State contract for one live, exclusively owned Zig registry.
||| Endpoint values include callback/context identity; no callback is executed
||| by this model. Raw-pointer validity and caller serialization are premises.
module Abi.RegistryState
import Abi.Registry
import Abi.Status

%default total

public export
data Slot = RESTSlot | MCPSlot

public export
record State endpoint where
constructor MkState
entries : Registry endpoint
busy : Bool

public export
lookup : Slot -> Registry endpoint -> Maybe endpoint
lookup RESTSlot r = restEndpoint r
lookup MCPSlot r = mcpEndpoint r

public export
replace : Slot -> Maybe endpoint -> Registry endpoint -> Registry endpoint
replace RESTSlot e r = MkRegistry e (mcpEndpoint r)
replace MCPSlot e r = MkRegistry (restEndpoint r) e

public export
other : Slot -> Slot
other RESTSlot = MCPSlot
other MCPSlot = RESTSlot

||| Updating a slot neither changes nor fabricates the other endpoint.
public export
replaceFramesOther : (slot : Slot) -> (e : Maybe endpoint) ->
(r : Registry endpoint) -> lookup (other slot) (replace slot e r) = lookup (other slot) r
replaceFramesOther RESTSlot e r = Refl
replaceFramesOther MCPSlot e r = Refl

public export
replaceSelectsExact : (slot : Slot) -> (e : Maybe endpoint) ->
(r : Registry endpoint) -> lookup slot (replace slot e r) = e
replaceSelectsExact RESTSlot e r = Refl
replaceSelectsExact MCPSlot e r = Refl

public export
record Reply endpoint where
constructor MkReply
status : Status
next : State endpoint

public export
register : Slot -> endpoint -> State endpoint -> Reply endpoint
register slot e (MkState r True) = MkReply ProcessFailed (MkState r True)
register slot e (MkState r False) = case lookup slot r of
Just _ => MkReply AlreadyExists (MkState r False)
Nothing => MkReply Ok (MkState (replace slot (Just e) r) False)

public export
unregister : Slot -> State endpoint -> Reply endpoint
unregister slot (MkState r True) = MkReply ProcessFailed (MkState r True)
unregister slot (MkState r False) = case lookup slot r of
Nothing => MkReply NotFound (MkState r False)
Just _ => MkReply Ok (MkState (replace slot Nothing r) False)

public export
busyRegisterRefuses : (slot : Slot) -> (e : endpoint) -> (r : Registry endpoint) ->
register slot e (MkState r True) = MkReply ProcessFailed (MkState r True)
busyRegisterRefuses slot e r = Refl

public export
busyUnregisterRefuses : (slot : Slot) -> (r : Registry endpoint) ->
unregister slot (MkState r True) = MkReply ProcessFailed (MkState r True)
busyUnregisterRefuses slot r = Refl

public export
duplicateRegisterFrames : (slot : Slot) -> (e : endpoint) -> (old : endpoint) -> (r : Registry endpoint) ->
lookup slot r = Just old ->
register slot e (MkState r False) = MkReply AlreadyExists (MkState r False)
duplicateRegisterFrames slot e old r found = rewrite found in Refl

public export
absentUnregisterFrames : (slot : Slot) -> (r : Registry endpoint) ->
lookup slot r = Nothing ->
unregister slot (MkState r False) = MkReply NotFound (MkState r False)
absentUnregisterFrames slot r absent = rewrite absent in Refl

||| This is a logical call ticket, not an unforgeable machine pointer or a
||| proof of linear ownership. The snapshot is held across a NORMAL return.
public export
record Invocation endpoint where
constructor MkInvocation
snapshot : Registry endpoint
selected : endpoint

public export
beginCall : Slot -> State endpoint -> Either (Reply endpoint) (Invocation endpoint)
beginCall slot (MkState r True) = Left (MkReply ProcessFailed (MkState r True))
beginCall slot (MkState r False) = case lookup slot r of
Nothing => Left (MkReply Unsupported (MkState r False))
Just e => Right (MkInvocation r e)

public export
duringCall : Invocation endpoint -> State endpoint
duringCall call = MkState (snapshot call) True

public export
normalReturn : Invocation endpoint -> State endpoint
normalReturn call = MkState (snapshot call) False

public export
beginPreservesIdentity : (slot : Slot) -> (r : Registry endpoint) -> (e : endpoint) ->
lookup slot r = Just e ->
beginCall slot (MkState r False) = Right (MkInvocation r e)
beginPreservesIdentity slot r e found = rewrite found in Refl

public export
normalReturnRestoresFrame : (call : Invocation endpoint) ->
entries (normalReturn call) = entries (duringCall call)
normalReturnRestoresFrame call = Refl

public export
reentrantCallRefuses : (slot : Slot) -> (call : Invocation endpoint) ->
beginCall slot (duringCall call) = Left (MkReply ProcessFailed (duringCall call))
reentrantCallRefuses slot call = Refl

||| Decode foreign signed status BEFORE narrowing it; values outside the
||| declared ABI range become ProcessFailed, never a fabricated status.
public export
sanitize : Integer -> Status
sanitize 0 = Ok
sanitize 1 = Err
sanitize 2 = InvalidParam
sanitize 3 = OutOfMemory
sanitize 4 = NullPointer
sanitize 5 = PathDenied
sanitize 6 = ProcessFailed
sanitize 7 = Timeout
sanitize 8 = NotFound
sanitize 9 = AlreadyExists
sanitize 10 = SlotExhausted
sanitize 11 = Unsupported
sanitize _ = ProcessFailed
Loading