import Base import machine.bend as M import event_runtime.bend as Ev import machine_stream_queue.bend as Q # FIFO metadata: front oldest-first, back newest-first; size counts events. type Stream is Data: Stream{queue: Q.Queue>,positions: List<&2,Ev.Position>,frameStart: Nat} type Reader is Type: Reader{namespace: U32,id: U32} def empty(~V: Data) -> Stream: Stream{Q.empty(~M.Transition),[],0n} def publish(~V: Data,stream: Stream,tick: Nat,event: M.Transition) -> Stream: match stream: case Stream{queue,positions,frameStart}: Stream{Q.publish(~M.Transition,queue,tick,[event]),positions,frameStart} def activate(~V: Data,stream: Stream,id: U32,tick: Nat) -> Stream: match stream: case Stream{queue,positions,frameStart}: Stream{queue,Ev.activate(positions,id,tick),frameStart} def complete(~V: Data,stream: Stream,id: U32,tick: Nat,success: Bool) -> Stream: match stream success: case Stream{queue,positions,frameStart} True{}: Stream{queue,Ev.update(positions,id,tick,False{}),frameStart} case stream False{}: stream # A condition-false system never executes its reader parameter. def skip(~V: Data,stream: Stream,id: U32,tick: Nat) -> Stream: stream # Bevy insert_state replaces messages while preserving reader positions and loss. def initial_queue(~V:Data,queue:Q.Queue>,tick:Nat,value:V) -> Q.Queue>: match queue: case Q.Queue{_,_,_,dropped,_}:Q.Queue{[],[Q.Batch{tick,1n,[M.Initial{value}]}],1n,dropped,1n} def initial(~V:Data,stream:Stream,tick:Nat,value:V) -> Stream: match stream: case Stream{queue,positions,frameStart}:Stream{initial_queue(~V,queue,tick,value),positions,frameStart} def observe_position(~V: Data,position: Maybe,batches: List<&2,Ev.Batch>>,dropped: Nat,id: U32,tick: Nat) -> Ev.Reading>: match position: case None{}: Ev.reading(~M.Transition,Ev.Position{id,0n,tick},batches,dropped) case Some{position}: Ev.reading(~M.Transition,position,batches,dropped) def observe_registered(~V: Data,position: Ev.Position,+queue: Q.Queue>) -> Ev.Reading>: match position queue: case Ev.Position{_,+cursor,registered} Q.Queue{+front,+back,+size,+dropped,+batches}: Ev.Reading{Q.read(~M.Transition,Q.Queue{front,back,size,dropped,batches},cursor),Nat.is_lt(Nat.max(cursor,registered),dropped)} def observe_queue_position(~V: Data,position: Maybe,queue: Q.Queue>,id: U32,tick: Nat) -> Ev.Reading>: match position: case None{}: observe_registered(~V,Ev.Position{id,0n,tick},queue) case Some{position}: observe_registered(~V,position,queue) def observe(~V: Data,stream: Stream,+id: U32,tick: Nat) -> Ev.Reading>: match stream: case Stream{queue,positions,_}: observe_queue_position(~V,Ev.lookup(positions,id),queue,id,tick) def batches_into_queue(~V:Data,batches:List<&2,Ev.Batch>>,queue:Q.Queue>) -> Q.Queue>: match batches: case []:queue case Ev.Batch{tick,+items} <> rest:batches_into_queue(~V,rest,Q.publish_nonempty(~M.Transition,queue,tick,items,Q.count_loop(~M.Transition,items,0n))) def trimmed(~V:Data,frameStart:Nat,positions:List<&2,Ev.Position>,result:Ev.Trimmed>) -> Stream: match result: case Ev.Trimmed{batches,dropped}:Stream{batches_into_queue(~V,batches,Q.Queue{[],[],0n,dropped,0n}),positions,frameStart} def frame(~V: Data,stream: Stream,tick: Nat) -> Stream: match stream: case Stream{queue,+positions,frameStart}: Stream{Q.trim(~M.Transition,queue,Ev.hold_boundary(positions,frameStart),65536n),positions,tick} def dispose(~V: Data,stream: Stream,id: U32) -> Stream: match stream: case Stream{queue,positions,frameStart}: Stream{queue,Ev.update(positions,id,0n,True{}),frameStart} # Canonical snapshot includes every retained batch/payload, without changing cursor. def batches_loop(~V: Data,batches: List<&2,Q.Batch>>,acc: List<&2,Ev.Batch>>) -> List<&2,Ev.Batch>>: match batches: case []: List.reverse(&2,Ev.Batch>,acc) case Q.Batch{tick,_,items} <> rest: batches_loop(~V,rest,Ev.Batch{tick,items} <> acc) def snapshot_queue(~V: Data,queue: Q.Queue>,positions: List<&2,Ev.Position>,frameStart: Nat) -> List<&2,Ev.Batch>> & (List<&2,Ev.Position> & (Nat & Nat)): match queue: case Q.Queue{front,back,_,dropped,_}: (batches_loop(~V,Ev.append(~Q.Batch>,front,List.reverse(&2,Q.Batch>,back)),[]),(positions,(dropped,frameStart))) def snapshot(~V: Data,stream: Stream) -> List<&2,Ev.Batch>> & (List<&2,Ev.Position> & (Nat & Nat)): match stream: case Stream{queue,positions,frameStart}: snapshot_queue(~V,queue,positions,frameStart) # Engine entry points consume and return the real affine reader capability. # A foreign-world refusal preserves both owners and never activates a cursor. type OpenResult is Type: Opened{reader: Reader,stream: Stream,reading: Ev.Reading>} ReaderRefused{reader: Reader,stream: Stream} def open_checked(~S: Data,~M: Data,~V: Data,allowed: Bool,reader: Reader,+stream: Stream,+tick: Nat) -> OpenResult: match allowed reader: case False{}, reader: ReaderRefused{reader,stream} case True{}, Reader{namespace,+id}: Opened{Reader{namespace,id},activate(~V,stream,id,tick),observe(~V,stream,id,tick)} def open(~S: Data,~M: Data,~V: Data,reader: Reader,stream: Stream,namespace: U32,tick: Nat) -> OpenResult: match reader: case Reader{+owner,id}: open_checked(~S,~M,~V,U32.is_eq(owner,namespace),Reader{owner,id},stream,tick) def finish(~S: Data,~M: Data,~V: Data,reader: Reader,stream: Stream,tick: Nat,success: Bool) -> Reader & Stream: match reader: case Reader{namespace,+id}: (Reader{namespace,id},complete(~V,stream,id,tick,success)) def skip_owned(~S: Data,~M: Data,~V: Data,reader: Reader,stream: Stream,tick: Nat) -> Reader & Stream: match reader: case Reader{namespace,+id}: (Reader{namespace,id},skip(~V,stream,id,tick))