1 import Base 2 import inspector.bend as I 3 import inspector_query_projection.bend as Q 4 import world.bend as W 5 import component.bend as C 6 import column.bend as Col 7 import capabilities.bend as Cap 8 9 # Trusted declaration setup receives a nominal cursor-free frame. The public 10 # query grant exposes neither lifecycle filters nor added/changed observations. 11 # Machine conditions belong to #55; services/streams have no grant here. 12 # Trusted cursor-free Check frame owning World. Ordinary condition bodies 13 # receive opaque H with read capabilities, not raw world constructors. 14 # No lifecycle cursor or event reader is allocated by this frame. 15 type Frame<-S:Data,-Store:Type,-R:Type,-E:Data> is Type: 16 Frame{world:W.World} 17 # Condition query membership: Required excludes absent components; Optional 18 # retains live rows with explicit absence. No Added/Changed mode is offered. 19 type PlainSelection is Data: 20 Required{} 21 Optional{} 22 # Detached condition-query row: entity handle plus component Access. 23 # Does not expose added/changed flags or an affine component payload. 24 type Row is Data: 25 Row{entity:W.Handle,value:C.Access} 26 # Preflight failure listing required runtime resources absent from the plan. 27 # Check body is not invoked when this error is returned. 28 type Error is Data: 29 MissingRuntimeRequirements{resources:List<&2,String>} 30 # Trusted setup pairing runtime-requirement observation with read grants. 31 # required must return World; arbitrary callback mutations are not isolated 32 # by the Check abstraction. Bodies receive only the declared Ops(H). 33 type Plan<-S:Data,-Store:Type,-R:Type,-E:Data,-Ops:Type -> Type> is Type: 34 Plan{required:W.World -> W.World & List<&2,String>,caps:Ops(Frame)} 35 # Check outcome retaining World with either a preflight Error or Bool. 36 # False is a successful condition result, distinct from missing requirements. 37 type Checked is Type: 38 Checked{world:W.World,result:Result<&2,&2,Error,Bool>} 39 40 def selection(value:PlainSelection) -> Q.Selection: 41 match value: 42 case Required{}:Q.Required{} 43 case Optional{}:Q.Optional{} 44 def plain_row(~S:Data,~V:Data,value:Q.Row) -> Row: 45 match value: 46 case Q.Row{entity,I.Value{access,_,_}}:Row{entity,access} 47 def plain_rows(~S:Data,~V:Data,values:List<&2,Q.Row>) -> List<&2,Row>: 48 match values: 49 case []:[] 50 case value <> rest:plain_row(~S,~V,value) <> plain_rows(~S,~V,rest) 51 def query_done(~S:Data,~Store:Type,~R:Type,~E:Data,~V:Data,result:I.Frame & List<&2,Q.Row>) -> Frame & List<&2,Row>: 52 match result: 53 case (I.Frame{world,_},rows):(Frame{world},plain_rows(~S,~V,rows)) 54 def query_read(~S:Data,~Store:Type,~R:Type,~E:Data,~Rest:Type,~Payload:Type,~V:Data,~Token:Data,~take:Store -> Rest & Col.Column,~put:Rest -> Col.Column -> Store,~project:Payload -> Payload & V,~family:C.Family,~mode:PlainSelection,frame:Frame) -> Frame & List<&2,Row>: 55 match frame: 56 case Frame{world}:query_done(~S,~Store,~R,~E,~V,Q.scan(~S,~Store,~R,~E,~Rest,~Payload,~V,~Token,~take,~put,~project,~family,~selection(mode),I.Frame{world,None{}})) 57 # Build a read-only component query for Required or Optional membership. 58 # Canonical scan returns detached Access rows in its live-handle traversal order, 59 # without lifecycle flags or consuming registered system cursors. project must 60 # preserve payload owners; raw family lenses remain trusted schema setup. 61 def query_grant(~S:Data,~Store:Type,~R:Type,~E:Data,~Rest:Type,~Payload:Type,~V:Data,~Token:Data,~take:Store -> Rest & Col.Column,~put:Rest -> Col.Column -> Store,~project:Payload -> Payload & V,~family:C.Family,~mode:PlainSelection) -> Cap.ValueRead,List<&2,Row>>: 62 Cap.ValueRead{frame => query_read(~S,~Store,~R,~E,~Rest,~Payload,~V,~Token,~take,~put,~project,~family,~mode,frame)} 63 def resource_done(~S:Data,~Store:Type,~R:Type,~E:Data,~V:Data,result:W.World & V) -> Frame & V: 64 (world,value)=result 65 (Frame{world},value) 66 def resource_read(~S:Data,~Store:Type,~R:Type,~E:Data,~V:Data,~project:R -> R & V,frame:Frame) -> Frame & V: 67 match frame: 68 case Frame{world}:resource_done(~S,~Store,~R,~E,~V,W.observe_resource(~S,~Store,~R,~E,~V,~project,world)) 69 # Build read-only access to a detached resource Data view. project threads 70 # affine R back unchanged in ownership; no replacement or event-read grant 71 # is provided and no resource projection runs during declaration construction. 72 def resource_grant(~S:Data,~Store:Type,~R:Type,~E:Data,~V:Data,~project:R -> R & V) -> Cap.ValueRead,V>: 73 Cap.ValueRead{frame => resource_read(~S,~Store,~R,~E,~V,~project,frame)} 74 75 def completed(~S:Data,~Store:Type,~R:Type,~E:Data,result:Frame & Bool) -> Checked: 76 match result: 77 case (Frame{world},value):Checked{world,Done{value}} 78 def ready(~S:Data,~Store:Type,~R:Type,~E:Data,~Ops:Type -> Type,~A:Data,~body:@-H:Type -> @-caps:Ops(H) -> A -> H -> H & Bool,~caps:Ops(Frame),args:A,result:W.World & List<&2,String>) -> Checked: 79 match result: 80 case (world,[]):completed(~S,~Store,~R,~E,Cap.invoke(~Ops,~A,~Bool,~body,~Frame,~caps,args,Frame{world})) 81 case (world,name <> rest):Checked{world,Fail{MissingRuntimeRequirements{name <> rest}}} 82 def required_field(~S:Data,~Store:Type,~R:Type,~E:Data,~Ops:Type -> Type,plan:Plan) -> W.World -> W.World & List<&2,String>: 83 match plan: 84 case Plan{required,_}:required 85 def caps_field(~S:Data,~Store:Type,~R:Type,~E:Data,~Ops:Type -> Type,plan:Plan) -> Ops(Frame): 86 match plan: 87 case Plan{_,caps}:caps 88 # Check runtime requirements, then evaluate a cursor-free Bool body through 89 # its declared capabilities. Missing requirements return Checked with Fail 90 # without invoking body; otherwise Done contains True or False. World is 91 # returned on both paths, including any changes made by trusted requirement 92 # callbacks. Check itself advances no system/lifecycle/event cursor or clock. 93 # No automatic transaction rollback applies to arbitrary setup callbacks. 94 # Condition contract and consumers: docs/parity/inspect.md. 95 def run(~S:Data,~Store:Type,~R:Type,~E:Data,~Ops:Type -> Type,~A:Data,~body:@-H:Type -> @-caps:Ops(H) -> A -> H -> H & Bool,~plan:Plan,args:A,world:W.World) -> Checked: 96 ready(~S,~Store,~R,~E,~Ops,~A,~body,~caps_field(~S,~Store,~R,~E,~Ops,plan),args,required_field(~S,~Store,~R,~E,~Ops,plan)(world))