Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .changeset/18318-evalcontext-no-query-api.md
Original file line number Diff line number Diff line change
@@ -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.

<!-- adr-0087: not-required (runtime-interface-only packages/formula/src/types.ts#EvalContext) `EvalContext` is a published runtime TypeScript interface with no metadata surface — no Zod schema, no `packages/spec` declaration, no stored representation — so `objectstack migrate meta` has nothing to rewrite and the ledger has no channel to carry. The compiler at the consumer's own call site reaches every affected caller instead. Nothing is re-pointed either: `buildScope()` never read `ctx.api`, so the whole remedy is deleting a property that was already inert. -->

Clause-②: yes
49 changes: 49 additions & 0 deletions packages/formula/src/eval-context-no-query-api.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
20 changes: 11 additions & 9 deletions packages/formula/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -63,15 +74,6 @@ export interface EvalContext {
previous?: Record<string, unknown>;
/** Action / flow input payload. */
input?: Record<string, unknown>;
/**
* 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<string, unknown> | null;
};
/** Free-form bag for niche call sites; merged onto the variable scope. */
extra?: Record<string, unknown>;
}
Expand Down
Loading