Skip to content

Commit b2ef226

Browse files
committed
refactor(objectql): ScopedContext derive and the app-authored api write appAuthored through the new instance's host
Their bodies move into ScopedContextFacet. Each builds a ScopedContext and writes its private appAuthored through that instance's own host setter (the port the class's static block fills), so the facade keeps delegations only. The class surface and its .d.ts are unchanged. The two facade exception rows retire. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FEkQ5kF4yD4vMatZJ2chQM
1 parent c046475 commit b2ef226

3 files changed

Lines changed: 40 additions & 28 deletions

File tree

‎packages/objectql/src/engine-scoped-context.ts‎

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
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`
@@ -32,12 +36,14 @@
3236

3337
import { driverSupportsTransactions } from '@objectstack/spec/data';
3438
import type { IScopedContext } from '@objectstack/spec/contracts';
39+
import type { ExecutionContext } from '@objectstack/spec/kernel';
3540
import { appAutomationElevationOf, markAppAutomationElevation } from '@objectstack/core';
3641
import { UnscopedHookApi } from './hook-run-as.js';
3742
import { refuseAppAuthoredPermanentDelete } from './recycle-bin/app-authored-delete.js';
3843
import 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
*/
4955
export 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. */
6066
export 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);

‎packages/objectql/src/engine.ts‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ import {
140140
type RetryOptions,
141141
recordNotFoundError,
142142
resolveUserAuthzGrants,
143-
markAppAutomationElevation,
144-
appAutomationElevationOf,
145143
} from '@objectstack/core';
146144
import { WriteEpoch } from './write-epoch.js';
147145
import { SummaryRecomputeError, cascadeStoppedRefusal, rolledBackRefusal, type SummaryRecomputeFailure } from './summary-errors.js';
@@ -10213,11 +10211,9 @@ export class ScopedContext implements IScopedContext, RunAsDerivableApi {
1021310211
*/
1021410212
private appAuthored = false;
1021510213

10216-
/** A child over `executionContext` that keeps this api's provenance. */
1021710214
private derive(executionContext: ExecutionContext): ScopedContext {
10218-
const child = new ScopedContext(executionContext, this.engine);
10219-
child.appAuthored = this.appAuthored;
10220-
return child;
10215+
// [#30975] The body moved to `engine-scoped-context.ts`, its docblock with it (see `ScopedContextHost`).
10216+
return ScopedContextFacet.derive.call(scopedContextHost.of(this), executionContext);
1022110217
}
1022210218

1022310219
/**
@@ -10261,11 +10257,8 @@ export class ScopedContext implements IScopedContext, RunAsDerivableApi {
1026110257
* keeps its mark; a user's context is not elevated and needs none.
1026210258
*/
1026310259
[APP_AUTHORED_HOOK_API](): ScopedContext {
10264-
const ctx = this.executionContext;
10265-
const inheritsElevation = ctx.isSystem && !ctx.flowRunId && !appAutomationElevationOf(ctx);
10266-
const api = new ScopedContext(inheritsElevation ? markAppAutomationElevation(ctx, 'hook') : ctx, this.engine);
10267-
api.appAuthored = true;
10268-
return api;
10260+
// [#30975] The body moved to `engine-scoped-context.ts` (see `ScopedContextHost`).
10261+
return ScopedContextFacet.appAuthoredHookApi.call(scopedContextHost.of(this));
1026910262
}
1027010263

1027110264
/**
@@ -10648,6 +10641,7 @@ export class ScopedContext implements IScopedContext, RunAsDerivableApi {
1064810641
static {
1064910642
scopedContextHost.install((instance) => ({
1065010643
get appAuthored() { return instance.appAuthored; },
10644+
set appAuthored(v) { instance.appAuthored = v; },
1065110645
derive: (...a) => instance.derive(...a),
1065210646
get engine() { return instance.engine; },
1065310647
get executionContext() { return instance.executionContext; },

‎scripts/facade-delegation-registry.json‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,6 @@
6666
"card": "#30020",
6767
"reason": "state-owning: bare-this, the body hands the instance itself on (#29986 report: stays on the class)"
6868
},
69-
{
70-
"file": "packages/objectql/src/engine.ts",
71-
"class": "ScopedContext",
72-
"member": "derive",
73-
"card": "#30975",
74-
"reason": "codemod limit: foreign-private, it writes `appAuthored` on another instance, which a module function cannot (#30052 report)"
75-
},
76-
{
77-
"file": "packages/objectql/src/engine.ts",
78-
"class": "ScopedContext",
79-
"member": "[APP_AUTHORED_HOOK_API]",
80-
"card": "#30975",
81-
"reason": "codemod limit: not-identifier-name, a symbol-keyed method; it also writes `appAuthored` on another instance (#30052 report)"
82-
},
8369
{
8470
"file": "packages/objectql/src/engine.ts",
8571
"class": "ScopedContext",

0 commit comments

Comments
 (0)