ordinary_app_capture_query.bend
bendvy-ecs-core@0.1.0.0 · 126 lines · 0 definitions
1import Base
2import bendvy-ecs-core@0.1.0.0/ordinary_app_execution_recipe.bend as Exec
3import bendvy-ecs-core@0.1.0.0/execution_context.bend as Ctx
4import bendvy-ecs-core@0.1.0.0/schedule_provision.bend as SP
5import bendvy-ecs-core@0.1.0.0/captured_schedule_composition.bend as Choice
6import bendvy-ecs-core@0.1.0.0/ordinary_app_composer.bend as Build
7import bendvy-ecs-core@0.1.0.0/ordinary_app_registration_fields.bend as Records
8import bendvy-ecs-core@0.1.0.0/ordinary_app_records.bend as Leaf
9import bendvy-ecs-core@0.1.0.0/world.bend as W
10import bendvy-ecs-core@0.1.0.0/system.bend as Sys
11import bendvy-ecs-core@0.1.0.0/transaction.bend as T
12import bendvy-ecs-core@0.1.0.0/compose.bend as Q
13import bendvy-ecs-core@0.1.0.0/ordinary_capture_query.bend as O
14import bendvy-ecs-core@0.1.0.0/ordinary_query_declaration.bend as Decl
15import bendvy-ecs-core@0.1.0.0/ordinary_app_capture_query_fields.bend as Fields
16import bendvy-ecs-core@0.1.0.0/ordinary_app_observation_data.bend as DTO
17import bendvy-ecs-core@0.1.0.0/trace_facts.bend as Facts
18import bendvy-ecs-core@0.1.0.0/ordinary_app_recipe.bend as Tree
19
20# The actual affine capture-query owner is the invocation token. Fields are
21# refreshed from that owner after each execution, including its real cursor.
22# The registered owner of a repeatable query with affine capture L.
23# Retain the actual returned owner, including its capture and reader cursor;
24# metadata alone cannot replace its authority. Normally stored in Invocation.
25def Owned(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~Ops:Type -> Type,~Args:Data,~Error:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> Args -> H -> H & (L & (T.Outcome<Error> & Out))) -> Type:O.Owner<S,C,R,E,L,Ops,Args,Error,Out,query,body>
26# An App-owned captured-query invocation: registered owner, repeatable Args,
27# and latest row outputs. Thread the returned invocation through every update;
28# use arguments, outputs and with_arguments rather than reconstructing tokens.
29def Invocation(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~Ops:Type -> Type,~Args:Data,~Error:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> Args -> H -> H & (L & (T.Outcome<Error> & Out))) -> Type:Exec.Invocation<S,Owned(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body),Args,Out>
30# Failure of this captured App leaf. CallbackFailure wraps the query's access
31# or user error; RegistrationRefused reports rejected execution authority.
32# A failure result still returns the context and captured-query owner.
33type ExecutionError<Error:Data> is Data:
34 CallbackFailure{error:Q.Error<Error>}
35 RegistrationRefused{}
36def execution_extracted(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~Ops:Type -> Type,~Args:Data,~Error:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> Args -> H -> H & (L & (T.Outcome<Error> & Out)),args:Args,outputs:List<&2,Out>,outcome:Sys.Outcome<Ctx.Context<W.World<S,C,R,E>>,ExecutionError<Error>,Unit>,result:Owned(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body) & Records.Fields<S>) -> Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body) & Sys.Outcome<Ctx.Context<W.World<S,C,R,E>>,ExecutionError<Error>,Unit>:
37 match result:
38 case (owner,fields):(Exec.Invocation{Leaf.Record{owner,fields},args,outputs},outcome)
39def execution_returned(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~Ops:Type -> Type,~Args:Data,~Error:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> Args -> H -> H & (L & (T.Outcome<Error> & Out)),args:Args,trace:Facts.State,result:O.Run<W.World<S,C,R,E>,Owned(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body),Args,Q.Error<Error>,Out>) -> Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body) & Sys.Outcome<Ctx.Context<W.World<S,C,R,E>>,ExecutionError<Error>,Unit>:
40 match result:
41 case O.Completed{world,owner,Done{outputs}}:execution_extracted(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body,args,outputs,Sys.Succeeded{Ctx.Context{world,trace},Unit{}},Fields.fields(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body,owner))
42 case O.Completed{world,owner,Fail{error}}:execution_extracted(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body,args,[],Sys.Failed{Ctx.Context{world,trace},CallbackFailure{error}},Fields.fields(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body,owner))
43 case O.Refused{world,owner,returnedArgs}:execution_extracted(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body,returnedArgs,[],Sys.Failed{Ctx.Context{world,trace},RegistrationRefused{}},Fields.fields(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body,owner))
44def execution_allowed(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~Ops:Type -> Type,~Args:Data,~Error:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> Args -> H -> H & (L & (T.Outcome<Error> & Out)),allowed:Bool,invocation:Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body),context:Ctx.Context<W.World<S,C,R,E>>) -> Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body) & Sys.Outcome<Ctx.Context<W.World<S,C,R,E>>,ExecutionError<Error>,Unit>:
45 match allowed invocation context:
46 case False{} invocation context:(invocation,Sys.Failed{context,RegistrationRefused{}})
47 case True{} Exec.Invocation{Leaf.Record{owner,_},+args,_} Ctx.Context{world,trace}:execution_returned(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body,args,trace,O.run(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body,owner,world,args))
48def execution_run(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~Ops:Type -> Type,~Args:Data,~Error:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> Args -> H -> H & (L & (T.Outcome<Error> & Out)),invocation:Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body),context:Ctx.Context<W.World<S,C,R,E>>,id:U32) -> Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body) & Sys.Outcome<Ctx.Context<W.World<S,C,R,E>>,ExecutionError<Error>,Unit>:
49 match invocation:
50 case Exec.Invocation{Leaf.Record{owner,+fields},args,outputs}:execution_allowed(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body,U32.is_eq(id,Exec.registration_id(~S,fields)),Exec.Invocation{Leaf.Record{owner,fields},args,outputs},context)
51# Executable App type after appending a query with retained affine capture.
52# The new invocation is paired with prior owners; earlier inspection rows remain.
53# The polymorphic body receives declared Ops and must return both its opaque
54# frame and capture on every branch. Args, Error and Out are repeatable Data.
55# See examples/captured-query/app.bend for named aliases and a complete App.
56def Application(~S:Data,~C:Type,~R:Type,~E:Data,~Owners:Type,~Rows:Data,~observe:W.World<S,C,R,E> -> W.World<S,C,R,E> & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Records.Fields<S>>,~PriorError:Data,~priorPlan:Owners -> Owners & SP.Plan<S>,~priorDispatch:Owners -> Ctx.Context<W.World<S,C,R,E>> -> U32 -> Owners & Sys.Outcome<Ctx.Context<W.World<S,C,R,E>>,PriorError,Unit>,~priorProvide:W.World<S,C,R,E> -> W.World<S,C,R,E> & List<&2,SP.Requirement<S>>,~L:Type,~Ops:Type -> Type,~Args:Data,~Error:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> Args -> H -> H & (L & (T.Outcome<Error> & Out))) -> Type:
57 Exec.Registered<S,W.World<S,C,R,E>,Owners & Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body),Rows,observe,Leaf.fold_pair(~S,~Owners,~Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body),~fold,~Exec.invocation_fields(~S,~Owned(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body),~Args,~Out)),Choice.Choice<PriorError,ExecutionError<Error>>,Exec.plan_pair(~S,~Owners,~Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body),~priorPlan,~Exec.invocation_plan(~S,~Owned(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body),~Args,~Out)),Exec.dispatch_pair(~S,~W.World<S,C,R,E>,~Owners,~Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body),~PriorError,~ExecutionError<Error>,~fold,~priorDispatch,~execution_run(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body)),Exec.provision_pair(~S,~W.World<S,C,R,E>,~priorProvide,~Exec.empty_provide(~S,~W.World<S,C,R,E>))>
58def added_extracted(~S:Data,~C:Type,~R:Type,~E:Data,~Owners:Type,~Rows:Data,~observe:W.World<S,C,R,E> -> W.World<S,C,R,E> & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Records.Fields<S>>,~PriorError:Data,~priorPlan:Owners -> Owners & SP.Plan<S>,~priorDispatch:Owners -> Ctx.Context<W.World<S,C,R,E>> -> U32 -> Owners & Sys.Outcome<Ctx.Context<W.World<S,C,R,E>>,PriorError,Unit>,~priorProvide:W.World<S,C,R,E> -> W.World<S,C,R,E> & List<&2,SP.Requirement<S>>,~L:Type,~Ops:Type -> Type,~Args:Data,~Error:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> Args -> H -> H & (L & (T.Outcome<Error> & Out)),~Output:Type,args:Args,done:Application(~S,~C,~R,~E,~Owners,~Rows,~observe,~fold,~PriorError,~priorPlan,~priorDispatch,~priorProvide,~L,~Ops,~Args,~Error,~Out,~query,~body) -> Output,world:W.World<S,C,R,E>,owners:Owners,result:Owned(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body) & Records.Fields<S>,recipe:Tree.Recipe<W.World<S,C,R,E>,Rows,observe>,debug:Bool) -> Output:
59 match result:
60 case (owner,fields):done(Exec.Registered{Build.Registered{world,(owners,Exec.Invocation{Leaf.Record{owner,fields},args,[]}),recipe,debug},Exec.Recipe{}})
61def added(~S:Data,~C:Type,~R:Type,~E:Data,~Owners:Type,~Rows:Data,~observe:W.World<S,C,R,E> -> W.World<S,C,R,E> & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Records.Fields<S>>,~PriorError:Data,~priorPlan:Owners -> Owners & SP.Plan<S>,~priorDispatch:Owners -> Ctx.Context<W.World<S,C,R,E>> -> U32 -> Owners & Sys.Outcome<Ctx.Context<W.World<S,C,R,E>>,PriorError,Unit>,~priorProvide:W.World<S,C,R,E> -> W.World<S,C,R,E> & List<&2,SP.Requirement<S>>,~L:Type,~Ops:Type -> Type,~Args:Data,~Error:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> Args -> H -> H & (L & (T.Outcome<Error> & Out)),~Output:Type,args:Args,done:Application(~S,~C,~R,~E,~Owners,~Rows,~observe,~fold,~PriorError,~priorPlan,~priorDispatch,~priorProvide,~L,~Ops,~Args,~Error,~Out,~query,~body) -> Output,failed:Exec.Registered<S,W.World<S,C,R,E>,Owners,Rows,observe,fold,PriorError,priorPlan,priorDispatch,priorProvide> -> L -> Output,result:Fields.Appended<Build.Registered<S,W.World<S,C,R,E>,Owners,Rows,observe,fold>,Build.Registered<S,W.World<S,C,R,E>,Owners & Owned(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body),Rows,observe,Leaf.fold_pair(~S,~Owners,~Owned(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body),~fold,~Fields.fold(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body))>,L>) -> Output:
62 match result:
63 case Fields.Appended{Build.Registered{world,(owners,owner),recipe,debug}}:added_extracted(~S,~C,~R,~E,~Owners,~Rows,~observe,~fold,~PriorError,~priorPlan,~priorDispatch,~priorProvide,~L,~Ops,~Args,~Error,~Out,~query,~body,~Output,args,done,world,owners,Fields.fields(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body,owner),recipe,debug)
64 case Fields.AppendRejected{app,capture}:failed(Exec.Registered{app,Exec.Recipe{}},capture)
65# Register a captured query during App setup, consuming value and capture.
66# On success done receives the extended App. On refusal failed receives the
67# returned prior App AND capture, so the caller can recover or explicitly drop
68# those affine owners. args is retained for future invocations; name follows
69# the ordinary registry rules. No callback runs during this registration.
70# During execution, failure rolls back transactional writes and clears row
71# outputs, but retains the callback-returned capture: capture mutation is not
72# ECS rollback. Failure/refusal do not advance the query reader cursor; success does.
73# Deferred structural commands still need the ordinary explicit barrier.
74# Runnable example: bash examples/captured-query/run.sh js (or native).
75# Its app.bend:add_second shows add; body.bend returns all owners on failure.
76def add(~S:Data,~C:Type,~R:Type,~E:Data,~Owners:Type,~Rows:Data,~observe:W.World<S,C,R,E> -> W.World<S,C,R,E> & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Records.Fields<S>>,~PriorError:Data,~priorPlan:Owners -> Owners & SP.Plan<S>,~priorDispatch:Owners -> Ctx.Context<W.World<S,C,R,E>> -> U32 -> Owners & Sys.Outcome<Ctx.Context<W.World<S,C,R,E>>,PriorError,Unit>,~priorProvide:W.World<S,C,R,E> -> W.World<S,C,R,E> & List<&2,SP.Requirement<S>>,~L:Type,~Ops:Type -> Type,~Args:Data,~Error:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> Args -> H -> H & (L & (T.Outcome<Error> & Out)),~Output:Type,done:Application(~S,~C,~R,~E,~Owners,~Rows,~observe,~fold,~PriorError,~priorPlan,~priorDispatch,~priorProvide,~L,~Ops,~Args,~Error,~Out,~query,~body) -> Output,failed:Exec.Registered<S,W.World<S,C,R,E>,Owners,Rows,observe,fold,PriorError,priorPlan,priorDispatch,priorProvide> -> L -> Output,value:Exec.Registered<S,W.World<S,C,R,E>,Owners,Rows,observe,fold,PriorError,priorPlan,priorDispatch,priorProvide>,args:Args,name:String,capture:L) -> Output:
77 match value:
78 case Exec.Registered{app,_}:added(~S,~C,~R,~E,~Owners,~Rows,~observe,~fold,~PriorError,~priorPlan,~priorDispatch,~priorProvide,~L,~Ops,~Args,~Error,~Out,~query,~body,~Output,args,done,failed,Fields.register(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body,~Owners,~Rows,~observe,~fold,app,name,capture))
79# Advanced App-composition type projection: prior owners plus this Invocation.
80# Use the complete matching execution_* family to name a composed App.
81def execution_owners(~S:Data,~C:Type,~R:Type,~E:Data,~Owners:Type,~Rows:Data,~observe:W.World<S,C,R,E> -> W.World<S,C,R,E> & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Records.Fields<S>>,~PriorError:Data,~priorPlan:Owners -> Owners & SP.Plan<S>,~priorDispatch:Owners -> Ctx.Context<W.World<S,C,R,E>> -> U32 -> Owners & Sys.Outcome<Ctx.Context<W.World<S,C,R,E>>,PriorError,Unit>,~priorProvide:W.World<S,C,R,E> -> W.World<S,C,R,E> & List<&2,SP.Requirement<S>>,~L:Type,~Ops:Type -> Type,~Args:Data,~Error:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> Args -> H -> H & (L & (T.Outcome<Error> & Out))) -> Type:
82 Owners & Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body)
83# Advanced App-composition type projection: prior inspection rows, unchanged.
84# Captured row results are read with outputs, not added to inspection here.
85def execution_rows(~S:Data,~C:Type,~R:Type,~E:Data,~Owners:Type,~Rows:Data,~observe:W.World<S,C,R,E> -> W.World<S,C,R,E> & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Records.Fields<S>>,~PriorError:Data,~priorPlan:Owners -> Owners & SP.Plan<S>,~priorDispatch:Owners -> Ctx.Context<W.World<S,C,R,E>> -> U32 -> Owners & Sys.Outcome<Ctx.Context<W.World<S,C,R,E>>,PriorError,Unit>,~priorProvide:W.World<S,C,R,E> -> W.World<S,C,R,E> & List<&2,SP.Requirement<S>>,~L:Type,~Ops:Type -> Type,~Args:Data,~Error:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> Args -> H -> H & (L & (T.Outcome<Error> & Out))) -> Data:
86 Rows
87# Advanced App-composition projection: retain the prior World observer.
88# This captured leaf does not add a separate inspection-query projection.
89def execution_observe(~S:Data,~C:Type,~R:Type,~E:Data,~Owners:Type,~Rows:Data,~observe:W.World<S,C,R,E> -> W.World<S,C,R,E> & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Records.Fields<S>>,~PriorError:Data,~priorPlan:Owners -> Owners & SP.Plan<S>,~priorDispatch:Owners -> Ctx.Context<W.World<S,C,R,E>> -> U32 -> Owners & Sys.Outcome<Ctx.Context<W.World<S,C,R,E>>,PriorError,Unit>,~priorProvide:W.World<S,C,R,E> -> W.World<S,C,R,E> & List<&2,SP.Requirement<S>>,~L:Type,~Ops:Type -> Type,~Args:Data,~Error:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> Args -> H -> H & (L & (T.Outcome<Error> & Out))) -> W.World<S,C,R,E> -> W.World<S,C,R,E> & DTO.Observation<Rows>:
90 observe
91# Advanced App-composition projection: collect live registration fields while
92# returning every owner, including this query's actual reader cursor.
93def execution_fold(~S:Data,~C:Type,~R:Type,~E:Data,~Owners:Type,~Rows:Data,~observe:W.World<S,C,R,E> -> W.World<S,C,R,E> & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Records.Fields<S>>,~PriorError:Data,~priorPlan:Owners -> Owners & SP.Plan<S>,~priorDispatch:Owners -> Ctx.Context<W.World<S,C,R,E>> -> U32 -> Owners & Sys.Outcome<Ctx.Context<W.World<S,C,R,E>>,PriorError,Unit>,~priorProvide:W.World<S,C,R,E> -> W.World<S,C,R,E> & List<&2,SP.Requirement<S>>,~L:Type,~Ops:Type -> Type,~Args:Data,~Error:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> Args -> H -> H & (L & (T.Outcome<Error> & Out))) -> (Owners & Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body)) -> (Owners & Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body)) & List<&2,Records.Fields<S>>:
94 Leaf.fold_pair(~S,~Owners,~Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body),~fold,~Exec.invocation_fields(~S,~Owned(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body),~Args,~Out))
95# Advanced App-composition projection: build the combined registration plan
96# without consuming or executing the retained captures.
97def execution_plan(~S:Data,~C:Type,~R:Type,~E:Data,~Owners:Type,~Rows:Data,~observe:W.World<S,C,R,E> -> W.World<S,C,R,E> & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Records.Fields<S>>,~PriorError:Data,~priorPlan:Owners -> Owners & SP.Plan<S>,~priorDispatch:Owners -> Ctx.Context<W.World<S,C,R,E>> -> U32 -> Owners & Sys.Outcome<Ctx.Context<W.World<S,C,R,E>>,PriorError,Unit>,~priorProvide:W.World<S,C,R,E> -> W.World<S,C,R,E> & List<&2,SP.Requirement<S>>,~L:Type,~Ops:Type -> Type,~Args:Data,~Error:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> Args -> H -> H & (L & (T.Outcome<Error> & Out))) -> (Owners & Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body)) -> (Owners & Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body)) & SP.Plan<S>:
98 Exec.plan_pair(~S,~Owners,~Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body),~priorPlan,~Exec.invocation_plan(~S,~Owned(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body),~Args,~Out))
99# Advanced App-composition projection: dispatch a registered ID and return all
100# owners/context on success or failure. Prefer App schedule/run in gameplay;
101# use the library-generated registration IDs and reader cursors.
102def execution_dispatch(~S:Data,~C:Type,~R:Type,~E:Data,~Owners:Type,~Rows:Data,~observe:W.World<S,C,R,E> -> W.World<S,C,R,E> & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Records.Fields<S>>,~PriorError:Data,~priorPlan:Owners -> Owners & SP.Plan<S>,~priorDispatch:Owners -> Ctx.Context<W.World<S,C,R,E>> -> U32 -> Owners & Sys.Outcome<Ctx.Context<W.World<S,C,R,E>>,PriorError,Unit>,~priorProvide:W.World<S,C,R,E> -> W.World<S,C,R,E> & List<&2,SP.Requirement<S>>,~L:Type,~Ops:Type -> Type,~Args:Data,~Error:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> Args -> H -> H & (L & (T.Outcome<Error> & Out))) -> (Owners & Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body)) -> Ctx.Context<W.World<S,C,R,E>> -> U32 -> (Owners & Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body)) & Sys.Outcome<Ctx.Context<W.World<S,C,R,E>>,Choice.Choice<PriorError,ExecutionError<Error>>,Unit>:
103 Exec.dispatch_pair(~S,~W.World<S,C,R,E>,~Owners,~Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body),~PriorError,~ExecutionError<Error>,~fold,~priorDispatch,~execution_run(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body))
104# Advanced App-composition projection: combine prior provisioning requirements.
105# This query leaf adds no host-service provisioner and returns the World owner.
106def execution_provide(~S:Data,~C:Type,~R:Type,~E:Data,~Owners:Type,~Rows:Data,~observe:W.World<S,C,R,E> -> W.World<S,C,R,E> & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Records.Fields<S>>,~PriorError:Data,~priorPlan:Owners -> Owners & SP.Plan<S>,~priorDispatch:Owners -> Ctx.Context<W.World<S,C,R,E>> -> U32 -> Owners & Sys.Outcome<Ctx.Context<W.World<S,C,R,E>>,PriorError,Unit>,~priorProvide:W.World<S,C,R,E> -> W.World<S,C,R,E> & List<&2,SP.Requirement<S>>,~L:Type,~Ops:Type -> Type,~Args:Data,~Error:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> Args -> H -> H & (L & (T.Outcome<Error> & Out))) -> W.World<S,C,R,E> -> W.World<S,C,R,E> & List<&2,SP.Requirement<S>>:
107 Exec.provision_pair(~S,~W.World<S,C,R,E>,~priorProvide,~Exec.empty_provide(~S,~W.World<S,C,R,E>))
108
109# Read the repeatable arguments without losing the invocation.
110# Returns (invocation, args); the affine capture remains inside the invocation.
111def arguments(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~Ops:Type -> Type,~Args:Data,~Error:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> Args -> H -> H & (L & (T.Outcome<Error> & Out)),value:Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body)) -> Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body) & Args:
112 match value:
113 case Exec.Invocation{record,+args,outputs}:(Exec.Invocation{record,args,outputs},args)
114# Read this invocation's latest per-row outputs, preserving the invocation.
115# A callback failure or Refused returned by the captured query runner clears
116# outputs. An early ID-routing refusal returns the invocation and its previous
117# outputs unchanged. Values are Data, not capture owners.
118def outputs(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~Ops:Type -> Type,~Args:Data,~Error:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> Args -> H -> H & (L & (T.Outcome<Error> & Out)),value:Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body)) -> Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body) & List<&2,Out>:
119 match value:
120 case Exec.Invocation{record,args,+outputs}:(Exec.Invocation{record,args,outputs},outputs)
121# Replace repeatable invocation arguments, preserving capture, cursor and the
122# stored output list. Does not run the query or reset failure visibility.
123# See examples/captured-query/app.bend's retry path with Entry.update_owners.
124def with_arguments(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~Ops:Type -> Type,~Args:Data,~Error:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> Args -> H -> H & (L & (T.Outcome<Error> & Out)),value:Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body),args:Args) -> Invocation(~S,~C,~R,~E,~L,~Ops,~Args,~Error,~Out,~query,~body):
125 match value:
126 case Exec.Invocation{record,_,outputs}:Exec.Invocation{record,args,outputs}