Skip to content

Commit de1f2cc

Browse files
committed
fix(lint): a flow condition's bare-name type verdict stands down where a screen field or an open binder can hold the name
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGufzicfrt7Yr8FaVdDsXD
1 parent c23501b commit de1f2cc

6 files changed

Lines changed: 236 additions & 24 deletions

File tree

‎packages/formula/src/validate.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,39 @@ describe('validateExpression (ADR-0032)', () => {
851851
.toEqual(['type-mismatch']);
852852
});
853853

854+
// [#30025] Where the run can bind a bare name no reader sees (an open
855+
// binder node, a contract-less screen), a bare-name mismatch is not proven
856+
// to be the field's: it takes no declared outcome and is the run-may-fail
857+
// warning. A `record.<field>` read still is the field and keeps the error,
858+
// also when a bare mismatch comes first in the same source.
859+
it('drops a bare-name mismatch to the run-may-fail warning where bare names are unproven, keeping record.<field> refused (#30025)', () => {
860+
const flat = { ...schema, scope: 'flattened' as const, faultOutcome: 'run-fails' as const, bareNamesUnproven: true };
861+
for (const source of ['name > 5', 'is_active > 0', 'name * 2 > 1']) {
862+
const r = validateExpression('predicate', source, flat);
863+
expect(r.errors, source).toEqual([]);
864+
expect(r.warnings.map((w) => w.code), source).toEqual(['type-mismatch-run-may-fail']);
865+
expect(r.warnings[0].message, source).toContain('which this check cannot prove here');
866+
}
867+
for (const [source, reference] of [
868+
['record.name > 5', 'record.name'],
869+
['previous.is_active > 0', 'previous.is_active'],
870+
['name > 5 && record.title > 5', 'record.title'],
871+
] as const) {
872+
const r = validateExpression('predicate', source, flat);
873+
expect(r.warnings, source).toEqual([]);
874+
expect(r.errors.map((e) => [e.code, (e.params as { reference?: string }).reference]), source)
875+
.toEqual([['type-mismatch-run-fails', reference]]);
876+
}
877+
// Controls: the key absent is the error as before; a bare name the run
878+
// binds as a variable is still untyped; a `record`-scoped site ignores it.
879+
expect(validateExpression('predicate', 'name > 5', { ...flat, bareNamesUnproven: undefined }).errors.map((e) => e.code))
880+
.toEqual(['type-mismatch-run-fails']);
881+
const shadowed = validateExpression('predicate', 'name > 5', { ...flat, flowVariables: ['name'] });
882+
expect([shadowed.errors, shadowed.warnings]).toEqual([[], []]);
883+
expect(validateExpression('predicate', 'record.name > 5', { ...schema, faultOutcome: 'fail-closed' as const, bareNamesUnproven: true }).errors.map((e) => e.code))
884+
.toEqual(['type-mismatch-fail-closed']);
885+
});
886+
854887
// [#29959] With no declared outcome a flattened condition is a flow's
855888
// that is not proven to bind the field: a warning, but one saying the run
856889
// fails on the fault, since a flow condition never reads null.

‎packages/formula/src/validate.ts‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,18 @@ export interface ExprSchemaHint {
267267
* typed. Absent ⇒ every bare field name is typed, exactly as before this key.
268268
*/
269269
flowVariables?: readonly string[];
270+
/**
271+
* [#30025] `'flattened'` scope only: the flow's run can bind a name no static
272+
* reader sees — a node that hands out the live variable map or folds a
273+
* resume bag in (`script`, `connector_action`, `wait`, `subflow`, `map`, a
274+
* screen with no input contract), or an entrance whose record is not in hand
275+
* (`@objectstack/lint`'s `flowCelRootScope`, `provable: false`). A bare name
276+
* is then not proven to hold the field, so a bare-name `type-mismatch` takes
277+
* no declared {@link faultOutcome}: it is the `type-mismatch-run-may-fail`
278+
* warning. A `record.<name>` / `previous.<name>` read still reads the field
279+
* and keeps the outcome. Absent ⇒ every verdict is exactly as before this key.
280+
*/
281+
bareNamesUnproven?: boolean;
270282
}
271283

272284
/**
@@ -414,14 +426,29 @@ function typeSoundnessIssue(
414426
role: 'predicate' | 'value',
415427
faultOutcome: ExprSchemaHint['faultOutcome'],
416428
flowVariables?: readonly string[],
429+
bareNamesUnproven?: boolean,
417430
): { issue: ExprValidationError; severity: 'error' | 'warning' } | null {
418431
const celFieldTypes = toCelFieldTypes(fieldTypes);
419432
// [#29959] A bare name a flow variable holds is not the field (see
420433
// `ExprSchemaHint.flowVariables`), for every bare-name verdict here.
421434
const shadowed = scope === 'flattened' && flowVariables && flowVariables.length > 0
422435
? new Set(flowVariables)
423436
: undefined;
424-
const mismatch = firstTypeMismatch(source, celFieldTypes, scope, shadowed);
437+
let mismatch = firstTypeMismatch(source, celFieldTypes, scope, shadowed);
438+
// [#30025] Where the run can bind a bare name no reader sees
439+
// (`ExprSchemaHint.bareNamesUnproven`), a bare-name mismatch is not proven to
440+
// be the field's, so it takes no declared outcome. A `record.<field>` /
441+
// `previous.<field>` mismatch in the same source still does: with every bare
442+
// name left `dyn`, only the record-root pass can answer, and its answer wins.
443+
let outcome = faultOutcome;
444+
if (
445+
mismatch && scope === 'flattened' && bareNamesUnproven && faultOutcome !== undefined
446+
&& mismatch.category === 'type-mismatch' && mismatch.root === undefined
447+
) {
448+
const rootRead = firstTypeMismatch(source, celFieldTypes, scope, new Set(Object.keys(celFieldTypes)));
449+
if (rootRead && rootRead.root !== undefined) mismatch = rootRead;
450+
else outcome = undefined;
451+
}
425452
if (!mismatch) {
426453
const bareCelTypes = shadowed
427454
? Object.fromEntries(Object.entries(celFieldTypes).filter(([name]) => !shadowed.has(name)))
@@ -472,10 +499,10 @@ function typeSoundnessIssue(
472499
`type mismatch \`${mismatch.operands}\` — ${subject} but is used with \`${mismatch.operator}\` ` +
473500
`against a number. CEL has no such operation, so wherever the value is not numeric text ` +
474501
`(an empty value included) the expression faults`;
475-
if (faultOutcome !== undefined) {
502+
if (outcome !== undefined) {
476503
// [#29926] One code per outcome: a code names ONE message template
477504
// (`expression-refusal.ts`), and the three outcomes are three sentences.
478-
const arm = TYPE_MISMATCH_FAULT_ARMS[faultOutcome];
505+
const arm = TYPE_MISMATCH_FAULT_ARMS[outcome];
479506
return {
480507
severity: 'error',
481508
issue: refusal(arm.code, params, source, `${faults}, and here ${arm.clause} ${convert}`),
@@ -1405,7 +1432,8 @@ export function validateExpression(
14051432
// date-field arithmetic (error). Flow variables stay `dyn` (never flagged);
14061433
// equality/ordering of a date is runtime-safe (never flagged).
14071434
if (schema.fieldTypes) {
1408-
const r = typeSoundnessIssue(source, schema.fieldTypes, 'flattened', role, schema.faultOutcome, schema.flowVariables);
1435+
const r = typeSoundnessIssue(source, schema.fieldTypes, 'flattened', role, schema.faultOutcome, schema.flowVariables,
1436+
schema.bareNamesUnproven);
14091437
if (r) (r.severity === 'error' ? errors : warnings).push(r.issue);
14101438
}
14111439
}

‎packages/lint/src/flow-variable-scope.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ export const VARIABLE_NAME_CONFIG_KEYS = ['iteratorVariable', 'indexVariable', '
131131
*/
132132
const ASSIGNMENT_NODE_TYPE = 'assignment';
133133

134+
/** The node type whose resume binds its declared fields' names ({@link collectScreenFieldNames}). */
135+
const SCREEN_NODE_TYPE = 'screen';
136+
134137
/**
135138
* The keys an `assignments` ARRAY entry may name its target with — the three
136139
* `logic-nodes.ts` reads, in its precedence order. Exported so the guard can
@@ -248,6 +251,36 @@ export function collectFlowVariableNames(
248251
return names;
249252
}
250253

254+
/**
255+
* [#30025] Every `screen` node's field `name`, across every graph of the flow:
256+
* the names a resume binds. A screen's collected inputs land under their plain
257+
* names (`applyResumeSignal`, the resume bag's `variables`), over whatever the
258+
* run flattened there, so downstream of the screen a bare read of one holds the
259+
* collected value, not the record's field. {@link collectFlowVariableNames}
260+
* does not collect them (its consumers predate the screen row), so a caller
261+
* that needs every name a bare read can hold instead of the field unions both.
262+
*
263+
* Only the names a screen DECLARES: a screen with no input contract folds any
264+
* bag, which no reader can enumerate — `flowCelRootScope` reports such a flow
265+
* as not provable, and that is the caller's other half.
266+
*/
267+
export function collectScreenFieldNames(graphs: readonly FlowGraphLike[]): ReadonlySet<string> {
268+
const names = new Set<string>();
269+
for (const graph of graphs) {
270+
for (const flowNode of recordsOf(graph.nodes)) {
271+
if (flowNode.type !== SCREEN_NODE_TYPE) continue;
272+
const rawConfig = flowNode.config;
273+
if (!rawConfig || typeof rawConfig !== 'object' || Array.isArray(rawConfig)) continue;
274+
const fields = (rawConfig as AnyRec).fields;
275+
if (!Array.isArray(fields)) continue;
276+
for (const field of recordsOf(fields)) {
277+
if (typeof field.name === 'string' && field.name) names.add(field.name);
278+
}
279+
}
280+
}
281+
return names;
282+
}
283+
251284
/**
252285
* Upper bound on the bare-root enumeration below. A predicate reaching for more
253286
* distinct undeclared roots than this is pathological, and the loop must

‎packages/lint/src/validate-expressions.test.ts‎

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,83 @@ describe('validateStackExpressions (ADR-0032 build-time)', () => {
11871187
]);
11881188
});
11891189

1190+
// [#30025] A screen's resume lands its collected values under their plain
1191+
// names (`applyResumeSignal`), over the flattened field: a bare `title`
1192+
// downstream reads the NUMBER the screen collected, and `title > 5` runs.
1193+
// Where a node can bind a name no reader sees (`script` here, a screen with
1194+
// no input contract), a bare name is not proven to be the field: the verdict
1195+
// is the run-may-fail warning. `record.title` is the field either way.
1196+
describe('a bare name a screen or an open binder can hold (#30025)', () => {
1197+
const rtFlow = (name: string, mid: Record<string, unknown>): Record<string, unknown> => ({
1198+
name,
1199+
nodes: [
1200+
{ id: 'start', type: 'start', config: { objectName: 'crm_opportunity', triggerType: 'record-after-update' } },
1201+
mid,
1202+
{ id: 'route', type: 'decision', config: { conditions: [{ label: 'Big', expression: 'title > 5' }, { label: 'Long', expression: 'record.title > 5' }] } },
1203+
{ id: 'end', type: 'end' },
1204+
],
1205+
edges: [
1206+
{ id: 'e1', source: 'start', target: 'mid' },
1207+
{ id: 'e2', source: 'mid', target: 'route', condition: 'title > 5' },
1208+
{ id: 'e3', source: 'route', target: 'end', label: 'Big' },
1209+
{ id: 'e4', source: 'route', target: 'end', label: 'Long' },
1210+
],
1211+
});
1212+
const objects = [{ name: 'crm_opportunity', fields: { title: { type: 'text', required: true } } }];
1213+
const longBranch = (flow: string): string =>
1214+
`flow '${flow}' · node 'route' (decision) decision branch expression at config.conditions[1].expression`;
1215+
1216+
it('does not type a bare name a screen field holds as the field it shadows', () => {
1217+
const issues = validateStackExpressions({
1218+
objects,
1219+
flows: [rtFlow('rt_screen', { id: 'mid', type: 'screen', config: { fields: [{ name: 'title', type: 'number', label: 'Title' }] } })],
1220+
});
1221+
const found = typeMismatches(issues);
1222+
expect(found.map(i => [i.where, i.severity])).toEqual([[longBranch('rt_screen'), 'error']]);
1223+
expect(found[0]!.message).toContain('`record.title` holds text');
1224+
});
1225+
1226+
it('warns, does not refuse, a bare-name mismatch after a node that can bind any name', () => {
1227+
for (const [flow, mid] of [
1228+
['rt_script', { id: 'mid', type: 'script', config: { functionName: 'calc' } }],
1229+
['rt_formscreen', { id: 'mid', type: 'screen', config: { kind: 'object-form', objectName: 'crm_opportunity' } }],
1230+
] as const) {
1231+
const issues = validateStackExpressions({ objects, flows: [rtFlow(flow, mid)] });
1232+
const found = typeMismatches(issues);
1233+
expect(found.map(i => [i.where, i.severity]), flow).toEqual([
1234+
[`flow '${flow}' · node 'route' (decision) decision branch expression at config.conditions[0].expression`, 'warning'],
1235+
[longBranch(flow), 'error'],
1236+
[`flow '${flow}' · edge 'e2' (mid→route) condition`, 'warning'],
1237+
]);
1238+
for (const i of found.filter(f => f.severity === 'warning')) {
1239+
expect(i.message, `${flow} ${i.where}`).toContain('which this check cannot prove here, so this is a warning, not a refusal');
1240+
}
1241+
}
1242+
});
1243+
1244+
it('leaves the null-guard twin silent on a bare name a screen field holds', () => {
1245+
const flow = (name: string, fields: unknown[]) => ({
1246+
name,
1247+
nodes: [
1248+
{ id: 'start', type: 'start', config: { objectName: 'crm_opportunity', triggerType: 'record-after-update' } },
1249+
{ id: 'mid', type: 'screen', config: { fields } },
1250+
{ id: 'end', type: 'end' },
1251+
],
1252+
edges: [{ id: 'e1', source: 'start', target: 'mid' }, { id: 'e2', source: 'mid', target: 'end', condition: 'budget > 100' }],
1253+
});
1254+
const nullable = (issues: ReturnType<typeof validateStackExpressions>) =>
1255+
issues.filter(i => /declares as nullable/.test(i.message)).map(i => [i.where, i.severity]);
1256+
const budgetObjects = [{ name: 'crm_opportunity', fields: { budget: { type: 'number' } } }];
1257+
expect(nullable(validateStackExpressions({
1258+
objects: budgetObjects, flows: [flow('ng_screen', [{ name: 'budget', type: 'number', label: 'Budget' }])],
1259+
}))).toEqual([]);
1260+
// Control: a screen collecting another name leaves `budget` the field, refused.
1261+
expect(nullable(validateStackExpressions({
1262+
objects: budgetObjects, flows: [flow('ng_other', [{ name: 'note', type: 'text', label: 'Note' }])],
1263+
}))).toEqual([["flow 'ng_other' · edge 'e2' (mid→end) condition", 'error']]);
1264+
});
1265+
});
1266+
11901267
it('keeps the warning on an inert node condition of a record-triggered flow (#29926)', () => {
11911268
const issues = validateStackExpressions({
11921269
objects: [{ name: 'crm_opportunity', fields: { title: { type: 'text', required: true } } }],
@@ -4128,10 +4205,14 @@ describe('validateStackExpressions — reads only keys the spec declares (meta-t
41284205
// tabled receivers (`rule.severity`) — never read off metadata itself.
41294206
'site',
41304207
// [#29959] A `decision` branch's flow-condition hint (`objectName`,
4131-
// `faultOutcome`, `flowVariables`), built at the call site from the
4208+
// `faultOutcome`, `flowBare`), built at the call site from the
41324209
// walk's own `objectName`, the ledger's answer and the flow's bound
41334210
// names — never read off metadata itself.
41344211
'flowCondition',
4212+
// [#30025] One flow's bare-name facts (`variables`, `unproven`), built
4213+
// from the flow's collected names and the root judge's `provable`, and
4214+
// its screen-field name set (Set methods) — never read off metadata.
4215+
'flowBare', 'screenFields',
41354216
]);
41364217
expect(receivers.filter((r) => !tabled.has(r) && !PLUMBING.has(r))).toEqual([]);
41374218
});

0 commit comments

Comments
 (0)