2424 * 6. **What stays as it was**: a readable id starts the run (the firing
2525 * control for every zero above), a read the definition does not declare,
2626 * the system principal.
27+ * 7. **An input the author declares a record id** (`objectName` on the
28+ * variable): loaded from the declared object at every door, read in plain
29+ * sight or not, beside any visible read of another object, followed into a
30+ * callee; the same input undeclared keeps today's rule (the control).
2731 *
2832 * The data double is ROW-SCOPED per object: each row is visible to its owner
2933 * and to nobody else, and no other id exists, so the real
@@ -84,6 +88,18 @@ const BY_COMPUTED = 'contract_by_computed';
8488const SCREEN_EDIT = 'contract_screen_edit' ;
8589const SUBFLOW_PARENT = 'contract_subflow_parent' ;
8690const MAP_PARENT = 'contract_map_parent' ;
91+ const DECLARED = 'contract_declared' ;
92+ const UNDECLARED = 'contract_undeclared' ;
93+ const DECLARED_AND_READ = 'case_declared_contract_read' ;
94+ const DECLARED_CALLEE_PARENT = 'contract_declared_callee_parent' ;
95+
96+ /** A flow whose start input `name` the author declares a record id of `object` (`objectName`). */
97+ function declaring ( def : FlowDef , input : string , object : string ) : FlowDef {
98+ return {
99+ ...def ,
100+ variables : ( def . variables as Array < Record < string , unknown > > ) . map ( ( v ) => ( v . name === input ? { ...v , objectName : object } : v ) ) ,
101+ } ;
102+ }
87103
88104const FLOWS : Record < string , FlowDef > = {
89105 [ BY_RECORD ] : flow ( BY_RECORD , [ 'recordId' ] , [ getRow ( CONTRACT , { id : cel ( 'recordId' ) } ) ] , { runAs : 'system' } ) ,
@@ -99,6 +115,19 @@ const FLOWS: Record<string, FlowDef> = {
99115 [ MAP_PARENT ] : flow ( MAP_PARENT , [ 'contractIds' ] , [
100116 { id : 'each' , type : 'map' , label : 'Each' , config : { collection : cel ( 'contractIds' ) , flowName : BY_CUSTOM , input : { contractId : cel ( 'item' ) } } } ,
101117 ] ) ,
118+ // The declared input is read by no node: only the declaration says it is a record id.
119+ [ DECLARED ] : declaring ( flow ( DECLARED , [ 'contractId' ] , [ ] , { runAs : 'system' } ) , 'contractId' , CONTRACT ) ,
120+ // The control: the same flow without the declaration.
121+ [ UNDECLARED ] : flow ( UNDECLARED , [ 'contractId' ] , [ ] , { runAs : 'system' } ) ,
122+ // Declared an id of `crm_case`, and read in plain sight as an id of `crm_contract`.
123+ [ DECLARED_AND_READ ] : declaring (
124+ flow ( DECLARED_AND_READ , [ 'subjectId' ] , [ getRow ( CONTRACT , { id : cel ( 'subjectId' ) } ) ] , { runAs : 'system' } ) ,
125+ 'subjectId' , CASE ,
126+ ) ,
127+ // Hands its own undeclared input to the declared input of a callee.
128+ [ DECLARED_CALLEE_PARENT ] : flow ( DECLARED_CALLEE_PARENT , [ 'target' ] , [
129+ { id : 'sub' , type : 'subflow' , label : 'Sub' , config : { flowName : DECLARED , input : { contractId : cel ( 'target' ) } } } ,
130+ ] ) ,
102131} ;
103132
104133/** The acting principal, as `resolveExecutionContext` builds one. */
@@ -115,7 +144,13 @@ const CUSTOM_ACTION = {
115144 params : [ { name : 'contractId' , label : 'Contract' , type : 'text' } ] ,
116145} ;
117146const SCRIPT_ACTION = { name : 'touch_case' , label : 'Touch' , objectName : CASE , type : 'script' , execute : 'return 1' } ;
118- const CASE_DEF = { name : CASE , actions : [ CONTRACT_ACTION , CUSTOM_ACTION , SCRIPT_ACTION ] } ;
147+ const DECLARED_ACTION = {
148+ name : 'open_declared_contract' , label : 'Open declared contract' , objectName : CASE , type : 'flow' , target : DECLARED ,
149+ params : [ { name : 'contractId' , label : 'Contract' , type : 'text' } ] ,
150+ ai : { exposed : true , description : 'Open the declared contract.' } ,
151+ } ;
152+ const UNDECLARED_ACTION = { ...DECLARED_ACTION , name : 'open_undeclared_contract' , label : 'Open undeclared contract' , target : UNDECLARED } ;
153+ const CASE_DEF = { name : CASE , actions : [ CONTRACT_ACTION , CUSTOM_ACTION , SCRIPT_ACTION , DECLARED_ACTION , UNDECLARED_ACTION ] } ;
119154const CONTRACT_DEF = { name : CONTRACT , actions : [ ] } ;
120155const schemaOf = ( n : string ) => ( n === CASE ? CASE_DEF : n === CONTRACT ? CONTRACT_DEF : undefined ) ;
121156
@@ -574,3 +609,174 @@ describe('the REST action door refuses a routed record that is not one id, befor
574609 expect ( reads ( h ) . length ) . toBeGreaterThan ( 0 ) ;
575610 } ) ;
576611} ) ;
612+
613+ // ───────────────────────────────────────────────────────────────────────────
614+ // 7. An input the author declares a record id (`objectName`, #29425 ruling A)
615+ // ───────────────────────────────────────────────────────────────────────────
616+
617+ describe ( 'a start input declared a record id is loaded from the declared object at every door, before the run exists' , ( ) => {
618+ const readsOf = ( name : string ) =>
619+ flowDeclaredRecordReads ( { getFlow : async ( n : string ) => FLOWS [ n ] ?? null } , name ) . then ( ( m ) => Object . fromEntries ( m ) ) ;
620+
621+ /** The declared `type: 'flow'` endpoint door, on the real `callData`. */
622+ async function endpoint ( target : string , body : unknown ) {
623+ const declared = ApiEndpointSchema . parse ( {
624+ name : `${ target } _open` , path : `/api/v1/apps/${ target } /open` , method : 'POST' , type : 'flow' , target,
625+ } ) ;
626+ const ql = makeQl ( ) ;
627+ const automation = makeAutomation ( ) ;
628+ const deps : any = { resolveService : async ( ) => undefined , getObjectQL : async ( ) => ql } ;
629+ const protocolContext : any = { request : { } , environmentId : 'platform' , executionContext : ec ( MEMBER ) } ;
630+ const answer = await executeEndpointTarget (
631+ buildEndpointExecutionContext ( {
632+ request : { method : 'POST' , path : declared . path , query : { } , headers : { } , body } ,
633+ match : { endpoint : declared , params : { } } ,
634+ executionContext : ec ( MEMBER ) as any ,
635+ environmentId : 'platform' ,
636+ } ) ,
637+ {
638+ automationService : automation ,
639+ callData : ( action , params , dataDriver , scopeId , execCtx ) =>
640+ callData ( deps , protocolContext , action , params , dataDriver , scopeId , execCtx ) ,
641+ } ,
642+ ) ;
643+ return { status : answer . status , error : ( answer . body as any ) ?. error ?? { } , ql, automation } ;
644+ }
645+
646+ /**
647+ * One start of `flowName` with `contractId` set to `value`, at each door that
648+ * starts a flow by name, read back as status + error + what was loaded + runs.
649+ */
650+ const DOORS : Record < string , ( flowName : string , value : unknown ) => Promise < { status : unknown ; error : any ; ql : any ; automation : any } > > = {
651+ 'POST /automation/:name/trigger' : async ( flowName , value ) => {
652+ const h = boot ( ec ( MEMBER ) ) ;
653+ const res = await h . trigger ( flowName , { params : { contractId : value } } ) ;
654+ return { status : statusOf ( res ) , error : errorOf ( res ) , ql : h . ql , automation : h . automation } ;
655+ } ,
656+ 'legacy POST /automation/trigger/:name' : async ( flowName , value ) => {
657+ const h = boot ( ec ( MEMBER ) ) ;
658+ const res = await h . trigger ( flowName , { params : { contractId : value } } , true ) ;
659+ return { status : statusOf ( res ) , error : errorOf ( res ) , ql : h . ql , automation : h . automation } ;
660+ } ,
661+ 'a declared `type: flow` endpoint' : ( flowName , value ) => endpoint ( flowName , { params : { contractId : value } } ) ,
662+ 'REST POST /actions/:object/:action' : async ( flowName , value ) => {
663+ const h = boot ( ec ( MEMBER ) ) ;
664+ const action = flowName === DECLARED ? DECLARED_ACTION : UNDECLARED_ACTION ;
665+ const res = await h . actions ( `/${ CASE } /${ action . name } /${ BOTH } ` , { params : { contractId : value } } ) ;
666+ return { status : statusOf ( res ) , error : errorOf ( res ) , ql : h . ql , automation : h . automation } ;
667+ } ,
668+ 'MCP run_action' : async ( flowName , value ) => {
669+ const h = boot ( ec ( MEMBER ) ) ;
670+ const action = flowName === DECLARED ? DECLARED_ACTION : UNDECLARED_ACTION ;
671+ try {
672+ await h . runAction ( action . name , { objectName : CASE , recordId : BOTH , params : { contractId : value } } ) ;
673+ return { status : 200 , error : { } , ql : h . ql , automation : h . automation } ;
674+ } catch ( err : any ) {
675+ return { status : err ?. status , error : err ?? { } , ql : h . ql , automation : h . automation } ;
676+ }
677+ } ,
678+ } ;
679+ /** Every `crm_contract` row the start loaded. */
680+ const contractReads = ( ql : any ) => ql . find . mock . calls . filter ( ( c : any [ ] ) => c [ 0 ] === CONTRACT ) . map ( ( c : any [ ] ) => c [ 1 ] ?. where ?. id ) ;
681+
682+ it ( 'the reading: a declared input is one id of the declared object, read by no node' , async ( ) => {
683+ expect ( await readsOf ( DECLARED ) ) . toEqual ( { contractId : [ { object : CONTRACT , many : false } ] } ) ;
684+ expect ( await readsOf ( UNDECLARED ) ) . toEqual ( { } ) ;
685+ } ) ;
686+
687+ it ( 'the reading: a declaration sits beside a read in plain sight of another object, never in place of it' , async ( ) => {
688+ expect ( await readsOf ( DECLARED_AND_READ ) ) . toEqual ( {
689+ subjectId : [ { object : CASE , many : false } , { object : CONTRACT , many : false } ] ,
690+ } ) ;
691+ } ) ;
692+
693+ it ( 'the reading: a callee\'s declared input is followed through the hand-off' , async ( ) => {
694+ expect ( await readsOf ( DECLARED_CALLEE_PARENT ) ) . toEqual ( { target : [ { object : CONTRACT , many : false } ] } ) ;
695+ } ) ;
696+
697+ it ( 'the reading: a loop that rebinds the name does not withdraw the declaration' , async ( ) => {
698+ const looped = declaring ( flow ( 'looped' , [ 'x' ] , [
699+ { id : 'loop' , type : 'loop' , label : 'Loop' , config : { collection : [ 1 ] , iteratorVariable : 'x' , body : { nodes : [ getRow ( CASE , { id : cel ( 'x' ) } ) ] , edges : [ ] } } } ,
700+ ] ) , 'x' , CONTRACT ) ;
701+ const got = await flowDeclaredRecordReads ( { getFlow : async ( ) => looped } , 'looped' ) ;
702+ expect ( Object . fromEntries ( got ) ) . toEqual ( { x : [ { object : CONTRACT , many : false } ] } ) ;
703+ } ) ;
704+
705+ for ( const [ door , start ] of Object . entries ( DOORS ) ) {
706+ describe ( door , ( ) => {
707+ it ( 'a declared input naming a record the caller can read: the run starts, the firing control' , async ( ) => {
708+ const got = await start ( DECLARED , BOTH ) ;
709+
710+ expect ( got . status , JSON . stringify ( got . error ) ) . toBe ( 200 ) ;
711+ expect ( got . automation . execute ) . toHaveBeenCalledTimes ( 1 ) ;
712+ expect ( contractReads ( got . ql ) ) . toContain ( BOTH ) ;
713+ } ) ;
714+
715+ it . each ( [
716+ [ 'the caller cannot read' , CONTRACT_OTHER ] ,
717+ [ 'does not exist' , GHOST ] ,
718+ ] ) ( 'a declared input naming a record the caller %s: 404 RECORD_NOT_FOUND, no run' , async ( _label , id ) => {
719+ const got = await start ( DECLARED , id ) ;
720+
721+ expect ( got . status ) . toBe ( 404 ) ;
722+ expect ( got . error . code ) . toBe ( 'RECORD_NOT_FOUND' ) ;
723+ expect ( contractReads ( got . ql ) ) . toContain ( id ) ;
724+ expect ( got . automation . execute ) . not . toHaveBeenCalled ( ) ;
725+ } ) ;
726+
727+ it . each ( [
728+ [ 'an object' , { id : BOTH } ] ,
729+ [ 'a list' , [ BOTH ] ] ,
730+ [ 'a boolean' , true ] ,
731+ ] ) ( 'a declared input given %s: 400 VALIDATION_FAILED naming the input, nothing loaded from the declared object, no run' , async ( _label , value ) => {
732+ const got = await start ( DECLARED , value ) ;
733+
734+ expect ( got . status ) . toBe ( 400 ) ;
735+ expect ( got . error . code ) . toBe ( 'VALIDATION_FAILED' ) ;
736+ expect ( String ( got . error . message ) ) . toContain ( '`contractId`' ) ;
737+ expect ( contractReads ( got . ql ) ) . toHaveLength ( 0 ) ;
738+ expect ( got . automation . execute ) . not . toHaveBeenCalled ( ) ;
739+ } ) ;
740+
741+ it ( 'the same input undeclared, read by no node: not judged, the run starts on an id the caller cannot read (today\'s rule)' , async ( ) => {
742+ const got = await start ( UNDECLARED , CONTRACT_OTHER ) ;
743+
744+ expect ( got . status , JSON . stringify ( got . error ) ) . toBe ( 200 ) ;
745+ expect ( got . automation . execute ) . toHaveBeenCalledTimes ( 1 ) ;
746+ expect ( contractReads ( got . ql ) ) . toHaveLength ( 0 ) ;
747+ } ) ;
748+ } ) ;
749+ }
750+
751+ it ( 'declared one object and read from another: the caller must read the row in both' , async ( ) => {
752+ const inCaseOnly = boot ( ec ( MEMBER ) ) ;
753+ const refused = await inCaseOnly . trigger ( DECLARED_AND_READ , { params : { subjectId : CASE_ONLY } } ) ;
754+ expect ( statusOf ( refused ) ) . toBe ( 404 ) ;
755+ expect ( errorOf ( refused ) . code ) . toBe ( 'RECORD_NOT_FOUND' ) ;
756+ expect ( reads ( inCaseOnly ) ) . toEqual ( expect . arrayContaining ( [ [ CASE , CASE_ONLY ] , [ CONTRACT , CASE_ONLY ] ] ) ) ;
757+ expect ( inCaseOnly . automation . execute ) . not . toHaveBeenCalled ( ) ;
758+
759+ const inBoth = boot ( ec ( MEMBER ) ) ;
760+ const started = await inBoth . trigger ( DECLARED_AND_READ , { params : { subjectId : BOTH } } ) ;
761+ expect ( statusOf ( started ) , JSON . stringify ( started ?. response ?. body ) ) . toBe ( 200 ) ;
762+ expect ( inBoth . automation . execute ) . toHaveBeenCalledTimes ( 1 ) ;
763+ } ) ;
764+
765+ it ( 'a parent that hands its input to a callee\'s declared input is judged by the callee\'s declaration' , async ( ) => {
766+ const h = boot ( ec ( MEMBER ) ) ;
767+ const res = await h . trigger ( DECLARED_CALLEE_PARENT , { params : { target : CONTRACT_OTHER } } ) ;
768+
769+ expect ( statusOf ( res ) ) . toBe ( 404 ) ;
770+ expect ( errorOf ( res ) . code ) . toBe ( 'RECORD_NOT_FOUND' ) ;
771+ expect ( h . automation . execute ) . not . toHaveBeenCalled ( ) ;
772+ } ) ;
773+
774+ it ( 'the system principal starts on any declared id, nothing loaded' , async ( ) => {
775+ const h = boot ( { isSystem : true } ) ;
776+ const res = await h . trigger ( DECLARED , { params : { contractId : CONTRACT_OTHER } } ) ;
777+
778+ expect ( statusOf ( res ) ) . toBe ( 200 ) ;
779+ expect ( h . automation . execute ) . toHaveBeenCalledTimes ( 1 ) ;
780+ expect ( reads ( h ) ) . toHaveLength ( 0 ) ;
781+ } ) ;
782+ } ) ;
0 commit comments