import Base import ./world.bend as W import ./system.bend as Sy # One event domain; World owns payloads, this owner owns batch and reader metadata. # Ordered detached Data publications grouped at a runtime tick. This is not an # affine payload owner or an acknowledgement from a reader. type Batch is Data: Batch{tick: Nat,values: List<&2,E>} # Activated reader metadata: last consumed cursor and activation tick. Merely # allocating a Reader token does not add a Position or hold old batches. type Position is Data: Position{id: U32,cursor: Nat,registeredAt: Nat} # Actual affine token binding runtime namespace and reader id to one registry id. # Raw construction is trusted setup, not proof of registration/provenance. type Reader is Type: Reader{namespace: U32,id: U32,registryId: U32} # Sole actual World plus Data batch/reader metadata and retention accounting. # This lower-level runtime handles Data E; affine Type payload logs use separate # owned-event APIs. Raw metadata construction and World callbacks remain trusted. type Runtime is Type: Runtime{world: W.World,namespace: U32,batches: List<&2,Batch>,positions: List<&2,Position>,nextReader: U32,tick: Nat,frameStart: Nat,boundary: Nat,capacity: Nat,droppedThrough: Nat} # Unread Data values and lag flag supplied to a runner with actual possibly- # affine Args. Callback consumption does not imply Args recovery on failure. type Input is Type: Input{values: List<&2,E>,lagged: Bool,args: A} # Detached unread Data in publication order plus bounded-retention lag status. # Reading alone does not advance a cursor or protect messages from trimming. type Reading is Data: Reading{values: List<&2,E>,lagged: Bool} type Trimmed is Data: Trimmed{batches: List<&2,Batch>,dropped: Nat} def count_loop(~E: Data,items: List<&2,E>,+acc: Nat) -> Nat: match items: case []: acc case _ <> rest: count_loop(~E,rest,1n+acc) def count(~E: Data,items: List<&2,E>) -> Nat: count_loop(~E,items,0n) def append_loop(~E: Data,reversed: List<&2,E>,+output: List<&2,E>) -> List<&2,E>: match reversed: case []: output case head <> rest: append_loop(~E,rest,head <> output) def append(~E: Data,+left: List<&2,E>,+right: List<&2,E>) -> List<&2,E>: match left right: case Nil{} right: right case left Nil{}: left case left right: append_loop(~E,List.reverse(&2,E,left),right) def values(~E: Data,batches: List<&2,Batch>) -> List<&2,E>: match batches: case []: [] case Batch{_,items} <> rest: append(~E,items,values(~E,rest)) def size_loop(~E: Data,batches: List<&2,Batch>,+acc: Nat) -> Nat: match batches: case []: acc case Batch{_,items} <> rest: size_loop(~E,rest,count_loop(~E,items,acc)) def size(~E: Data,batches: List<&2,Batch>) -> Nat: size_loop(~E,batches,0n) def hold_boundary(positions: List<&2,Position>,+boundary: Nat) -> Nat: match positions: case []: boundary case Position{_,cursor,_} <> rest: hold_boundary(rest,Nat.min(boundary,cursor)) def past_checked(~E: Data,remove: Bool,+tick: Nat,+items: List<&2,E>,+rest: List<&2,Batch>,+dropped: Nat,ih: Nat -> Trimmed) -> Trimmed: match remove: case False{}: Trimmed{Batch{tick,items} <> rest,dropped} case True{}: ih(Nat.max(dropped,tick)) def past_window(~E: Data,batches: List<&2,Batch>,+boundary: Nat,+dropped: Nat) -> Trimmed: match batches: case []: Trimmed{[],dropped} case Batch{+tick,+items} <> +rest: past_checked(~E,Nat.is_le(tick,boundary),tick,items,rest,dropped,gone => past_window(~E,rest,boundary,gone)) def capacity_checked(~E: Data,overflow: Bool,+tick: Nat,+items: List<&2,E>,+rest: List<&2,Batch>,+dropped: Nat,ih: Nat -> Trimmed) -> Trimmed: match overflow: case False{}: Trimmed{Batch{tick,items} <> rest,dropped} case True{}: ih(Nat.max(dropped,tick)) def at_capacity(~E: Data,batches: List<&2,Batch>,+capacity: Nat,+dropped: Nat) -> Trimmed: match batches: case []: Trimmed{[],dropped} case Batch{+tick,+items} <> +rest: capacity_checked(~E,Nat.is_lt(capacity,size(~E,Batch{tick,items} <> rest)),tick,items,rest,dropped,gone => at_capacity(~E,rest,capacity,gone)) def trim_capacity(~E: Data,trimmed: Trimmed,+capacity: Nat) -> Trimmed: match trimmed: case Trimmed{batches,dropped}: at_capacity(~E,batches,capacity,dropped) def trim_result(~S: Data,~C: Type,~R: Type,~E: Data,world: W.World,+namespace: U32,+positions: List<&2,Position>,+nextReader: U32,+tick: Nat,+frameStart: Nat,+boundary: Nat,+capacity: Nat,trimmed: Trimmed) -> Runtime: match trimmed: case Trimmed{+batches,dropped}: Runtime{W.events_replace(~S,~C,~R,~E,world,values(~E,batches)),namespace,batches,positions,nextReader,tick,frameStart,boundary,capacity,dropped} def trim(~S: Data,~C: Type,~R: Type,~E: Data,runtime: Runtime) -> Runtime: match runtime: case Runtime{world,+namespace,+batches,+positions,+nextReader,+tick,+frameStart,+boundary,+capacity,+dropped}: trim_result(~S,~C,~R,~E,world,namespace,positions,nextReader,tick,frameStart,boundary,capacity,trim_capacity(~E,past_window(~E,batches,hold_boundary(positions,boundary),dropped),capacity)) # Advance frame markers and apply trim, returning actual Runtime/World. This # bounds normal visibility while preserving unread holds only within hard capacity. # Does not run a system, apply pending structural commands or acknowledge readers. def frame(~S: Data,~C: Type,~R: Type,~E: Data,runtime: Runtime) -> Runtime: match runtime: case Runtime{world,namespace,batches,positions,nextReader,+tick,+frameStart,_,capacity,dropped}: trim(~S,~C,~R,~E,Runtime{world,namespace,batches,positions,nextReader,tick,tick,frameStart,capacity,dropped}) def start_named(~S: Data,~C: Type,~R: Type,~E: Data,+capacity: Nat,named: W.World & U32) -> Runtime: (world,namespace) = named Runtime{world,namespace,[],[],1,0n,0n,0n,capacity,0n} # Start runtime metadata for the actual World namespace and supplied capacity. # Initial batches/readers/tick are empty/zero; this does not consume World events # or automatically synchronize an already populated log. def start(~S: Data,~C: Type,~R: Type,~E: Data,world: W.World,+capacity: Nat) -> Runtime: start_named(~S,~C,~R,~E,capacity,W.namespace(~S,~C,~R,~E,world)) def update_checked(found: Bool,dispose: Bool,+id: U32,+old: Nat,+registered: Nat,+rest: List<&2,Position>,+cursor: Nat) -> List<&2,Position>: match found dispose: case False{} _: Position{id,old,registered} <> rest case True{} False{}: Position{id,cursor,registered} <> rest case True{} True{}: rest def update(positions: List<&2,Position>,+id: U32,+cursor: Nat,+dispose: Bool) -> List<&2,Position>: match positions: case []: [] case Position{+actual,+old,+registered} <> rest: update_checked(U32.is_eq(actual,id),dispose,actual,old,registered,update(rest,id,cursor,dispose),cursor) def choose_position(found: Bool,+position: Position,fallback: Maybe) -> Maybe: match found: case False{}: fallback case True{}: Some{position} def lookup_checked(position: Position,+id: U32,fallback: Maybe) -> Maybe: match position: case Position{+actual,+cursor,+registered}: choose_position(U32.is_eq(actual,id),Position{actual,cursor,registered},fallback) def lookup(positions: List<&2,Position>,+id: U32) -> Maybe: match positions: case []: None{} case +position <> rest: lookup_checked(position,id,lookup(rest,id)) def since_checked(~E: Data,visible: Bool,+items: List<&2,E>,+rest: List<&2,E>) -> List<&2,E>: match visible: case False{}: rest case True{}: append(~E,items,rest) def since(~E: Data,batches: List<&2,Batch>,+cursor: Nat) -> List<&2,E>: match batches: case []: [] case Batch{+tick,+items} <> rest: since_checked(~E,Nat.is_lt(cursor,tick),items,since(~E,rest,cursor)) def reading(~E: Data,position: Position,+batches: List<&2,Batch>,+dropped: Nat) -> Reading: match position: case Position{_,+cursor,+registered}: Reading{since(~E,batches,cursor),Nat.is_lt(Nat.max(cursor,registered),dropped)} def sync_checked(~E: Data,nonempty: Bool,+batches: List<&2,Batch>,+tick: Nat,+published: List<&2,E>) -> List<&2,Batch>: match nonempty: case False{}: batches case True{}: append(~Batch,batches,[Batch{tick,published}]) def sync_values(~S: Data,~C: Type,~R: Type,~E: Data,observed: W.World & List<&2,E>,+namespace: U32,+batches: List<&2,Batch>,+positions: List<&2,Position>,+nextReader: U32,+tick: Nat,+frameStart: Nat,+boundary: Nat,+capacity: Nat,+dropped: Nat) -> Runtime: (world,+all) = observed +published = List.drop(&2,E,all,size(~E,batches)) Runtime{world,namespace,sync_checked(~E,Nat.is_lt(0n,count(~E,published)),batches,tick,published),positions,nextReader,tick,frameStart,boundary,capacity,dropped} def with_world_result(~S: Data,~C: Type,~R: Type,~E: Data,~O: Type,returned: W.World & O,+namespace: U32,+batches: List<&2,Batch>,+positions: List<&2,Position>,+nextReader: U32,+tick: Nat,+frameStart: Nat,+boundary: Nat,+capacity: Nat,+dropped: Nat) -> Runtime & O: (world,output) = returned (sync_values(~S,~C,~R,~E,W.events_view(~S,~C,~R,~E,world),namespace,batches,positions,nextReader,tick,frameStart,boundary,capacity,dropped),output) # Advanced action bridge: consume/return actual World and Out, advance runtime # tick, then synchronize newly appended World events by the existing retained-log # prefix length. Action must respect that synchronization invariant; this is not # a read-only guarantee, transaction wrapper or implicit structural barrier. def with_world(~S: Data,~C: Type,~R: Type,~E: Data,~O: Type,effect: W.World -> W.World & O,runtime: Runtime) -> Runtime & O: match runtime: case Runtime{world,+namespace,+batches,+positions,+nextReader,+tick,+frameStart,+boundary,+capacity,+dropped}: with_world_result(~S,~C,~R,~E,~O,effect(world),namespace,batches,positions,nextReader,1n+tick,frameStart,boundary,capacity,dropped) # Reader registration result. Success returns actual Runtime, Reader and registry; # failure returns Runtime without dropping World. No callback executes here. type Registered -> Input -> Sy.Outcome,Error,O>> is Type: Registered{runtime: Runtime,reader: Reader,registry: Sy.Registry,Error,O,runner>} RegisterFailed{runtime: Runtime} # Succeeded returns actual Out; Failed carries callback Error without consumed # Args; Rejected returns unconsumed Args. Higher-level semantic errors encoded # inside Out must be inspected separately from transport success. type Completion is Type: Succeeded{output: O} Failed{error: Error} Rejected{args: A} # Every reader run returns actual Runtime, Reader and registry with Completion. # No duplicate World or runner owner is retained in detached observations. type RunResult -> Input -> Sy.Outcome,Error,O>> is Type: Ran{runtime: Runtime,reader: Reader,registry: Sy.Registry,Error,O,runner>,completion: Completion} def registered_identity(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,world: W.World,identity: Sy.Registry,Error,O,runner> & U32,+namespace: U32,+batches: List<&2,Batch>,+positions: List<&2,Position>,+nextReader: U32,+tick: Nat,+frameStart: Nat,+boundary: Nat,+capacity: Nat,+dropped: Nat) -> Registered: (registry,+registryId) = identity Registered{Runtime{world,namespace,batches,positions,(nextReader+1 : U32),tick,frameStart,boundary,capacity,dropped},Reader{namespace,nextReader,registryId},registry} def registered_result(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,result: Sy.RegistrationResult,Error,O,runner>,+namespace: U32,+batches: List<&2,Batch>,+positions: List<&2,Position>,+nextReader: U32,+tick: Nat,+frameStart: Nat,+boundary: Nat,+capacity: Nat,+dropped: Nat) -> Registered: match result: case Sy.RegistrationFailed{world}: RegisterFailed{Runtime{world,namespace,batches,positions,nextReader,tick,frameStart,boundary,capacity,dropped}} case Sy.Registered{world,registry}: registered_identity(~S,~C,~R,~E,~A,~Error,~O,~runner,world,Sy.identity(~S,~C,~R,~E,~Input,~Error,~O,~runner,registry),namespace,batches,positions,nextReader,tick,frameStart,boundary,capacity,dropped) # Register a runner with supplied name/access on the actual World, allocate its # reader token on success and return Runtime on refusal. This does not run the # body or activate a reader position; unused registrations do not retain backlog. # Name/access and runner coherence are trusted lower-level provisioning. def register(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,runtime: Runtime,+name: String,+access: List<&2,String>) -> Registered: match runtime: case Runtime{world,+namespace,+batches,+positions,+nextReader,+tick,+frameStart,+boundary,+capacity,+dropped}: registered_result(~S,~C,~R,~E,~A,~Error,~O,~runner,Sy.register(~S,~C,~R,~E,~Input,~Error,~O,~runner,world,name,access),namespace,batches,positions,nextReader,tick,frameStart,boundary,capacity,dropped) def activation(position: Maybe,+positions: List<&2,Position>,+id: U32,+registered: Nat) -> List<&2,Position>: match position: case None{}: Position{id,0n,registered} <> positions case Some{_}: positions def activate(+positions: List<&2,Position>,+id: U32,+registered: Nat) -> List<&2,Position>: activation(lookup(positions,id),positions,id,registered) def run_outcome(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,outcome: Sy.Outcome,Error,O>,reader: Reader,registry: Sy.Registry,Error,O,runner>,+readerId: U32,+namespace: U32,+batches: List<&2,Batch>,+positions: List<&2,Position>,+nextReader: U32,+tick: Nat,+frameStart: Nat,+boundary: Nat,+capacity: Nat,+dropped: Nat) -> RunResult: match outcome: case Sy.Failed{world,error}: Ran{Runtime{world,namespace,batches,activate(positions,readerId,Nat.sub(tick,1n)),nextReader,tick,frameStart,boundary,capacity,dropped},reader,registry,Failed{error}} case Sy.Succeeded{world,output}: Ran{sync_values(~S,~C,~R,~E,W.events_view(~S,~C,~R,~E,world),namespace,batches,update(activate(positions,readerId,Nat.sub(tick,1n)),readerId,tick,False{}),nextReader,tick,frameStart,boundary,capacity,dropped),reader,registry,Succeeded{output}} def reject_input(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,input: Input,runtime: Runtime,reader: Reader,registry: Sy.Registry,Error,O,runner>) -> RunResult: match input: case Input{_,_,args}: Ran{runtime,reader,registry,Rejected{args}} def run_registered_result(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,result: Sy.RunResult,Error,O,runner>,reader: Reader,+readerId: U32,+namespace: U32,+batches: List<&2,Batch>,+positions: List<&2,Position>,+nextReader: U32,+tick: Nat,+frameStart: Nat,+boundary: Nat,+capacity: Nat,+dropped: Nat) -> RunResult: match result: case Sy.RegistrationRejected{registry,world,args}: reject_input(~S,~C,~R,~E,~A,~Error,~O,~runner,args,Runtime{world,namespace,batches,positions,nextReader,tick,frameStart,boundary,capacity,dropped},reader,registry) case Sy.Completed{registry,outcome}: run_outcome(~S,~C,~R,~E,~A,~Error,~O,~runner,outcome,reader,registry,readerId,namespace,batches,positions,nextReader,tick,frameStart,boundary,capacity,dropped) def run_reading(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,reading: Reading,world: W.World,reader: Reader,registry: Sy.Registry,Error,O,runner>,args: A,+readerId: U32,+namespace: U32,+batches: List<&2,Batch>,+positions: List<&2,Position>,+nextReader: U32,+tick: Nat,+frameStart: Nat,+boundary: Nat,+capacity: Nat,+dropped: Nat) -> RunResult: match reading: case Reading{items,lagged}: run_registered_result(~S,~C,~R,~E,~A,~Error,~O,~runner,Sy.run(~S,~C,~R,~E,~Input,~Error,~O,~runner,registry,world,Input{items,lagged,args}),reader,readerId,namespace,batches,positions,nextReader,tick,frameStart,boundary,capacity,dropped) def run_position(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,position: Maybe,world: W.World,reader: Reader,registry: Sy.Registry,Error,O,runner>,args: A,+readerId: U32,+namespace: U32,+batches: List<&2,Batch>,+positions: List<&2,Position>,+nextReader: U32,+tick: Nat,+frameStart: Nat,+boundary: Nat,+capacity: Nat,+dropped: Nat) -> RunResult: match position: case None{}: run_reading(~S,~C,~R,~E,~A,~Error,~O,~runner,reading(~E,Position{readerId,0n,tick},batches,dropped),world,reader,registry,args,readerId,namespace,batches,positions,nextReader,1n+tick,frameStart,boundary,capacity,dropped) case Some{position}: run_reading(~S,~C,~R,~E,~A,~Error,~O,~runner,reading(~E,position,batches,dropped),world,reader,registry,args,readerId,namespace,batches,positions,nextReader,1n+tick,frameStart,boundary,capacity,dropped) def run_checked(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,allowed: Bool,world: W.World,reader: Reader,registry: Sy.Registry,Error,O,runner>,args: A,+readerId: U32,+namespace: U32,+batches: List<&2,Batch>,+positions: List<&2,Position>,+nextReader: U32,+tick: Nat,+frameStart: Nat,+boundary: Nat,+capacity: Nat,+dropped: Nat) -> RunResult: match allowed: case False{}: Ran{Runtime{world,namespace,batches,positions,nextReader,tick,frameStart,boundary,capacity,dropped},reader,registry,Rejected{args}} case True{}: run_position(~S,~C,~R,~E,~A,~Error,~O,~runner,lookup(positions,readerId),world,reader,registry,args,readerId,namespace,batches,positions,nextReader,tick,frameStart,boundary,capacity,dropped) def run_identity(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,runtime: Runtime,reader: Reader,registry: Sy.Registry,Error,O,runner>,actual: U32,args: A) -> RunResult: match runtime reader: case Runtime{world,+namespace,+batches,+positions,+nextReader,+tick,+frameStart,+boundary,+capacity,+dropped} Reader{+readerNamespace,+readerId,+expected}: run_checked(~S,~C,~R,~E,~A,~Error,~O,~runner,Bool.and(U32.is_eq(namespace,readerNamespace),U32.is_eq(expected,actual)),world,Reader{readerNamespace,readerId,expected},registry,args,readerId,namespace,batches,positions,nextReader,tick,frameStart,boundary,capacity,dropped) def run_identified(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,runtime: Runtime,reader: Reader,identified: Sy.Registry,Error,O,runner> & U32,args: A) -> RunResult: (registry,actual) = identified run_identity(~S,~C,~R,~E,~A,~Error,~O,~runner,runtime,reader,registry,actual,args) # Validate token namespace/registry identity, then invoke the actual registered # runner with ordered unread Data, lag flag and Args. Admission rejection returns # Args without body execution; registry checks remain authoritative in Sy.run. # Success advances reader cursor and synchronizes returned publications. Callback # failure preserves unread position, activating an absent reader under the failed- # reader rule; returned World is whatever the callback returned, not automatically # rolled back by this runtime. Actual transaction behavior belongs to the runner. # Runtime ticks and reader cursors are distinct from system lifecycle cursors. def run(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,runtime: Runtime,reader: Reader,registry: Sy.Registry,Error,O,runner>,args: A) -> RunResult: run_identified(~S,~C,~R,~E,~A,~Error,~O,~runner,runtime,reader,Sy.identity(~S,~C,~R,~E,~Input,~Error,~O,~runner,registry),args) # A condition skip discards stream backlog without invoking gameplay. def skip_checked(~S: Data,~C: Type,~R: Type,~E: Data,allowed: Bool,runtime: Runtime,reader: Reader,+id: U32) -> Runtime & Reader: match allowed runtime: case False{} runtime: (runtime,reader) case True{} Runtime{world,namespace,batches,positions,nextReader,+tick,frameStart,boundary,capacity,dropped}: (Runtime{world,namespace,batches,update(positions,id,tick,False{}),nextReader,tick,frameStart,boundary,capacity,dropped},reader) # Condition-false lifecycle operation: for a matching namespace, advance an # existing position to current tick without invoking gameplay. Does not activate # an absent reader; foreign namespace leaves Runtime/Reader unchanged. No trim # or registry disposal is performed. def skip(~S: Data,~C: Type,~R: Type,~E: Data,runtime: Runtime,reader: Reader) -> Runtime & Reader: match runtime reader: case Runtime{world,+namespace,batches,positions,nextReader,tick,frameStart,boundary,capacity,dropped} Reader{+readerNamespace,+id,+expected}: skip_checked(~S,~C,~R,~E,U32.is_eq(namespace,readerNamespace),Runtime{world,namespace,batches,positions,nextReader,tick,frameStart,boundary,capacity,dropped},Reader{readerNamespace,id,expected},id) # Detached active-reader id/cursor/activation tick with unread count and lag flag. # Registry ids are separate; statuses do not grant reading or mutation authority. type ReaderStatus is Data: ReaderStatus{id: U32,cursor: Nat,registeredAt: Nat,unread: Nat,lagged: Bool} # Detached visible batch values, retained World log and activated-reader statuses # with tick/boundary. This is observation, not a restorable runtime checkpoint. type Snapshot is Data: Snapshot{visible: List<&2,E>,retained: List<&2,E>,readers: List<&2,ReaderStatus>,tick: Nat,boundary: Nat} def reader_status(~E: Data,position: Position,+batches: List<&2,Batch>,+dropped: Nat) -> ReaderStatus: match position: case Position{+id,+cursor,+registered}: ReaderStatus{id,cursor,registered,count(~E,since(~E,batches,cursor)),Nat.is_lt(Nat.max(cursor,registered),dropped)} def reader_statuses(~E: Data,positions: List<&2,Position>,+batches: List<&2,Batch>,+dropped: Nat) -> List<&2,ReaderStatus>: match positions: case []: [] case position <> rest: reader_status(~E,position,batches,dropped) <> reader_statuses(~E,rest,batches,dropped) def inspect_values(~S: Data,~C: Type,~R: Type,~E: Data,observed: W.World & List<&2,E>,+namespace: U32,+batches: List<&2,Batch>,+positions: List<&2,Position>,+nextReader: U32,+tick: Nat,+frameStart: Nat,+boundary: Nat,+capacity: Nat,+dropped: Nat) -> Runtime & Snapshot: (world,retained) = observed (Runtime{world,namespace,batches,positions,nextReader,tick,frameStart,boundary,capacity,dropped},Snapshot{since(~E,batches,boundary),retained,reader_statuses(~E,List.reverse(&2,Position,positions),batches,dropped),tick,boundary}) # Return Runtime and detached Snapshot without synchronization, trimming, frame # advance or reader acknowledgement. visible derives from batches after boundary; # retained is the actual World event log, so unsynchronized publications may differ. # Reader statuses follow reversed position order, not an inferred registry catalog. def inspect(~S: Data,~C: Type,~R: Type,~E: Data,runtime: Runtime) -> Runtime & Snapshot: match runtime: case Runtime{world,+namespace,+batches,+positions,+nextReader,+tick,+frameStart,+boundary,+capacity,+dropped}: inspect_values(~S,~C,~R,~E,W.events_view(~S,~C,~R,~E,world),namespace,batches,positions,nextReader,tick,frameStart,boundary,capacity,dropped) # Success returns Runtime after registration/position removal. Rejection returns # Runtime plus actual Reader and registry; no payload destructor is invented. type Disposal -> Input -> Sy.Outcome,Error,O>> is Type: Disposed{runtime: Runtime} DisposalRejected{runtime: Runtime,reader: Reader,registry: Sy.Registry,Error,O,runner>} def disposal_result(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,result: Sy.Disposal,Error,O,runner>,reader: Reader,+id: U32,+namespace: U32,+batches: List<&2,Batch>,+positions: List<&2,Position>,+nextReader: U32,+tick: Nat,+frameStart: Nat,+boundary: Nat,+capacity: Nat,+dropped: Nat) -> Disposal: match result: case Sy.Disposed{world}: Disposed{Runtime{world,namespace,batches,update(positions,id,tick,True{}),nextReader,tick,frameStart,boundary,capacity,dropped}} case Sy.DisposalRejected{world,registry}: DisposalRejected{Runtime{world,namespace,batches,positions,nextReader,tick,frameStart,boundary,capacity,dropped},reader,registry} def disposal_checked(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,allowed: Bool,world: W.World,reader: Reader,registry: Sy.Registry,Error,O,runner>,+id: U32,+namespace: U32,+batches: List<&2,Batch>,+positions: List<&2,Position>,+nextReader: U32,+tick: Nat,+frameStart: Nat,+boundary: Nat,+capacity: Nat,+dropped: Nat) -> Disposal: match allowed: case False{}: DisposalRejected{Runtime{world,namespace,batches,positions,nextReader,tick,frameStart,boundary,capacity,dropped},reader,registry} case True{}: disposal_result(~S,~C,~R,~E,~A,~Error,~O,~runner,Sy.dispose(~S,~C,~R,~E,~Input,~Error,~O,~runner,registry,world),reader,id,namespace,batches,positions,nextReader,tick,frameStart,boundary,capacity,dropped) def disposal_identity(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,runtime: Runtime,reader: Reader,registry: Sy.Registry,Error,O,runner>,actual: U32) -> Disposal: match runtime reader: case Runtime{world,+namespace,+batches,+positions,+nextReader,+tick,+frameStart,+boundary,+capacity,+dropped} Reader{+readerNamespace,+id,+expected}: disposal_checked(~S,~C,~R,~E,~A,~Error,~O,~runner,Bool.and(U32.is_eq(namespace,readerNamespace),U32.is_eq(actual,expected)),world,Reader{readerNamespace,id,expected},registry,id,namespace,batches,positions,nextReader,tick,frameStart,boundary,capacity,dropped) def disposal_identified(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,runtime: Runtime,reader: Reader,identified: Sy.Registry,Error,O,runner> & U32) -> Disposal: (registry,actual) = identified disposal_identity(~S,~C,~R,~E,~A,~Error,~O,~runner,runtime,reader,registry,actual) # Validate token/registry identity and dispose through Sy.dispose, removing the # reader position only after success. Return actual owners on refusal. Does not # immediately trim retained batches or dispose unrelated readers/log domains. def dispose(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,runtime: Runtime,reader: Reader,registry: Sy.Registry,Error,O,runner>) -> Disposal: disposal_identified(~S,~C,~R,~E,~A,~Error,~O,~runner,runtime,reader,Sy.identity(~S,~C,~R,~E,~Input,~Error,~O,~runner,registry)) # Gameplay receives only a closed capability over an abstract affine owner. # Closed read capability over opaque H returning detached Reading. The bound # operation defines lifecycle behavior; raw adapters remain trusted setup. type ReadAccess H & Reading> is Data: ReadAccess{} # Closed Data-publication capability threading opaque H. This type alone does # not promise transaction staging or rollback of arbitrary host side effects. type WriteAccess E -> H> is Data: WriteAccess{} # Invoke the declared closed observation, returning actual H plus Reading. # Adds no validation or acknowledgement beyond the supplied operation. def all(~H: Type,~E: Data,~observe: H -> H & Reading,_: ReadAccess,owner: H) -> H & Reading: observe(owner) # Invoke the declared publication operation with Data event and return H. # Publication timing/transaction behavior belongs to that bound provider. def emit(~H: Type,~E: Data,~publish: H -> E -> H,_: WriteAccess,owner: H,event: E) -> H: publish(owner,event) # Project copied unread values and lag from Input while retaining actual Args # inside the returned Input owner. Does not rerun or advance the reader. def input_read(~E: Data,~A: Type,input: Input) -> Input & Reading: match input: case Input{+items,+lagged,args}: (Input{items,lagged,args},Reading{items,lagged})