diff --git a/.changeset/18318-evalcontext-no-query-api.md b/.changeset/18318-evalcontext-no-query-api.md new file mode 100644 index 00000000000..bdd8e3e77a4 --- /dev/null +++ b/.changeset/18318-evalcontext-no-query-api.md @@ -0,0 +1,15 @@ +--- +'@objectstack/formula': minor +--- + +`EvalContext` no longer declares `api?: { exists, count, lookup }` — the kernel query API behind `os.exists` / `os.count` / `os.lookup`, which `buildScope()` never bound (#18318). + +**BREAKING** for a TypeScript consumer: an `EvalContext` literal that carries `api` stops compiling. The level stays `minor` because the launch window refuses `major` outright — while it is open, breaking-ness is carried by this banner and by the ADR-0087 disposition at the foot of this changeset, not by the bump. + +The member's docblock said it was "implemented opportunistically by call sites that have a query engine", and no call site ever could: `ctx.api` was read **zero** times in this package — control in the same sweep, `ctx.user`, three reads in `stdlib.ts` — so the three functions reached no evaluation scope however completely a caller populated the member. An author who wrote a predicate to the declaration got `runtime: found no matching overload for 'dyn.lookup(string, dyn)'` instead, and because an unevaluable predicate refuses the write it guards, a validation rule authored that way locked **every** write on its object. The harm came from the declaration existing, not from the implementation missing, so it is removed rather than implemented — with the reason written at the deletion site, and with no shim, alias or reserved spelling left behind. + +**Migration — `api: { … }` → delete the property.** There is no replacement key and nothing to re-point: every implementation ever passed there was discarded before evaluation, so removing the property changes no result your predicates produce. TypeScript is where you will hear about it: an `EvalContext` literal carrying `api` now fails to compile, which is the whole of the break. Reading a related record's field from inside a predicate remains unexpressible in any spelling — that capability is tracked as its own card, relationship traversal (`record.crm_account.type`), and deliberately not as `os.lookup` queries; no schedule is implied by this removal. + + + +Clause-②: yes diff --git a/packages/formula/src/eval-context-no-query-api.test.ts b/packages/formula/src/eval-context-no-query-api.test.ts new file mode 100644 index 00000000000..022fbc58f9c --- /dev/null +++ b/packages/formula/src/eval-context-no-query-api.test.ts @@ -0,0 +1,49 @@ +import { describe, expect, it } from 'vitest'; + +import { celEngine } from './cel-engine'; +import { buildScope } from './stdlib'; +import type { EvalContext } from './types'; +import type { Expression } from '@objectstack/spec'; + +const cel = (source: string): Expression => ({ dialect: 'cel', source }); + +/** + * `EvalContext` once declared an optional `api?: { exists, count, lookup }`, + * and `buildScope()` never read it — so `os.exists` / `os.count` / `os.lookup` + * reached no scope however well a call site cooperated. The member is gone; + * these pins hold it gone in both directions: the type no longer accepts it, + * and a context still carrying one at runtime binds nothing. + * + * The two runtime pins launder their context through `unknown` on purpose — + * the shape they need is exactly the one the type no longer admits. + */ +describe('EvalContext carries no query API', () => { + it('does not declare an `api` member (type-level pin)', () => { + const ctx: EvalContext = { + record: { amount: 10 }, + // @ts-expect-error `api` is not a member of EvalContext. If this + // directive ever reports as unused, the retired kernel query API has + // been re-declared without being bound — reopen the capability question + // rather than restoring the declaration. + api: { lookup: () => null }, + }; + + expect(buildScope(ctx)).toEqual({ record: { amount: 10 } }); + }); + + it('binds no `os` namespace from a context whose only key is a query api', () => { + const scope = buildScope({ api: { exists: () => true } } as unknown as EvalContext); + + expect(scope).toEqual({}); + expect('os' in scope).toBe(false); + }); + + it('refuses a predicate calling os.lookup even when the call site supplies one', () => { + const result = celEngine.evaluate(cel('os.lookup("crm_account", record.account_id) != null'), { + record: { account_id: 'acc_1' }, + api: { lookup: () => ({ type: 'partner' }) }, + } as unknown as EvalContext); + + expect(result.ok).toBe(false); + }); +}); diff --git a/packages/formula/src/types.ts b/packages/formula/src/types.ts index be65387ef43..27f991bbd75 100644 --- a/packages/formula/src/types.ts +++ b/packages/formula/src/types.ts @@ -20,6 +20,17 @@ import type { Expression } from '@objectstack/spec'; * Every field is optional — call sites populate only what they have. The CEL * engine binds `record`, `previous`, `input`, `os` directly as top-level * variables when present. + * + * **It carries data, never a query API.** `buildScope()` binds exactly the + * fields declared here, so an expression reads only what its call site already + * passed in — reaching a row this context does not carry is outside the + * contract. A kernel API (`os.exists` / `os.count` / `os.lookup`) was declared + * here and never bound by `buildScope()`, and it is removed rather than + * implemented because the harm came from the declaration existing, not from + * the implementation missing: a predicate written to it faulted at runtime + * (`found no matching overload for 'dyn.lookup(string, dyn)'`), and an + * unevaluable predicate refuses the write it guards — so a validation rule + * authored against the declaration locked every write on its object. */ export interface EvalContext { /** Logical "now" snapshot — pinned per evaluation run for determinism. */ @@ -63,15 +74,6 @@ export interface EvalContext { previous?: Record; /** Action / flow input payload. */ input?: Record; - /** - * Optional kernel API for `os.exists / os.count / os.lookup`. - * Implemented opportunistically by call sites that have a query engine. - */ - api?: { - exists?: (object: string, predicate: Expression) => boolean; - count?: (object: string, predicate: Expression) => number; - lookup?: (object: string, id: string) => Record | null; - }; /** Free-form bag for niche call sites; merged onto the variable scope. */ extra?: Record; }