Skip to content

Commit 5340ec1

Browse files
committed
feat(spec,objectql,service-analytics): IObjectQLEngine.fixedReadLeafClassOf, wired into the analytics plugin's native-path hook (ADR-0155 D4)
Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibwJEh7hMPPvagWM1g7
1 parent 74cac04 commit 5340ec1

4 files changed

Lines changed: 105 additions & 12 deletions

File tree

‎packages/objectql/src/engine.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
22

33
import { AsyncLocalStorage } from 'node:async_hooks';
4-
import { QueryAST, QueryInput, HookContext, ServiceObject } from '@objectstack/spec/data';
4+
import { QueryAST, QueryInput, HookContext, ServiceObject, type FixedReadLeaf } from '@objectstack/spec/data';
55
// [commit 74155c735] The defaulting node schema `fillQueryAstDefaults` runs author input
66
// through — the declared `.default()` stays in `packages/spec`, the engine
77
// only invokes it.
@@ -52,6 +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';
5556
// [#25522 cut 3] The lifecycle health bodies; the methods of the same names
5657
// below hand each body the members it reads (`LifecycleHealthHost`).
5758
import { checkDriversHealth, resolvePrimaryDatasource } from './engine-lifecycle-health.js';
@@ -1916,6 +1917,9 @@ export class ObjectQL implements IObjectQLEngine {
19161917
return MiddlewareAndContextFacet.hasObjectMiddleware.call(middlewareAndContextHost.of(this), objectName);
19171918
}
19181919

1920+
/** [ADR-0155 D4] `IObjectQLEngine.fixedReadLeafClassOf`: the seam's own recognition of D4's two classes for `leaf`, `undefined` for neither. */
1921+
fixedReadLeafClassOf(leaf: FixedReadLeaf): FixedReadLeafClass | undefined { return fixedReadLeafClassOfEngine(this, leaf); }
1922+
19191923
private executeWithMiddleware(ctx: OperationContext, executor: () => Promise<any>): Promise<any> {
19201924
// [#29986 codemod] The body moved to `engine-middleware-and-context.ts`, its docblock with it (see `MiddlewareAndContextHost`).
19211925
return MiddlewareAndContextFacet.executeWithMiddleware.call(middlewareAndContextHost.of(this), ctx, executor);

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

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ import { asAcceptedSqlDialect, type AcceptedSqlDialect } from './text-match-sql.
5858
* `IObjectQLEngine`, so the `Partial<>` around it is not decoration — it is
5959
* what keeps this seam usable against an engine that is not ObjectQL.
6060
* `judgeFilter` (#20157) is optional on the contract itself, by ruling, and
61-
* the auto-bridge probes it the same way. So is `hasObjectMiddleware`
62-
* (#21080), whose absence the bridge below reads as "cannot say".
61+
* the auto-bridge probes it the same way. So are `hasObjectMiddleware`
62+
* (#21080) and `fixedReadLeafClassOf` (ADR-0155 D4, #35946), whose absence
63+
* the bridges below read as "cannot say".
6364
*/
6465
/** One bridged `executeAggregate` call's input (the plugin option's own declaration). */
6566
type BridgedAggregateInput = Parameters<NonNullable<AnalyticsServicePluginOptions['executeAggregate']>>[1];
@@ -82,7 +83,7 @@ const AGGREGATE_ORDER_CUT: unique symbol = Symbol.for('objectstack.objectql.aggr
8283
type DataEngineLike =
8384
Pick<IDataEngine, 'aggregate'>
8485
& Partial<Pick<IDataEngine, 'execute' | 'resolveEffectiveDatasource' | 'getDriverForObject'>>
85-
& Partial<Pick<IObjectQLEngine, 'getObject' | 'judgeFilter' | 'hasObjectMiddleware'>>;
86+
& Partial<Pick<IObjectQLEngine, 'getObject' | 'judgeFilter' | 'hasObjectMiddleware' | 'fixedReadLeafClassOf'>>;
8687

8788
/**
8889
* The slice of the `IDataDriver` CONTRACT the analytics layer consumes —
@@ -1531,6 +1532,45 @@ export class AnalyticsServicePlugin implements Plugin {
15311532
return undefined;
15321533
};
15331534

1535+
/**
1536+
* [ADR-0155 D4, plan step 5, #35946] The engine seam's recognition of D4's
1537+
* two classes for a fixed-read leaf a read scope carries
1538+
* (`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.
1542+
*
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.
1550+
*/
1551+
let reportedEngineWithoutLeafClassAnswer = false;
1552+
const fixedReadLeafClassOf: NonNullable<AnalyticsServiceConfig['fixedReadLeafClassOf']> = (leaf) => {
1553+
let svc: DataEngineLike | undefined;
1554+
try {
1555+
svc = ctx.getService<DataEngineLike>('data');
1556+
} catch {
1557+
return undefined;
1558+
}
1559+
if (!svc) return undefined;
1560+
if (typeof svc.fixedReadLeafClassOf === 'function') return svc.fixedReadLeafClassOf(leaf) ?? null;
1561+
if (!reportedEngineWithoutLeafClassAnswer) {
1562+
reportedEngineWithoutLeafClassAnswer = true;
1563+
ctx.logger.warn(
1564+
'[Analytics] The "data" engine has no fixedReadLeafClassOf (IObjectQLEngine.fixedReadLeafClassOf), so ' +
1565+
'NativeSQLStrategy cannot ask the engine whether it may compile a SharedRecordIds leaf in a read scope. It ' +
1566+
'declines every query whose read scope carries one, and the ObjectQL strategy serves it through the engine; ' +
1567+
'a query that strategy cannot serve is refused. Register ObjectQLPlugin\'s engine as "data" to keep the ' +
1568+
'native path for those scopes. Reported once.',
1569+
);
1570+
}
1571+
return undefined;
1572+
};
1573+
15341574
const config: AnalyticsServiceConfig = {
15351575
cubes: this.options.cubes,
15361576
logger: ctx.logger,
@@ -1657,6 +1697,9 @@ export class AnalyticsServicePlugin implements Plugin {
16571697
// `hasObjectMiddleware` above. Wired whoever executes raw SQL: the
16581698
// middlewares live in the engine either way.
16591699
hasObjectMiddleware,
1700+
// [ADR-0155 D4, #35946] The engine seam's class recognition for a
1701+
// fixed-read leaf — see `fixedReadLeafClassOf` above.
1702+
fixedReadLeafClassOf,
16601703
// ADR-0062 D6 — a federated object carries an `external` block (ADR-0015).
16611704
// Reported so NativeSQLStrategy declines it (its hand-compiled FROM would
16621705
// hit the wrong physical table) and the driver-correct ObjectQL path runs.

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

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import type { FlowFunctionEffect, FlowFunctionEntry } from '../automation/flow-f
5757
import type { ServiceObject } from '../data/object.zod';
5858
import type { Hook, HookContext } from '../data/hook.zod';
5959
import type { BaseEngineOptions, EngineQueryOptions } from '../data/data-engine.zod';
60+
import type { FixedReadLeaf } from '../data/fixed-read-leaf';
6061

6162
/**
6263
* The engine's schema-registry view — the members reached through the
@@ -278,6 +279,23 @@ export type EngineFilterJudgement =
278279
| { ok: true }
279280
| { ok: false; code: string; status: number; message: string };
280281

282+
/**
283+
* The answer of {@link IObjectQLEngine.fixedReadLeafClassOf} (ADR-0155 D4):
284+
* which of the two classes a fixed-read leaf falls in on this engine, and what
285+
* 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.
288+
*/
289+
export type EngineFixedReadLeafClass =
290+
| {
291+
readonly class: 'other-datasource';
292+
readonly object: string;
293+
readonly table: string;
294+
readonly objectDatasource: string | undefined;
295+
readonly tableDatasource: string | undefined;
296+
}
297+
| { readonly class: 'registration-by-name'; readonly table: string; readonly registration: string };
298+
281299
/**
282300
* The full ObjectQL engine, as the `objectql` slot's consumers use it.
283301
*
@@ -290,12 +308,13 @@ export type EngineFilterJudgement =
290308
* guarded call into a `possibly undefined` error and push code back toward the
291309
* `any` this contract exists to remove.
292310
*
293-
* Four members are optional by ruling, not by this default: {@link judgeFilter}
311+
* Five members are optional by ruling, not by this default: {@link judgeFilter}
294312
* (#19995 ruling C), {@link getDisabledActions} (#27842 ruling letter B,
295313
* the `getFlowRegistration?` shape), {@link getCascadeDeleteObjects}
296-
* (#28001, that shape again) and {@link deferSummaryRecompute} (#33795,
297-
* ruling 6922040858). Their callers must work with an engine that lacks
298-
* them, so the optional mark states that requirement in the type.
314+
* (#28001, that shape again), {@link deferSummaryRecompute} (#33795,
315+
* ruling 6922040858) and {@link fixedReadLeafClassOf} (#35946, ruling
316+
* 7051956587). Their callers must work with an engine that lacks them, so the
317+
* optional mark states that requirement in the type.
299318
*/
300319
export interface IObjectQLEngine extends IDataEngine, QueryEvaluationGate {
301320
// ── Schema access ────────────────────────────────────────────────────
@@ -510,6 +529,33 @@ export interface IObjectQLEngine extends IDataEngine, QueryEvaluationGate {
510529
* `typeof ql.hasObjectMiddleware === 'function'`.
511530
*/
512531
hasObjectMiddleware?(objectName: string): boolean;
532+
/**
533+
* [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
535+
* neither applies: the engine seam's own recognition
536+
* (`fixedReadLeafClassOfEngine`), the one the executor boundary runs.
537+
*
538+
* - `'other-datasource'`: the object the leaf names is bound to another
539+
* datasource than the table its fixed inner read reads.
540+
* - `'registration-by-name'`: an object-scoped middleware, or a
541+
* `beforeFind` / `afterFind` hook, is registered for that table by name.
542+
*
543+
* Read-only: it reads the engine's datasource bindings, middlewares and
544+
* hooks, and calls no driver and logs nothing.
545+
*
546+
* The consumer is a read path that compiles the leaf itself instead of
547+
* 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.
557+
*/
558+
fixedReadLeafClassOf?(leaf: FixedReadLeaf): EngineFixedReadLeafClass | undefined;
513559
/** Bind declarative Hook metadata — AppPlugin's app-bundle path. */
514560
bindHooks(
515561
hooks: unknown[] | undefined,

‎scripts/facade-delegation-registry.json‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@
172172
},
173173
{
174174
"file": "packages/objectql/src/engine.ts",
175-
"baseLines": 6754,
176-
"baseCommit": "63db7240aa",
177-
"maxLines": 6854,
178-
"ruling": "6897467376"
175+
"baseLines": 6853,
176+
"baseCommit": "170ca481f0",
177+
"maxLines": 6858,
178+
"ruling": "7051956587"
179179
},
180180
{
181181
"file": "packages/drivers/driver-sql/src/sql-driver.ts",

0 commit comments

Comments
 (0)