66 * `sudo`, `withRunAs`, `releaseJoinedHandle`, `txDriver`, `beginTransaction`,
77 * `commitTransaction`, `rollbackTransaction`, `serveFormulaField` moved out of
88 * `engine.ts` whole by `scripts/codemod-split-engine.mjs` (its second pass).
9+ * The bodies of `derive` and `[APP_AUTHORED_HOOK_API]` moved by hand
10+ * (#30975): each writes the private `appAuthored` of the instance it builds,
11+ * which the codemod refuses to move (`foreign-private`), so each writes it
12+ * through that instance's own host setter.
913 *
1014 * A MOVE, not a redesign. {@link ScopedContextFacet} is a stateless facet of
1115 * `ScopedContext`: each static method of it is the body of the `ScopedContext`
3236
3337import { driverSupportsTransactions } from '@objectstack/spec/data' ;
3438import type { IScopedContext } from '@objectstack/spec/contracts' ;
39+ import type { ExecutionContext } from '@objectstack/spec/kernel' ;
3540import { appAutomationElevationOf , markAppAutomationElevation } from '@objectstack/core' ;
3641import { UnscopedHookApi } from './hook-run-as.js' ;
3742import { refuseAppAuthoredPermanentDelete } from './recycle-bin/app-authored-delete.js' ;
3843import type { HookRunAs , HookRunAsRef } from './hook-run-as.js' ;
39- import { ObjectRepository } from './engine.js' ;
40- import type { ObjectQL , ScopedContext } from './engine.js' ;
44+ import { scopedContextHost } from './engine-scoped-hosts.js' ;
45+ import { ObjectRepository , ScopedContext } from './engine.js' ;
46+ import type { ObjectQL } from './engine.js' ;
4147
4248/**
4349 * The `ScopedContext` members the bodies of {@link ScopedContextFacet} read —
@@ -47,7 +53,7 @@ import type { ObjectQL, ScopedContext } from './engine.js';
4753 * are fields a body assigns.
4854 */
4955export interface ScopedContextHost {
50- readonly appAuthored : ScopedContext [ 'appAuthored' ] ;
56+ appAuthored : ScopedContext [ 'appAuthored' ] ;
5157 derive : ScopedContext [ 'derive' ] ;
5258 readonly engine : ScopedContext [ 'engine' ] ;
5359 readonly executionContext : ScopedContext [ 'executionContext' ] ;
@@ -58,6 +64,32 @@ export interface ScopedContextHost {
5864
5965/** The bodies of `ScopedContext`'s methods: a stateless facet, called by the class's stubs. */
6066export class ScopedContextFacet {
67+ /**
68+ * A child over `executionContext` that keeps this api's provenance.
69+ *
70+ * The child's private `appAuthored` is written through the child's own host
71+ * (`scopedContextHost.of(child)`), the port its class's static block fills:
72+ * a module function cannot write another instance's private field directly.
73+ */
74+ static derive ( this : Pick < ScopedContextHost , 'appAuthored' | 'engine' > , executionContext : ExecutionContext ) : ScopedContext {
75+ const child = new ScopedContext ( executionContext , this . engine ) ;
76+ scopedContextHost . of ( child ) . appAuthored = this . appAuthored ;
77+ return child ;
78+ }
79+
80+ /**
81+ * The body of `ScopedContext#[APP_AUTHORED_HOOK_API]`; the method's docblock
82+ * there is its contract. The new api's private `appAuthored` is written
83+ * through its own host, as in {@link ScopedContextFacet.derive}.
84+ */
85+ static appAuthoredHookApi ( this : Pick < ScopedContextHost , 'engine' | 'executionContext' > ) : ScopedContext {
86+ const ctx = this . executionContext ;
87+ const inheritsElevation = ctx . isSystem && ! ctx . flowRunId && ! appAutomationElevationOf ( ctx ) ;
88+ const api = new ScopedContext ( inheritsElevation ? markAppAutomationElevation ( ctx , 'hook' ) : ctx , this . engine ) ;
89+ scopedContextHost . of ( api ) . appAuthored = true ;
90+ return api ;
91+ }
92+
6193 /** The body of `ScopedContext#object`; the method's docblock there is its contract. */
6294 static object ( this : Pick < ScopedContextHost , 'appAuthored' | 'engine' | 'executionContext' > , name : string ) : ObjectRepository {
6395 const repo = new ObjectRepository ( name , this . executionContext , this . engine as any ) ;
0 commit comments