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
30 changes: 30 additions & 0 deletions .changeset/36964-flow-option-label-filters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
'@objectstack/lint': minor
'@objectstack/spec': patch
---

fix(lint)!: `os validate` and the publish gate refuse an option label in a flow's query filter, edge condition or decision branch

Clause-②: no (narrowing)
Metadata-protocol: narrowed
Skills-docs: follow-up #37504

<!-- adr-0087: not-required (already-registered publish-refuses-flows-that-save-and-do-not-run) the same option-label refusal at more positions of the same flow; this change extends that entry's surface from the start condition to an edge condition, a decision branch and a query filter operand, at the same doors (os validate / lint / compile / build and the runtime publish gate) -->

**BREAKING** accept-set narrowing, shipped as `minor`: a flow carrying one of the shapes below was accepted before and is refused now, with the fix named in the finding. The schema is unchanged and stored flows are not rewritten or refused at boot; the refusal comes at the next publish of the flow, and from `os validate` / `lint` / `compile` / `build`. No export is added or removed.

A select field stores the option's value, never its label, and a flow hands its filters and conditions to the data engine and the expression engine as written: nothing resolves a label to its value there. So `filter: { stage: 'Closed Lost' }` on a `get_record`, `update_record` or `delete_record` step matched no row, and the step reported success having read, updated or deleted nothing; an edge condition or `decision` branch `deal.stage == 'Closed Lost'` was never taken. `flow-condition-option-label` read only the start condition, so nothing said so.

- **The refusal.** `flow-condition-option-label` (`error`, as on the start condition) now also reads:
- each operand of a `get_record` / `update_record` / `delete_record` `filter`, and of a time-relative sweep's `timeRelative.filter`: a field's implicit equality, its `$eq` / `$ne` comparand and each member of its `$in` / `$nin` list, at the top level or under `$and` / `$or` / `$not`, at any region depth;
- every edge condition and every `decision` node's `conditions[].expression`, at any region depth, where it compares a select field of the trigger record (`record.F`, `previous.F`, or a bare `F`) on a record-change or time-relative flow, or of the row a single-row `get_record` step (`limit` absent or 1) bound to its `outputVariable` (`deal.F`).
- **Not judged.** A CEL value envelope, a variable or a template operand or comparand; a field whose options come from a shared picklist; a variable that anything else in the flow also binds (another step, a declared variable, a node id, a screen field); a `record` / `previous` root the flow rebinds itself; a bare `F` that such a binding shadows. A `readonly` select is still judged, as the start condition already judges it.
- **Escape, filters only.** A filter that is meant to find rows still holding a label (a clean-up after the options changed) writes the operand as a CEL value envelope, `{ dialect: 'cel', source: "'Closed Lost'" }`, which is evaluated before the query and not judged. An edge condition or a `decision` branch has no such escape: compare the value.
- **What carries it.** The ADR-0087 entry `publish-refuses-flows-that-save-and-do-not-run` now names the edge condition, the `decision` branch and the filter operand beside the start condition.

FROM → TO:

- `filter: { stage: 'Closed Lost' }` → `filter: { stage: 'closed_lost' }`
- `deal.stage == 'Closed Lost'` (an edge condition or a `decision` branch) → `deal.stage == 'closed_lost'`

One-line fix: write the option's value where the label is, as the finding names it.
284 changes: 284 additions & 0 deletions packages/lint/src/flow-publish-checks-second-batch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,290 @@ describe('flow-condition-option-label', () => {
});
});

describe('flow-condition-option-label past the start condition (#36964)', () => {
const labelHits = (findings: readonly { rule: string }[]) =>
findings.filter((f) => f.rule === FLOW_CONDITION_OPTION_LABEL) as AnyRec[];

/** A manual flow: no trigger record, so every hit below is the filter's or a bound row's. */
const manual = (nodes: AnyRec[], extra: AnyRec = {}) => stack(flow('sweep', {}, nodes, extra));
const crud = (type: string, filter: unknown, extra: AnyRec = {}) => ({
id: 'step',
type,
label: 'Step',
config: { objectName: 'deal', filter, ...extra },
});

it('refuses a label in a get_record filter, at the operand, naming the value', () => {
const hit = labelHits(validateFlowTriggerReadiness(manual([crud('get_record', { stage: 'Closed Lost' }, { outputVariable: 'deal' })])));
expect(hit).toHaveLength(1);
expect(hit[0]).toMatchObject({ severity: 'error', path: 'flows[0].nodes[1].config.filter.stage' });
expect(hit[0].message).toContain('matches no record');
expect(hit[0].hint).toBe("Filter on the option's value: write 'closed_lost' in place of 'Closed Lost'");
});

it('refuses a label in an update_record and a delete_record filter, operator, list and combinator forms included', () => {
const update = labelHits(
validateFlowTriggerReadiness(manual([crud('update_record', { stage: { $eq: 'Closed Lost' } }, { multi: true, fields: { name: 'x' } })])),
);
expect(update.map((f) => f.path)).toEqual(['flows[0].nodes[1].config.filter.stage.$eq']);

const remove = labelHits(
validateFlowTriggerReadiness(
manual([crud('delete_record', { $or: [{ stage: { $in: ['open', 'Closed Lost'] } }, { tags: 'Hot' }] })]),
),
);
expect(remove.map((f) => f.path)).toEqual([
'flows[0].nodes[1].config.filter.$or[0].stage.$in[1]',
'flows[0].nodes[1].config.filter.$or[1].tags',
]);
});

it('says an excluding operand excludes no record', () => {
const hit = labelHits(validateFlowTriggerReadiness(manual([crud('get_record', { stage: { $ne: 'Closed Lost' } })])));
expect(hit).toHaveLength(1);
expect(hit[0].message).toContain('excludes no record');
});

it('reads a filter inside a region, and a time-relative sweep filter', () => {
const loop = {
id: 'each',
type: 'loop',
label: 'Each',
config: { collection: { dialect: 'cel', source: '[1]' }, iteratorVariable: 'i', body: { nodes: [crud('get_record', { stage: 'Closed Lost' })], edges: [] } },
};
expect(labelHits(validateFlowTriggerReadiness(manual([loop]))).map((f) => f.path)).toEqual([
'flows[0].nodes[1].config.body.nodes[0].config.filter.stage',
]);
const sweep = labelHits(
validateFlowTriggerReadiness(stack(flow('remind', { objectName: 'deal', timeRelative: { ...SWEEP, filter: { stage: 'Closed Lost' } } }))),
);
expect(sweep.map((f) => f.path)).toEqual(['flows[0].nodes[0].config.timeRelative.filter.stage']);
});

it('is silent on the value, an expression operand, a template string, a picklist field and a nested relation', () => {
for (const filter of [
{ stage: 'closed_lost' },
{ stage: { dialect: 'cel', source: "'Closed Lost'" } },
{ stage: '{{wanted_stage}}' },
{ stage: { $in: [{ dialect: 'cel', source: 'wanted' }] } },
{ region: 'Anywhere' },
{ account: { stage: 'Closed Lost' } },
{ stage: { $gt: 'Closed Lost' } },
]) {
expect(rulesOf(validateFlowTriggerReadiness(manual([crud('get_record', filter)]))), JSON.stringify(filter)).not.toContain(
FLOW_CONDITION_OPTION_LABEL,
);
}
});

/** A get_record binding `deal`, then a decision whose out-edges compare. */
const branching = (condition: unknown, getConfig: AnyRec = {}, extra: AnyRec = {}): AnyRec =>
stack({
...flow('route', {}, [], extra),
nodes: [
startNode({}),
{ id: 'load', type: 'get_record', label: 'Load', config: { objectName: 'deal', filter: { id: 'd1' }, outputVariable: 'deal', ...getConfig } },
{ id: 'check', type: 'decision', label: 'Check', config: {} },
{ id: 'lost', type: 'end', label: 'Lost', config: {} },
{ id: 'other', type: 'end', label: 'Other', config: {} },
],
edges: [
{ id: 'e0', source: 'start', target: 'load' },
{ id: 'e1', source: 'load', target: 'check' },
{ id: 'e2', source: 'check', target: 'lost', condition },
{ id: 'e3', source: 'check', target: 'other', isDefault: true },
],
});

it('refuses a decision edge condition comparing a row a get_record bound with a label', () => {
const hit = labelHits(validateFlowTriggerReadiness(branching("deal.stage == 'Closed Lost'")));
expect(hit).toHaveLength(1);
expect(hit[0]).toMatchObject({ severity: 'error', path: 'flows[0].edges[2].condition' });
expect(hit[0].hint).toContain("'closed_lost'");
expect(rulesOf(validateFlowTriggerReadiness(branching({ dialect: 'cel', source: "deal.stage in ['Closed Lost']" })))).toContain(
FLOW_CONDITION_OPTION_LABEL,
);
});

it("refuses an edge on a record-change flow comparing the trigger record, and a decision node's branch", () => {
const onRecord = stack(
flow('lost', { triggerType: 'record-after-update', objectName: 'deal' }, [{ id: 'tell', type: 'end', label: 'Tell', config: {} }]),
);
onRecord.flows[0].edges[0].condition = "record.stage == 'Closed Lost'";
expect(labelHits(validateFlowTriggerReadiness(onRecord)).map((f) => f.path)).toEqual(['flows[0].edges[0].condition']);

const branched = branching(undefined);
branched.flows[0].nodes[2].config = { conditions: [{ label: 'lost', expression: "deal.stage == 'Closed Lost'" }] };
expect(labelHits(validateFlowTriggerReadiness(branched)).map((f) => f.path)).toEqual([
'flows[0].nodes[2].config.conditions[0].expression',
]);
});

it('is silent on the value, an expression comparand, and a row the condition cannot be sure of', () => {
const cases: Array<[string, AnyRec]> = [
['the value', branching("deal.stage == 'closed_lost'")],
['a variable comparand', branching('deal.stage == wanted_stage')],
['a template comparand', branching("deal.stage == '{{wanted_stage}}'")],
['a list read (limit > 1)', branching("deal.stage == 'Closed Lost'", { limit: 5 })],
['a templated object', branching("deal.stage == 'Closed Lost'", { objectName: '{object}' })],
[
'a name also declared as a flow variable',
branching("deal.stage == 'Closed Lost'", {}, { variables: [{ name: 'deal', type: 'object' }] }),
],
['a manual flow reading a bare field (no trigger record)', branching("stage == 'Closed Lost'")],
];
for (const [label, s] of cases) {
expect(rulesOf(validateFlowTriggerReadiness(s)), label).not.toContain(FLOW_CONDITION_OPTION_LABEL);
}
// A bare read on a record-change flow is the trigger field — unless a variable of that name shadows it.
const bare = (extra: AnyRec) => {
const s = stack(
flow('lost', { triggerType: 'record-after-update', objectName: 'deal' }, [{ id: 'tell', type: 'end', label: 'Tell', config: {} }], extra),
);
s.flows[0].edges[0].condition = "stage == 'Closed Lost'";
return rulesOf(validateFlowTriggerReadiness(s));
};
expect(bare({})).toContain(FLOW_CONDITION_OPTION_LABEL);
expect(bare({ variables: [{ name: 'stage', type: 'text' }] })).not.toContain(FLOW_CONDITION_OPTION_LABEL);
});

it('reads $not, $nin and $and, and says which way an excluding operand cuts', () => {
const run = (filter: unknown) => labelHits(validateFlowTriggerReadiness(manual([crud('get_record', filter)])));
// `$not` over `$ne` excludes twice over: the filter still matches no record.
const notNe = run({ $not: { stage: { $ne: 'Closed Lost' } } });
expect(notNe.map((f) => f.path)).toEqual(['flows[0].nodes[1].config.filter.$not.stage.$ne']);
expect(notNe[0].message).toContain('matches no record');
const nin = run({ stage: { $nin: ['open', 'Closed Lost'] } });
expect(nin.map((f) => f.path)).toEqual(['flows[0].nodes[1].config.filter.stage.$nin[1]']);
expect(nin[0].message).toContain('excludes no record');
const and = run({ $and: [{ name: 'x' }, { stage: 'Closed Lost' }] });
expect(and.map((f) => f.path)).toEqual(['flows[0].nodes[1].config.filter.$and[1].stage']);
expect(and[0].message).toContain('matches no record');
});

it('reads an edge inside a loop body and a decision branch inside a parallel branch', () => {
const inLoop = branching(undefined);
inLoop.flows[0].nodes.push({
id: 'each',
type: 'loop',
label: 'Each',
config: {
collection: { dialect: 'cel', source: '[1]' },
iteratorVariable: 'i',
body: {
nodes: [
{ id: 'a', type: 'assignment', label: 'A', config: { assignments: { n: 1 } } },
{ id: 'b', type: 'assignment', label: 'B', config: { assignments: { m: 1 } } },
],
edges: [{ id: 'inner', source: 'a', target: 'b', condition: "deal.stage == 'Closed Lost'" }],
},
},
});
expect(labelHits(validateFlowTriggerReadiness(inLoop)).map((f) => f.path)).toEqual([
'flows[0].nodes[5].config.body.edges[0].condition',
]);

const inBranch = branching(undefined);
inBranch.flows[0].nodes.push({
id: 'fan',
type: 'parallel',
label: 'Fan',
config: {
branches: [
{
name: 'left',
nodes: [{ id: 'pick', type: 'decision', label: 'Pick', config: { conditions: [{ label: 'lost', expression: "deal.stage == 'Closed Lost'" }] } }],
edges: [],
},
],
},
});
expect(labelHits(validateFlowTriggerReadiness(inBranch)).map((f) => f.path)).toEqual([
'flows[0].nodes[5].config.branches[0].nodes[0].config.conditions[0].expression',
]);
});

it("reads a time-relative flow's trigger record, and previous.F on a record-change flow", () => {
const sweep = stack(flow('remind', { objectName: 'deal', timeRelative: SWEEP }, [{ id: 'tell', type: 'end', label: 'Tell', config: {} }]));
sweep.flows[0].edges[0].condition = "record.stage == 'Closed Lost'";
expect(labelHits(validateFlowTriggerReadiness(sweep)).map((f) => f.path)).toEqual(['flows[0].edges[0].condition']);

const onUpdate = stack(
flow('lost', { triggerType: 'record-after-update', objectName: 'deal' }, [{ id: 'tell', type: 'end', label: 'Tell', config: {} }]),
);
onUpdate.flows[0].edges[0].condition = "previous.stage != 'Closed Lost'";
expect(labelHits(validateFlowTriggerReadiness(onUpdate)).map((f) => f.path)).toEqual(['flows[0].edges[0].condition']);
});

/** A second object whose `stage` stores the words the deal's `stage` uses as labels. */
const LEAD = {
name: 'lead',
label: 'Lead',
fields: { stage: { type: 'select', label: 'Stage', options: ['Open', 'Closed Lost'] } },
};

it('is silent when the flow rebinds `record` to another row (get_record, loop iterator, assignment)', () => {
const rebinding = (binder: AnyRec) => {
const f = flow('lost', { triggerType: 'record-after-update', objectName: 'deal' }, [
binder,
{ id: 'tell', type: 'end', label: 'Tell', config: {} },
]);
f.edges[1].condition = "record.stage == 'Closed Lost'";
return { objects: [DEAL, LEAD], flows: [f] };
};
const binders: AnyRec[] = [
{ id: 'load', type: 'get_record', label: 'Load', config: { objectName: 'lead', filter: { id: 'l1' }, outputVariable: 'record' } },
{ id: 'set', type: 'assignment', label: 'Set', config: { assignments: { record: { dialect: 'cel', source: 'lead_row' } } } },
{ id: 'each', type: 'loop', label: 'Each', config: { collection: { dialect: 'cel', source: 'leads' }, iteratorVariable: 'record', body: { nodes: [], edges: [] } } },
];
for (const binder of binders) {
expect(rulesOf(validateFlowTriggerReadiness(rebinding(binder))), binder.type).not.toContain(FLOW_CONDITION_OPTION_LABEL);
}
// Control: the same flow with a binder that does not touch `record` is reported.
const control = rebinding({ id: 'set', type: 'assignment', label: 'Set', config: { assignments: { other: 1 } } });
expect(rulesOf(validateFlowTriggerReadiness(control))).toContain(FLOW_CONDITION_OPTION_LABEL);
});

it('drops a variable two get_record steps bind to different objects, and one a node id or a screen field shadows', () => {
const twoObjects = branching("deal.stage == 'Closed Lost'");
twoObjects.objects = [DEAL, LEAD];
twoObjects.flows[0].nodes.push({ id: 'load2', type: 'get_record', label: 'Load 2', config: { objectName: 'lead', filter: { id: 'l1' }, outputVariable: 'deal' } });
expect(rulesOf(validateFlowTriggerReadiness(twoObjects))).not.toContain(FLOW_CONDITION_OPTION_LABEL);

const nodeId = branching("deal.stage == 'Closed Lost'");
nodeId.flows[0].nodes.push({ id: 'deal', type: 'end', label: 'Deal end', config: {} });
expect(rulesOf(validateFlowTriggerReadiness(nodeId))).not.toContain(FLOW_CONDITION_OPTION_LABEL);

const screenField = branching("deal.stage == 'Closed Lost'");
screenField.flows[0].nodes.push({ id: 'ask', type: 'screen', label: 'Ask', config: { fields: [{ name: 'deal', label: 'Deal', type: 'text' }] } });
expect(rulesOf(validateFlowTriggerReadiness(screenField))).not.toContain(FLOW_CONDITION_OPTION_LABEL);

// The bare read of the trigger field, shadowed by a node id and by a screen field.
const bare = (node: AnyRec) => {
const s = stack(
flow('lost', { triggerType: 'record-after-update', objectName: 'deal' }, [node, { id: 'tell', type: 'end', label: 'Tell', config: {} }]),
);
s.flows[0].edges[1].condition = "stage == 'Closed Lost'";
return rulesOf(validateFlowTriggerReadiness(s));
};
expect(bare({ id: 'stage', type: 'assignment', label: 'S', config: { assignments: { n: 1 } } })).not.toContain(FLOW_CONDITION_OPTION_LABEL);
expect(bare({ id: 'ask', type: 'screen', label: 'Ask', config: { fields: [{ name: 'stage', label: 'Stage', type: 'text' }] } })).not.toContain(
FLOW_CONDITION_OPTION_LABEL,
);
expect(bare({ id: 'other', type: 'assignment', label: 'O', config: { assignments: { n: 1 } } })).toContain(FLOW_CONDITION_OPTION_LABEL);
});

it('os validate reports it, and the runtime publish gate refuses the flow write', () => {
const s = branching("deal.stage == 'Closed Lost'");
expect(rulesOf(runAuthoringRules('validate', { normalized: s } as never))).toContain(FLOW_CONDITION_OPTION_LABEL);
const verdict = runRuntimeAuthoringRules({ type: 'flow', item: s.flows[0], context: { objects: [DEAL] } });
expect(rulesOf(verdict.errors)).toContain(FLOW_CONDITION_OPTION_LABEL);
const filtered = manual([crud('update_record', { stage: 'Closed Lost' }, { fields: { name: 'x' } })]);
const filterVerdict = runRuntimeAuthoringRules({ type: 'flow', item: filtered.flows[0], context: { objects: [DEAL] } });
expect(rulesOf(filterVerdict.errors)).toContain(FLOW_CONDITION_OPTION_LABEL);
});
});

describe('flow-time-relative-shadowed', () => {
it('refuses a record-* token beside a sweep descriptor', () => {
const findings = validateFlowTriggerReadiness(
Expand Down
Loading
Loading