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
25 changes: 25 additions & 0 deletions .changeset/33435-injected-column-null-guard-at-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
'@objectstack/lint': minor
'@objectstack/spec': minor
---

fix(lint)!: `os build` refuses a flow or hook condition that orders or does arithmetic on an injected column a stored row can hold null in, as the publish gate already does

Clause-②: no (narrowing)
Metadata-protocol: narrowed
Skills-docs: none — no skill under `skills/**` teaches a condition over an injected column or the null guard on one; `content/docs/automation/flows.mdx` and `content/docs/automation/hooks.mdx` gain the rule in this change.

<!-- adr-0087: registered flow-hook-condition-injected-column-null-guard-at-build -->

**BREAKING** for a stack whose record-triggered flow (start `condition`, edge `condition`, `decision` branch `expression`) or hook `condition` applies `<`, `<=`, `>`, `>=` or `+ - * / %` to an injected system column the object does not declare itself, with no `!= null` guard in the same branch. `os build` judged the null guard over the declared fields only and accepted these; the runtime publish gate, which reads the object as served with those columns declared, refused them. A stored row holds `null` there: a system-context insert stores `null` in `owner_id`, `created_by` and `updated_by`, a seeded row keeps `created_by` and `updated_by` `null`, nothing stamps `owning_business_unit_id` yet, `queue` is `null` until a queue write names one, a `beforeInsert` hook reads `organization_id` before it is resolved, and a `beforeInsert` hook with a `priority` below 10 reads `created_at` and `updated_at` before they are stamped. On such a row the flow run fails at the condition while the write that fired it lands, and a hook condition fails the write.

What moves (each line FROM → TO):

- `record.owner_id > 'a'` (or `created_by`, `updated_by`, `owning_business_unit_id`, `queue`, `organization_id`; bare in a flow too) at a record-triggered flow's start, edge or branch, or at a hook's `condition`: FROM accepted by `os build` and `os validate` TO refused with the publish gate's `expression-invalid` null-guard finding.
- `record.created_at > timestamp('2025-01-01T00:00:00Z')` (or `updated_at`) at a hook whose `events` include `beforeInsert`: FROM accepted by `os build` TO refused, as the publish gate refuses it.
- The MCP `validate_expression` tool at site `flow_condition` (it judges the object as authored): FROM a clean answer TO the publish gate's null-guard finding for the same conditions.
- A `publish-drafts` batch that carries a draft of the flow's or hook's own object (the draft body, which holds no injected column, replaces the served object): FROM published TO refused, as the single-item publish already refused it over the served object.

The fix: guard the column in the same branch, `record.owner_id != null && record.owner_id > 'a'`. A flow's start-condition guard holds at every edge and branch; `has()` is no guard.

Unchanged: the single-item publish gate's verdicts, and a batch's when it carries no draft of the flow's or hook's object; a column the object declares itself, which keeps the verdict its own keys give; `created_at` / `updated_at` at every other hook event and flow slot; equality (`==`, `!=`) on any column; and validation rules and `requiredWhen`, which both doors judge over the declared fields only.
5 changes: 5 additions & 0 deletions content/docs/automation/flows.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2602,6 +2602,11 @@ and is read as `vars["current_user"]` / `vars["vars"]`.
while the write that fired it still lands. A bare `budget` is `record.budget`,
and `os validate` judges it too; a start guard holds at every edge and branch — spell
it `record.budget != null`, which both doors accept, and `has()` is no guard.
The columns the platform adds count too: `owner_id`, `created_by`,
`updated_by`, `owning_business_unit_id`, `queue` and `organization_id` are
`null` on a row a system write, a seed or a guest left them empty in, so they
need the same guard; `created_at` and `updated_at` are stamped on every
stored row and need none.
</Callout>

<Callout type="info">
Expand Down
11 changes: 11 additions & 0 deletions content/docs/automation/hooks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ Because `record` now means the record's *state*, `record.done == true` alone is
on **every** update of an already-done row. If you wrote a condition under the old
payload semantics expecting "the write that changed it", add the `previous` half.

**A field that can be `null` needs a `!= null` guard before `<`, `>`, `+` and the
rest** — CEL has no ordering or arithmetic over `null`, so the condition faults and
the write fails. `os build` and the publish gate refuse the unguarded form. That
holds for the columns the platform adds as well: `owner_id`, `created_by`,
`updated_by`, `owning_business_unit_id`, `queue` and `organization_id` are `null` on
a row a system write, a seed or a guest left them empty in, so write
`record.owner_id != null && record.owner_id > 'a'`. `created_at` and `updated_at`
are stamped on every stored row and need no guard, except in a `beforeInsert` hook:
one with a `priority` below 10 runs before the stamp and reads them as `null`, so
`os build` and the publish gate ask for the guard at every `beforeInsert` hook.

## Elevation — `runAs`

A hook's `ctx.api` runs with the context of the write that **fired** it. That is usually what you want, and it is the one thing that makes a column both *guarded* and *maintained* impossible to express: author `editable: false` (or `readonly: true`) so nobody hand-writes the column, and the hook that computes it is refused by the very same check. The guard and the legitimate writer are the same door.
Expand Down
13 changes: 13 additions & 0 deletions packages/lint/src/authored-site-expression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,19 @@ describe('judgeAuthoredSiteExpression at a flow condition — every gate pass is
expect(flowEntryOn([withOwn, BAL], "record.title == 'x'")).toEqual([]);
});

// [#33435] The entry strips the injected columns and runs the walk as a flow
// write, so the build's injected-column null guard reaches it: an unguarded
// ordering over `owner_id` comes back with the publish gate's finding (it
// answered clean before), and the guarded spelling stays clean.
it('an unguarded ordering over an injected column returns the publish gate\'s null-guard finding (#33435)', () => {
const served = applyInjectedSystemColumns(FLOW_OBJ);
const entry = flowEntryOn([served], "record.owner_id > 'a'");
expect(entry).toHaveLength(1);
expect(entry[0]![1]).toContain('`record.owner_id`');
expect(entry).toEqual(flowGateOn([served], "record.owner_id > 'a'"));
expect(flowEntryOn([served], "record.owner_id != null && record.owner_id > 'a'")).toEqual([]);
});

it('answers nothing when the object is not among the definitions', () => {
expect(judgeAuthoredSiteExpression({ site: 'flow_condition', objectName: 'nope', expression: 'record.days > 3', objects: [FLOW_OBJ] }))
.toEqual([]);
Expand Down
104 changes: 103 additions & 1 deletion packages/lint/src/validate-expressions.flow-null-guards.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,13 @@ describe('flow conditions under the null-guard gate (#29026)', () => {
// hook (no record), and the other injected columns, which can hold null on a
// stored row. The type verdicts on an injected column are untouched: the door
// keeps the served types.
describe('the publish door judges a flow or hook condition over an injected system column by what a stored row holds (#33246)', () => {
// #33435 — `os build` reads the objects as authored, with no injected column
// declared, and used to leave every one of them out of the null-guard gate, so
// it accepted what the door refused. The flow and hook condition sites now
// judge the injected columns the authored object does not declare as the door
// judges them over the served object: the six a stored row can hold null in
// at every site, and `created_at` / `updated_at` at a `beforeInsert` hook only.
describe('a flow or hook condition over an injected system column is judged by what a stored row holds, at the publish door and at os build alike (#33246, #33435)', () => {
/** The trigger object as authored; `sharingModel` keeps `security-owd-unset` quiet. */
const account = {
name: 'crm_account',
Expand Down Expand Up @@ -421,6 +427,102 @@ describe('the publish door judges a flow or hook condition over an injected syst
}
});

/** The six injected columns a stored row can hold null in. */
const NULLABLE_INJECTED = ['owner_id', 'created_by', 'updated_by', 'owning_business_unit_id', 'queue', 'organization_id'];

it('⭐ LIT — os build refuses an unguarded ordering over each injected column a stored row can hold null in, with the door\'s finding, at every flow slot and at a hook', () => {
for (const column of NULLABLE_INJECTED) {
const condition = `record.${column} > 'a'`;
for (const write of writes(condition)) {
const build = atBuild(write);
expect(build, label(write, condition)).toHaveLength(1);
expect(build.filter(nullGuardOn(column)), label(write, condition)).toHaveLength(1);
expect(build, label(write, condition)).toEqual(atDoor(write));
}
}
});

it('⭐ LIT — os build judges a flow\'s bare spelling of an injected column as it judges a declared nullable field\'s', () => {
// The door's per-write snapshot judges no bare name, a declared field's
// included (the CONTROL leg), so the build is the one reader here.
for (const [condition, column] of [["owner_id > 'a'", 'owner_id'], ['amount > 0', 'amount']] as const) {
const write: Write = { type: 'flow', item: flowAt('start', condition), collection: 'flows' };
const build = atBuild(write);
expect(build, condition).toHaveLength(1);
expect(build[0]!.message, condition).toContain(`applies \`>\` to \`${column}\``);
expect(atDoor(write), condition).toEqual([]);
}
});

it('⭐ LIT — on each ownership tier os build judges exactly the injected columns the tier carries, as the door does', () => {
for (const ownership of ['business_unit', 'org', 'none'] as const) {
const tiered = { ...account, ownership };
const servedTier = applyInjectedSystemColumns(tiered);
for (const column of NULLABLE_INJECTED) {
const condition = `record.${column} > 'a'`;
for (const write of writes(condition)) {
const build = atBuild(write, [tiered]);
expect(build.filter(nullGuardOn(column)).length, `${ownership} · ${label(write, condition)}`)
.toBe(column in servedTier.fields ? 1 : 0);
expect(build, `${ownership} · ${label(write, condition)}`).toEqual(atDoor(write, [servedTier]));
}
}
}
});

it('⭐ LIT — os build refuses an ordering over `created_at` / `updated_at` at a `beforeInsert` hook, and only there, as the door does', () => {
for (const condition of [
"record.created_at > timestamp('2025-01-01T00:00:00Z')",
'record.updated_at > record.created_at',
]) {
for (const events of [['beforeInsert'], ['beforeInsert', 'beforeUpdate']]) {
for (const object of ['crm_account', ['crm_account']]) {
const write: Write = { type: 'hook', item: { ...hookOn(object, condition), events }, collection: 'hooks' };
expect(atBuild(write).filter((f) => f.message.includes('declares as nullable')).length, `${events.join(',')}: ${condition}`)
.toBeGreaterThan(0);
expect(atBuild(write), `${events.join(',')}: ${condition}`).toEqual(atDoor(write));
}
}
// A hook on a stamped stored row, and every flow slot: the build accepts, as the door does.
for (const write of [
...writes(condition),
{ type: 'hook', item: { ...hookOn('crm_account', condition), events: ['afterInsert'] }, collection: 'hooks' } as Write,
]) {
expect(atBuild(write), label(write, condition)).toEqual([]);
expect(atDoor(write), label(write, condition)).toEqual([]);
}
}
});

it('⭐ CONTROL — the guarded spelling publishes and builds over every injected column', () => {
for (const column of NULLABLE_INJECTED) {
const condition = `record.${column} != null && record.${column} > 'a'`;
for (const write of writes(condition)) {
expect(atBuild(write), label(write, condition)).toEqual([]);
expect(atDoor(write), label(write, condition)).toEqual([]);
}
}
});

it('⭐ CONTROL — an injected column the author declares keeps the author\'s verdict at the build, as at the door', () => {
// `required: true` is the author's own claim that the column is valued.
const declared = { ...account, fields: { ...account.fields, owner_id: { type: 'lookup', reference: 'sys_user', label: 'Owner', required: true } } };
const condition = "record.owner_id > 'a'";
for (const write of writes(condition)) {
expect(atBuild(write, [declared]), label(write, condition)).toEqual([]);
expect(atDoor(write, [applyInjectedSystemColumns(declared)]), label(write, condition)).toEqual([]);
}
});

it('⭐ CONTROL — a validation rule over an injected column keeps its verdict: the build judges declared fields only there, as the object door does', () => {
const ruled = { ...account, validations: [{ name: 'owner_named', type: 'script', condition: "record.owner_id > 'a'", message: 'Owner' }] };
const stack = { objects: [ruled] };
const build = runAuthoringRules('build', { normalized: stack, parsed: stack }).filter((f) => f.rule === EXPRESSION_INVALID).map(shape);
expect(build).toEqual([]);
const { errors, advisories } = runRuntimeAuthoringRules({ type: 'object', item: ruled, context: { objects: [served] } });
expect([...errors, ...advisories].filter((f) => f.rule === EXPRESSION_INVALID)).toEqual([]);
});

it('⭐ CONTROL — an unguarded ordering over a declared nullable field is refused at the door and at the build alike', () => {
for (const write of writes('record.amount > 0')) {
const door = atDoor(write);
Expand Down
6 changes: 6 additions & 0 deletions packages/lint/src/validate-expressions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4482,6 +4482,12 @@ describe('validateStackExpressions — reads only keys the spec declares (meta-t
// check; its keys are Array methods. The metadata read is `hook.events`,
// on the tabled `hook` receiver.
'hookEvents',
// [#33435] The flow and hook sites' null-guard index (the declared one
// joined with the injected columns the object as read does not carry),
// the pre-stamp index, and the join's own Maps and Sets. Their keys are
// Map / Set methods, never metadata keys; the injected columns are read
// off the spec's `injectedSystemColumnDefs` over the tabled `obj`.
'flowHookNullableIndex', 'preStampIndex', 'carriedFieldNames', 'joinedNullable', 'joinedFields',
// [#29926] The fault-outcome ledger's own site descriptor (`kind`,
// `severity`, `triggerKind`, `sweepsTypedObject`), built at each call site from the
// tabled receivers (`rule.severity`) — never read off metadata itself.
Expand Down
Loading
Loading