99 * lost a race; the stored owner is the winner.
1010 *
1111 * The cells cross the two answers a lost race used to get with the two unit
12- * shapes the engine has: an object with no metadata `beforeUpdate` hook opens
13- * no unit of work for the write; one with such a hook opens the after-abort
14- * unit (`opensAfterAbortUnit`'s side-writing term). When that unit enclosed the
15- * middleware chain, the loser's whole write — the authorization gate's
12+ * shapes the engine has: an object with no metadata update hook opens no unit
13+ * of work for the write; one with a metadata `beforeUpdate` hook opens the
14+ * after-abort unit (`opensAfterAbortUnit`'s side-writing term), and so does one
15+ * whose only metadata hook is a blocking `afterUpdate` (its aborting term, the
16+ * fifth cell). When that unit enclosed the middleware chain, the loser's whole write — the authorization gate's
1617 * caller-scoped pre-image read included — waited on the single connection for
1718 * the winner's commit, and then read a row it no longer owned: `private`
1819 * answered 404 (it cannot see the row), `public_read` answered 403 (it can see
@@ -55,7 +56,9 @@ const memberOf = (userId: string) => ({
5556} ) ;
5657const QUIET = { debug ( ) { } , info ( ) { } , warn ( ) { } , error ( ) { } } ;
5758
58- async function boot ( opts : { sharingModel : 'private' | 'public_read' ; beforeHook : boolean } ) {
59+ type HookShape = 'none' | 'beforeUpdate' | 'afterUpdate' ;
60+
61+ async function boot ( opts : { sharingModel : 'private' | 'public_read' ; hook : HookShape } ) {
5962 const engine = new ObjectQL ( ) ;
6063 engine . registerDriver (
6164 new SqlDriver ( { client : 'better-sqlite3' , connection : { filename : ':memory:' } , useNullAsDefault : true } ) as never ,
@@ -70,12 +73,15 @@ async function boot(opts: { sharingModel: 'private' | 'public_read'; beforeHook:
7073 } ] ,
7174 } as never ) ;
7275 await engine . syncSchemas ( ) ;
73- if ( opts . beforeHook ) {
76+ if ( opts . hook !== 'none' ) {
7477 // A metadata-bound hook that does nothing: its presence alone is what
75- // makes the engine open the after-abort unit for this object's updates.
76- bindHooksToEngine ( engine as never , [ { name : 'cq_touch' , object : CASE , events : [ 'beforeUpdate' ] , handler : ( ) => { } } as never ] , {
77- packageId : 'app:cq' , logger : QUIET as never ,
78- } ) ;
78+ // makes the engine open the after-abort unit for this object's updates —
79+ // any `beforeUpdate` (side-writing term), or a blocking `afterUpdate` whose
80+ // `onError` is `abort` (aborting term).
81+ const hook = opts . hook === 'afterUpdate'
82+ ? { name : 'cq_after' , object : CASE , events : [ 'afterUpdate' ] , onError : 'abort' , handler : ( ) => { } }
83+ : { name : 'cq_touch' , object : CASE , events : [ 'beforeUpdate' ] , handler : ( ) => { } } ;
84+ bindHooksToEngine ( engine as never , [ hook as never ] , { packageId : 'app:cq' , logger : QUIET as never } ) ;
7985 }
8086 await engine . insert ( 'sys_user' , [ A , B ] . map ( ( id ) => ( { id, name : id , email : `${ id } @cq.test` } ) ) , SYS ) ;
8187 await engine . insert ( 'sys_organization' , [ { id : ORG , name : 'CQ' , slug : 'cq' } ] , SYS ) ;
@@ -121,44 +127,55 @@ const claim = (engine: ObjectQL, id: string, user: string) =>
121127const outcome = ( r : PromiseSettledResult < unknown > ) : string =>
122128 r . status === 'fulfilled' ? 'fulfilled' : `${ ( r . reason as any ) ?. code } /${ ( r . reason as any ) ?. status } ` ;
123129
124- for ( const sharingModel of [ 'private' , 'public_read' ] as const ) {
125- for ( const beforeHook of [ false , true ] ) {
126- describe ( `[ADR-0142 D4] concurrent claims — ${ sharingModel } OWD, ${ beforeHook ? 'metadata beforeUpdate hook (the unit opens)' : 'no hook (no unit)' } ` , ( ) => {
127- it ( `exactly one claim lands and every loser answers 409 CONCURRENT_UPDATE, over ${ ROUNDS } rounds` , async ( ) => {
128- const engine = await boot ( { sharingModel, beforeHook } ) ;
129- const losers : Record < string , number > = { } ;
130- for ( let round = 0 ; round < ROUNDS ; round ++ ) {
131- const id = `case_${ round } ` ;
132- await seed ( engine , { id, name : 'parked' , owner_id : null , queue : QUEUE } ) ;
133- const settled = await Promise . allSettled ( [ claim ( engine , id , A ) , claim ( engine , id , B ) ] ) ;
134- const won = settled . flatMap ( ( r , i ) => ( r . status === 'fulfilled' ? [ [ A , B ] [ i ] ] : [ ] ) ) ;
135- expect ( won , `round ${ round } : ${ settled . map ( outcome ) . join ( ' | ' ) } ` ) . toHaveLength ( 1 ) ;
136- for ( const r of settled ) {
137- if ( r . status === 'rejected' ) {
138- const key = outcome ( r ) ;
139- losers [ key ] = ( losers [ key ] ?? 0 ) + 1 ;
140- }
130+ const HOOK_LABEL : Record < HookShape , string > = {
131+ none : 'no hook (no unit)' ,
132+ beforeUpdate : 'metadata beforeUpdate hook (the unit opens)' ,
133+ afterUpdate : 'only a blocking metadata afterUpdate hook (the unit opens)' ,
134+ } ;
135+ const CELLS : ReadonlyArray < { sharingModel : 'private' | 'public_read' ; hook : HookShape } > = [
136+ { sharingModel : 'private' , hook : 'none' } ,
137+ { sharingModel : 'private' , hook : 'beforeUpdate' } ,
138+ { sharingModel : 'public_read' , hook : 'none' } ,
139+ { sharingModel : 'public_read' , hook : 'beforeUpdate' } ,
140+ { sharingModel : 'private' , hook : 'afterUpdate' } ,
141+ ] ;
142+
143+ for ( const { sharingModel, hook } of CELLS ) {
144+ describe ( `[ADR-0142 D4] concurrent claims — ${ sharingModel } OWD, ${ HOOK_LABEL [ hook ] } ` , ( ) => {
145+ it ( `exactly one claim lands and every loser answers 409 CONCURRENT_UPDATE, over ${ ROUNDS } rounds` , async ( ) => {
146+ const engine = await boot ( { sharingModel, hook } ) ;
147+ const losers : Record < string , number > = { } ;
148+ for ( let round = 0 ; round < ROUNDS ; round ++ ) {
149+ const id = `case_${ round } ` ;
150+ await seed ( engine , { id, name : 'parked' , owner_id : null , queue : QUEUE } ) ;
151+ const settled = await Promise . allSettled ( [ claim ( engine , id , A ) , claim ( engine , id , B ) ] ) ;
152+ const won = settled . flatMap ( ( r , i ) => ( r . status === 'fulfilled' ? [ [ A , B ] [ i ] ] : [ ] ) ) ;
153+ expect ( won , `round ${ round } : ${ settled . map ( outcome ) . join ( ' | ' ) } ` ) . toHaveLength ( 1 ) ;
154+ for ( const r of settled ) {
155+ if ( r . status === 'rejected' ) {
156+ const key = outcome ( r ) ;
157+ losers [ key ] = ( losers [ key ] ?? 0 ) + 1 ;
141158 }
142- const stored = await engine . findOne ( CASE , { where : { id } } as never , SYS ) ;
143- expect ( ( stored as any ) ?. owner_id , `round ${ round } : the stored owner is the winner` ) . toBe ( won [ 0 ] ) ;
144159 }
145- expect ( losers ) . toEqual ( { 'CONCURRENT_UPDATE/409' : ROUNDS } ) ;
146- } ) ;
160+ const stored = await engine . findOne ( CASE , { where : { id } } as never , SYS ) ;
161+ expect ( ( stored as any ) ?. owner_id , `round ${ round } : the stored owner is the winner` ) . toBe ( won [ 0 ] ) ;
162+ }
163+ expect ( losers ) . toEqual ( { 'CONCURRENT_UPDATE/409' : ROUNDS } ) ;
147164 } ) ;
148- }
165+ } ) ;
149166}
150167
151168describe ( '[ADR-0142 D4] the unit around the engine body keeps the gate\'s refusals — private OWD, hooked object' , ( ) => {
152169 it ( 'a row that is gone still answers 404 RECORD_NOT_FOUND' , async ( ) => {
153- const engine = await boot ( { sharingModel : 'private' , beforeHook : true } ) ;
170+ const engine = await boot ( { sharingModel : 'private' , hook : 'beforeUpdate' } ) ;
154171 await seed ( engine , { id : 'case_gone' , name : 'parked' , owner_id : null , queue : QUEUE } ) ;
155172 await engine . delete ( CASE , { where : { id : 'case_gone' } , context : { isSystem : true } } as never ) ;
156173 const err = await claim ( engine , 'case_gone' , A ) . then ( ( ) => undefined , ( e : unknown ) => e ) ;
157174 expect ( err ) . toMatchObject ( { code : 'RECORD_NOT_FOUND' , status : 404 } ) ;
158175 } ) ;
159176
160177 it ( 'a row the caller cannot read answers as not found, never as forbidden' , async ( ) => {
161- const engine = await boot ( { sharingModel : 'private' , beforeHook : true } ) ;
178+ const engine = await boot ( { sharingModel : 'private' , hook : 'beforeUpdate' } ) ;
162179 await seed ( engine , { id : 'case_theirs' , name : 'owned' , owner_id : B , queue : null } ) ;
163180 const err = await claim ( engine , 'case_theirs' , A ) . then ( ( ) => undefined , ( e : unknown ) => e ) ;
164181 expect ( err ) . toMatchObject ( { code : 'RECORD_NOT_FOUND' , status : 404 } ) ;
0 commit comments