Skip to content

Commit e117d86

Browse files
committed
fix(spec,objectql,service-analytics): fixedReadLeafClassOf answers null for neither, so the optional call is fail-closed by construction
Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibwJEh7hMPPvagWM1g7
1 parent b6172bf commit e117d86

6 files changed

Lines changed: 88 additions & 38 deletions

File tree

‎.changeset/35946-fixed-read-leaf-engine-member.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ feat: the analytics plugin asks the engine which share-grant scopes its native S
99
Clause-②: yes
1010
Metadata-protocol: none
1111

12-
- **New optional contract member** (`@objectstack/spec`). `IObjectQLEngine.fixedReadLeafClassOf(leaf)` answers which of ADR-0155 D4's two classes a `SharedRecordIds` leaf falls in on an engine: `'other-datasource'` (the leaf's object is bound to another datasource than `sys_record_share`) or `'registration-by-name'` (a middleware or a `beforeFind` / `afterFind` hook is registered for `sys_record_share` by name), with what names it. It answers `undefined` when neither class applies. The answer type is the new `EngineFixedReadLeafClass`. The member is optional: a caller probes `typeof engine.fixedReadLeafClassOf === 'function'` and treats an engine without it as one that cannot say.
13-
- **The engine implements it** (`@objectstack/objectql`). `ObjectQL.fixedReadLeafClassOf` returns the seam's own recognition, `fixedReadLeafClassOfEngine(engine, leaf)`. It reads only datasource bindings, middlewares and hooks. It calls no driver and logs nothing.
14-
- **The analytics plugin wires it** (`@objectstack/service-analytics`). `AnalyticsServicePlugin` now fills `AnalyticsServiceConfig.fixedReadLeafClassOf` from the `"data"` engine on each call. When the engine answers neither class, the native SQL path compiles a read scope that carries the leaf. When the engine names a class, or the engine has no such member, the query goes to the ObjectQL engine path, as before. An engine without the member is reported once at `warn`. Neither class is restated in analytics.
12+
- **New optional contract member** (`@objectstack/spec`). `IObjectQLEngine.fixedReadLeafClassOf(leaf)` answers which of ADR-0155 D4's two classes a `SharedRecordIds` leaf falls in on an engine: `'other-datasource'` (the leaf's object is bound to another datasource than `sys_record_share`) or `'registration-by-name'` (a middleware or a `beforeFind` / `afterFind` hook is registered for `sys_record_share` by name), with what names it. It answers `null` when neither class applies, and throws `TypeError` on a value that is not a fixed-read leaf. The answer type is the new `EngineFixedReadLeafClass`. The member is optional, and because "neither" is `null`, `engine.fixedReadLeafClassOf?.(leaf)` is a caller's whole answer: a class, `null`, or `undefined` from an engine without the member, which cannot say.
13+
- **The engine implements it** (`@objectstack/objectql`). `ObjectQL.fixedReadLeafClassOf` returns the seam's own recognition, `fixedReadLeafClassOfEngine(engine, leaf)`, with "neither" spelled `null`. It reads only datasource bindings, middlewares and hooks. It calls no driver and logs nothing.
14+
- **The analytics plugin wires it** (`@objectstack/service-analytics`). `AnalyticsServicePlugin` now fills `AnalyticsServiceConfig.fixedReadLeafClassOf` from the `"data"` engine on each call. When the engine answers `null` (neither class), the native SQL path compiles a read scope that carries the leaf. When the engine names a class, throws, answers anything but `null`, or has no such member, the query goes to the ObjectQL engine path, as before. An engine without the member is reported once at `warn`. Neither class is restated in analytics.
1515

1616
Nothing a metadata author writes changes. On a host that runs `ObjectQLPlugin`'s engine as `"data"`, a sharing-scoped analytics query outside both classes keeps the native SQL path once the producer starts writing the leaf (ADR-0155 step 6).

‎packages/objectql/src/engine.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import { negotiatedMessageLocale, objectDisplayLabel, validationMessageContext }
5252
// below hand each body the members it reads (`DatasourceRoutingHost`).
5353
import { getDriver, matchPattern, resolveDatasourceFromMapping } from './engine-datasource-routing.js';
5454
import { relationSemiJoinServes } from './relation-semi-join.js';
55-
import { fixedReadLeafClassOfEngine, type FixedReadLeafClass } from './fixed-read-leaf-seam.js';
55+
import { fixedReadLeafClassAnswerOf } from './fixed-read-leaf-engine-answer.js';
5656
// [#25522 cut 3] The lifecycle health bodies; the methods of the same names
5757
// below hand each body the members it reads (`LifecycleHealthHost`).
5858
import { checkDriversHealth, resolvePrimaryDatasource } from './engine-lifecycle-health.js';
@@ -128,7 +128,7 @@ import { ExecutionContext, type InstalledPackageSource } from '@objectstack/spec
128128
// contract is new (#5945).
129129
import type { IPubSub, IScopedContext, IScopedObjectRepository, IntrospectedSchema as SpecIntrospectedSchema } from '@objectstack/spec/contracts';
130130
// [#20157] The judge-only filter-admission member's verdict and options.
131-
import type { EngineFilterJudgement, EngineFilterJudgementOptions, IObjectQLEngine, InternalFieldEvaluationOptions, InternalFieldEvaluationQuery } from '@objectstack/spec/contracts';
131+
import type { EngineFilterJudgement, EngineFilterJudgementOptions, EngineFixedReadLeafClass, IObjectQLEngine, InternalFieldEvaluationOptions, InternalFieldEvaluationQuery } from '@objectstack/spec/contracts';
132132
import {
133133
IDataDriver,
134134
IDataEngine,
@@ -1916,8 +1916,8 @@ export class ObjectQL implements IObjectQLEngine {
19161916
return MiddlewareAndContextFacet.hasObjectMiddleware.call(middlewareAndContextHost.of(this), objectName);
19171917
}
19181918

1919-
/** [ADR-0155 D4] `IObjectQLEngine.fixedReadLeafClassOf`: the seam's own recognition of D4's two classes for `leaf`, `undefined` for neither. */
1920-
fixedReadLeafClassOf(leaf: FixedReadLeaf): FixedReadLeafClass | undefined { return fixedReadLeafClassOfEngine(this, leaf); }
1919+
/** [ADR-0155 D4] `IObjectQLEngine.fixedReadLeafClassOf`: the seam's own recognition of D4's two classes for `leaf`, `null` for neither. */
1920+
fixedReadLeafClassOf(leaf: FixedReadLeaf): EngineFixedReadLeafClass | null { return fixedReadLeafClassAnswerOf(this, leaf); }
19211921

19221922
private executeWithMiddleware(ctx: OperationContext, executor: () => Promise<any>): Promise<any> {
19231923
// [#29986 codemod] The body moved to `engine-middleware-and-context.ts`, its docblock with it (see `MiddlewareAndContextHost`).
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* [ADR-0155 D4, plan step 5] The answer `IObjectQLEngine.fixedReadLeafClassOf`
5+
* gives, which `ObjectQL` forwards to in one line (its facade allows nothing
6+
* else): the seam's own recognition, {@link fixedReadLeafClassOfEngine}, with
7+
* its "neither class" spelled `null` instead of `undefined`.
8+
*
9+
* Why the respelling: the member is optional, so a caller asking through
10+
* `engine.fixedReadLeafClassOf?.(leaf)` already gets `undefined` from an engine
11+
* without it, and must decline there (it cannot say). Were "neither" also
12+
* `undefined`, that caller could not tell an engine that answered from one
13+
* that could not, and a caller that read `undefined` as "neither" would compile
14+
* what it cannot see. With "neither" as `null`, the optional call is the
15+
* caller's whole three-state answer and its fail-closed reading needs no probe.
16+
*
17+
* Read-only, like the seam: it throws `TypeError` on a value that is not a
18+
* fixed-read leaf, and otherwise calls no driver and logs nothing.
19+
*/
20+
21+
import type { FixedReadLeaf } from '@objectstack/spec/data';
22+
import { fixedReadLeafClassOfEngine, type FixedReadLeafClass } from './fixed-read-leaf-seam.js';
23+
import type { ObjectQL } from './engine.js';
24+
25+
/** Which of D4's two classes `leaf` falls in on `engine`, or `null` when neither. */
26+
export function fixedReadLeafClassAnswerOf(engine: ObjectQL, leaf: FixedReadLeaf): FixedReadLeafClass | null {
27+
return fixedReadLeafClassOfEngine(engine, leaf) ?? null;
28+
}

‎packages/services/service-analytics/src/__tests__/fixed-read-leaf-native-compile.test.ts‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,9 @@ describe('[ADR-0155 Q2] the share-grant leaf on the analytics faces, live on SQL
302302
// [#35946, ruling 7051956587] `AnalyticsServicePlugin` wires the hook from
303303
// the "data" engine's `IObjectQLEngine.fixedReadLeafClassOf`, read
304304
// structurally: the engine here is a plain object over the live engine's
305-
// members, so the pin is the plugin's wiring, not the `ObjectQL` class.
305+
// members, so the pin is the plugin's wiring, not the `ObjectQL` class. The
306+
// member's three answers: a class, `null` for neither (compile), and
307+
// `undefined` from an engine without it (decline).
306308
describe('[#35946] AnalyticsServicePlugin asks the data engine\'s fixedReadLeafClassOf', () => {
307309
const CUBE = {
308310
name: 'docs', sql: 'srid_doc', public: true,
@@ -312,14 +314,19 @@ describe('[ADR-0155 Q2] the share-grant leaf on the analytics faces, live on SQL
312314
const QUERY = { cube: 'docs', dimensions: ['id'], measures: ['n'] } as AnalyticsQuery;
313315
const READ_SCOPE = S.cases.find((c) => c.name.startsWith("the read scope's shape"))!;
314316

315-
/** The "data" engine as a plain object; `member` says what its `fixedReadLeafClassOf` is. */
316-
const plainEngine = (member: 'present' | 'absent'): Record<string, unknown> => ({
317+
/**
318+
* The "data" engine as a plain object; `member` says what its
319+
* `fixedReadLeafClassOf` is: the live engine's, none, or one that breaks
320+
* the contract by answering `undefined` for neither.
321+
*/
322+
const plainEngine = (member: 'present' | 'absent' | 'answers-undefined'): Record<string, unknown> => ({
317323
aggregate: (o: string, q: any) => engine.aggregate(o, q),
318324
getObject: (n: string) => engine.getObject(n),
319325
judgeFilter: (o: string, w: any, j: any) => engine.judgeFilter(o, w, j),
320326
hasObjectMiddleware: (o: string) => engine.hasObjectMiddleware(o),
321327
getDriverForObject: (o: string) => engine.getDriverForObject(o),
322328
...(member === 'present' ? { fixedReadLeafClassOf: (leaf: any) => engine.fixedReadLeafClassOf(leaf) } : {}),
329+
...(member === 'answers-undefined' ? { fixedReadLeafClassOf: () => undefined } : {}),
323330
});
324331

325332
async function viaPlugin(data: Record<string, unknown>) {
@@ -344,7 +351,10 @@ describe('[ADR-0155 Q2] the share-grant leaf on the analytics faces, live on SQL
344351
}
345352
const idsOf = (rows: Array<Record<string, unknown>>) => rows.map((r) => String(r.id)).sort();
346353

347-
it('the member present, the object in neither class: the native path compiles the leaf', async () => {
354+
const LEAF = () => SharedRecordIds.read('srid_doc', 'u1');
355+
356+
it('the member present, the object in neither class: it answers null, and the native path compiles the leaf', async () => {
357+
expect(engine.fixedReadLeafClassOf(LEAF())).toBeNull();
348358
const { answer, rawSql } = await viaPlugin(plainEngine('present'));
349359
const { rows, err } = await answer();
350360
expect(err).toBeNull();
@@ -356,6 +366,7 @@ describe('[ADR-0155 Q2] the share-grant leaf on the analytics faces, live on SQL
356366
it('the member answers a D4 class: the native path declines, and the engine\'s seam refuses', async () => {
357367
engine.registerHook('beforeFind', () => {}, { object: 'sys_record_share' });
358368
try {
369+
expect(engine.fixedReadLeafClassOf(LEAF())).toMatchObject({ class: 'registration-by-name', table: 'sys_record_share' });
359370
const { answer, rawSql } = await viaPlugin(plainEngine('present'));
360371
const { err } = await answer();
361372
expect(rawSql).not.toHaveBeenCalled();
@@ -374,5 +385,14 @@ describe('[ADR-0155 Q2] the share-grant leaf on the analytics faces, live on SQL
374385
expect(idsOf(first.rows)).toEqual([...READ_SCOPE.expected]);
375386
expect(warn.mock.calls.filter((c) => String(c[0]).includes('fixedReadLeafClassOf'))).toHaveLength(1);
376387
});
388+
389+
it('a member that answers undefined (not the contract\'s null): the native path declines, the engine path answers', async () => {
390+
const { answer, rawSql, warn } = await viaPlugin(plainEngine('answers-undefined'));
391+
const { rows, err } = await answer();
392+
expect(err).toBeNull();
393+
expect(rawSql).not.toHaveBeenCalled();
394+
expect(idsOf(rows)).toEqual([...READ_SCOPE.expected]);
395+
expect(warn.mock.calls.filter((c) => String(c[0]).includes('fixedReadLeafClassOf'))).toHaveLength(0);
396+
});
377397
});
378398
});

‎packages/services/service-analytics/src/plugin.ts‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,17 +1536,17 @@ export class AnalyticsServicePlugin implements Plugin {
15361536
* [ADR-0155 D4, plan step 5, #35946] The engine seam's recognition of D4's
15371537
* two classes for a fixed-read leaf a read scope carries
15381538
* (`IObjectQLEngine.fixedReadLeafClassOf`), resolved per call like
1539-
* `hasObjectMiddleware` above. The engine's "neither" (`undefined` from the
1540-
* member) is this hook's `null`, the one answer on which `NativeSQLStrategy`
1541-
* compiles the leaf; a class is handed on as the engine named it.
1539+
* `hasObjectMiddleware` above, and handed on as the engine gave it: a
1540+
* class, or `null` for neither, the one answer on which `NativeSQLStrategy`
1541+
* compiles the leaf.
15421542
*
1543-
* It FAILS CLOSED: no engine, or an engine without the member, answers
1544-
* `undefined` ("cannot say"), and the strategy declines the query to the
1545-
* engine path, as it does on a class and on a throw. ⛔ Never
1546-
* `engine.fixedReadLeafClassOf?.(leaf) ?? null`: the absent member would
1547-
* read as "neither class". ⛔ No copy of either class here (D4: "restates
1548-
* none"). Said once, at `warn`: the rows stay correct, only the native path
1549-
* for a scope carrying the leaf is gone.
1543+
* It FAILS CLOSED: no engine, a `getService` throw, or an engine without
1544+
* the member answers `undefined` ("cannot say"). The strategy declines
1545+
* every answer but `null` to the engine path, so that tier declines, as a
1546+
* class and a throw do, and so does a member that does not keep the
1547+
* contract (one answering `undefined` for neither, say). ⛔ No copy of either class here (D4: "restates none"). An engine
1548+
* without the member is said once, at `warn`: the rows stay correct, only
1549+
* the native path for a scope carrying the leaf is gone.
15501550
*/
15511551
let reportedEngineWithoutLeafClassAnswer = false;
15521552
const fixedReadLeafClassOf: NonNullable<AnalyticsServiceConfig['fixedReadLeafClassOf']> = (leaf) => {
@@ -1557,8 +1557,7 @@ export class AnalyticsServicePlugin implements Plugin {
15571557
return undefined;
15581558
}
15591559
if (!svc) return undefined;
1560-
if (typeof svc.fixedReadLeafClassOf === 'function') return svc.fixedReadLeafClassOf(leaf) ?? null;
1561-
if (!reportedEngineWithoutLeafClassAnswer) {
1560+
if (typeof svc.fixedReadLeafClassOf !== 'function' && !reportedEngineWithoutLeafClassAnswer) {
15621561
reportedEngineWithoutLeafClassAnswer = true;
15631562
ctx.logger.warn(
15641563
'[Analytics] The "data" engine has no fixedReadLeafClassOf (IObjectQLEngine.fixedReadLeafClassOf), so ' +
@@ -1568,7 +1567,7 @@ export class AnalyticsServicePlugin implements Plugin {
15681567
'native path for those scopes. Reported once.',
15691568
);
15701569
}
1571-
return undefined;
1570+
return svc.fixedReadLeafClassOf?.(leaf);
15721571
};
15731572

15741573
const config: AnalyticsServiceConfig = {

‎packages/spec/src/contracts/objectql-engine.ts‎

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,10 @@ export type EngineFilterJudgement =
283283
* The answer of {@link IObjectQLEngine.fixedReadLeafClassOf} (ADR-0155 D4):
284284
* which of the two classes a fixed-read leaf falls in on this engine, and what
285285
* names it. `@objectstack/objectql`'s `FixedReadLeafClass`, the seam's own
286-
* answer, stated here because spec does not depend on the engine package;
287-
* `ObjectQL implements IObjectQLEngine` holds the two equal on every build.
286+
* answer, restated here because spec does not depend on the engine package.
287+
* `ObjectQL implements IObjectQLEngine` checks one direction only, on every
288+
* build: the seam's answer must be assignable to this type. A member this type
289+
* adds, or a member the seam drops, is caught; a member the seam adds is not.
288290
*/
289291
export type EngineFixedReadLeafClass =
290292
| {
@@ -531,7 +533,7 @@ export interface IObjectQLEngine extends IDataEngine, QueryEvaluationGate {
531533
hasObjectMiddleware?(objectName: string): boolean;
532534
/**
533535
* [ADR-0155 D4, plan step 5] Which of D4's two classes the fixed-read leaf
534-
* `leaf` (`SharedRecordIds`) falls in on this engine, or `undefined` when
536+
* `leaf` (`SharedRecordIds`) falls in on this engine, or `null` when
535537
* neither applies: the engine seam's own recognition
536538
* (`fixedReadLeafClassOfEngine`), the one the executor boundary runs.
537539
*
@@ -541,21 +543,22 @@ export interface IObjectQLEngine extends IDataEngine, QueryEvaluationGate {
541543
* `beforeFind` / `afterFind` hook, is registered for that table by name.
542544
*
543545
* Read-only: it reads the engine's datasource bindings, middlewares and
544-
* hooks, and calls no driver and logs nothing.
546+
* hooks, and calls no driver and logs nothing. It throws `TypeError` when
547+
* `leaf` is not a fixed-read leaf (`fixedInnerReadOf`).
545548
*
546549
* The consumer is a read path that compiles the leaf itself instead of
547550
* handing it to the engine: the analytics native-SQL strategy (ADR-0155 Q2
548-
* as ruled) asks this before it compiles, compiles only on `undefined`, and
549-
* declines the query to the engine on a class, where the seam answers it.
550-
* It restates neither class (D4: recognised once, in the seam).
551-
*
552-
* OPTIONAL by ruling 7051956587: an engine without it cannot answer, and
553-
* that consumer reads the absence as "cannot say" and declines, so the
554-
* probe is `typeof ql.fixedReadLeafClassOf === 'function'`. ⛔ Never
555-
* `ql.fixedReadLeafClassOf?.(leaf) ?? null` alone: an absent member then
556-
* reads as "neither class" and the consumer compiles what it cannot see.
551+
* as ruled) asks this before it compiles, compiles only on `null`, and
552+
* declines the query to the engine on a class, where the seam answers it,
553+
* and on a throw. It restates neither class (D4: recognised once, in the
554+
* seam).
555+
*
556+
* OPTIONAL by ruling 7051956587. "Neither" is `null`, never `undefined`, so
557+
* `ql.fixedReadLeafClassOf?.(leaf)` is the consumer's whole three-state
558+
* answer: a class, `null` (compile), or `undefined` from an engine without
559+
* the member, which cannot say, and the consumer declines.
557560
*/
558-
fixedReadLeafClassOf?(leaf: FixedReadLeaf): EngineFixedReadLeafClass | undefined;
561+
fixedReadLeafClassOf?(leaf: FixedReadLeaf): EngineFixedReadLeafClass | null;
559562
/** Bind declarative Hook metadata — AppPlugin's app-bundle path. */
560563
bindHooks(
561564
hooks: unknown[] | undefined,

0 commit comments

Comments
 (0)