Skip to content

Commit 4258030

Browse files
committed
fix(lint): type a time-relative flow's conditions against its swept object when the start names no objectName
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Utp6CaUg3ibF939L2RFB5D
1 parent 6f095b9 commit 4258030

2 files changed

Lines changed: 132 additions & 17 deletions

File tree

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

Lines changed: 82 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,15 +1107,19 @@ describe('validateStackExpressions (ADR-0032 build-time)', () => {
11071107
// record-change trigger's — so a sweep of the object the conditions are
11081108
// typed against gets the run-fails verdict, bare name included. A sweep of
11091109
// another object binds a row of THAT object: the warning stays.
1110-
it('refuses a type mismatch on a time-relative sweep of the flow object, and warns on a sweep of another (#29959)', () => {
1111-
const flowOf = (name: string, sweepObject: string | undefined) => ({
1110+
// [#30026] A start naming no `objectName` is typed against the swept
1111+
// object (`timeRelative.object`, which the schema requires): its record IS
1112+
// that row, so `tr_sweep_only` is refused like `tr_same`. Before, it got no
1113+
// type verdict at all.
1114+
it('refuses a type mismatch on a time-relative sweep of the typed object, and warns on a sweep of another (#29959, #30026)', () => {
1115+
const flowOf = (name: string, startObject: string | undefined, sweepObject: string) => ({
11121116
name,
11131117
nodes: [
11141118
{
11151119
id: 'start', type: 'start',
11161120
config: {
1117-
objectName: 'crm_opportunity', condition: 'title > 5', schedule: '0 * * * *',
1118-
timeRelative: { ...(sweepObject ? { object: sweepObject } : {}), dateField: 'close_date', offsetDays: 3 },
1121+
...(startObject ? { objectName: startObject } : {}), condition: 'title > 5', schedule: '0 * * * *',
1122+
timeRelative: { object: sweepObject, dateField: 'close_date', offsetDays: 3 },
11191123
},
11201124
},
11211125
{ id: 'route', type: 'decision', config: { conditions: [{ label: 'Long', expression: 'record.title > 5' }] } },
@@ -1130,12 +1134,12 @@ describe('validateStackExpressions (ADR-0032 build-time)', () => {
11301134
{ name: 'crm_opportunity', fields: { title: { type: 'text', required: true }, close_date: { type: 'date' } } },
11311135
{ name: 'crm_task', fields: { close_date: { type: 'date' } } },
11321136
];
1133-
for (const [name, sweepObject, severity] of [
1134-
['tr_default', undefined, 'error'],
1135-
['tr_same', 'crm_opportunity', 'error'],
1136-
['tr_other', 'crm_task', 'warning'],
1137+
for (const [name, startObject, sweepObject, severity] of [
1138+
['tr_sweep_only', undefined, 'crm_opportunity', 'error'],
1139+
['tr_same', 'crm_opportunity', 'crm_opportunity', 'error'],
1140+
['tr_other', 'crm_opportunity', 'crm_task', 'warning'],
11371141
] as const) {
1138-
const found = typeMismatches(validateStackExpressions({ objects, flows: [flowOf(name, sweepObject)] }));
1142+
const found = typeMismatches(validateStackExpressions({ objects, flows: [flowOf(name, startObject, sweepObject)] }));
11391143
expect(found.map(i => [i.where, i.severity]), name).toEqual([
11401144
[`flow '${name}' · node 'start' (start) condition`, severity],
11411145
[`flow '${name}' · node 'route' (decision) decision branch expression at config.conditions[0].expression`, severity],
@@ -1149,6 +1153,73 @@ describe('validateStackExpressions (ADR-0032 build-time)', () => {
11491153
}
11501154
});
11511155

1156+
// [#30026] Where the sweep is of another object, the author is told why the
1157+
// verdict is only a warning: once, at the start node's `objectName`, and
1158+
// only on a flow that was served a warning it explains.
1159+
it('says why a time-relative sweep of another object only warns, once, where a warning was served (#30026)', () => {
1160+
const objects = [
1161+
{ name: 'crm_opportunity', fields: { title: { type: 'text', required: true }, close_date: { type: 'date' } } },
1162+
{ name: 'crm_task', fields: { close_date: { type: 'date' } } },
1163+
];
1164+
const flowOf = (name: string, startObject: string | undefined, sweepObject: string, condition: string) => ({
1165+
name,
1166+
nodes: [
1167+
{
1168+
id: 'start', type: 'start',
1169+
config: {
1170+
...(startObject ? { objectName: startObject } : {}), condition, schedule: '0 * * * *',
1171+
timeRelative: { object: sweepObject, dateField: 'close_date', offsetDays: 3 },
1172+
},
1173+
},
1174+
{ id: 'end', type: 'end' },
1175+
],
1176+
edges: [{ id: 'e1', source: 'start', target: 'end', condition }],
1177+
});
1178+
const notes = (issues: ReturnType<typeof validateStackExpressions>) =>
1179+
issues.filter(i => i.where.endsWith('(start) config.objectName'));
1180+
const other = validateStackExpressions({ objects, flows: [flowOf('tr_other', 'crm_opportunity', 'crm_task', 'title > 5')] });
1181+
expect(notes(other).map(i => [i.where, i.severity, i.path, i.source])).toEqual([
1182+
["flow 'tr_other' · node 'start' (start) config.objectName", 'warning', 'flows[0].nodes[0].config.objectName', 'crm_opportunity'],
1183+
]);
1184+
expect(notes(other)[0]!.message).toContain("each run's record is a row of 'crm_task'");
1185+
expect(notes(other)[0]!.message).toContain("Set config.objectName to 'crm_task', or remove it");
1186+
// No warning to explain, or no other object: no note.
1187+
for (const [name, startObject, sweepObject, condition] of [
1188+
['tr_other_clean', 'crm_opportunity', 'crm_task', 'size(title) > 5'],
1189+
['tr_same', 'crm_opportunity', 'crm_opportunity', 'title > 5'],
1190+
['tr_sweep_only', undefined, 'crm_opportunity', 'title > 5'],
1191+
] as const) {
1192+
expect(notes(validateStackExpressions({ objects, flows: [flowOf(name, startObject, sweepObject, condition)] })), name).toEqual([]);
1193+
}
1194+
});
1195+
1196+
// [#30026] Typed against the swept object, a well-typed sweep-only flow
1197+
// stays clean: its fields resolve, so nothing is refused that runs.
1198+
it('leaves a well-typed time-relative flow typed only by its swept object clean (#30026)', () => {
1199+
const issues = validateStackExpressions({
1200+
objects: [{ name: 'crm_opportunity', fields: { title: { type: 'text', required: true }, amount: { type: 'number', required: true }, close_date: { type: 'date' } } }],
1201+
flows: [{
1202+
name: 'tr_sweep_only',
1203+
nodes: [
1204+
{
1205+
id: 'start', type: 'start',
1206+
config: {
1207+
condition: 'size(title) > 5 && record.amount > 10', schedule: '0 * * * *',
1208+
timeRelative: { object: 'crm_opportunity', dateField: 'close_date', offsetDays: 3 },
1209+
},
1210+
},
1211+
{ id: 'route', type: 'decision', config: { conditions: [{ label: 'Big', expression: 'amount > 10' }] } },
1212+
{ id: 'end', type: 'end' },
1213+
],
1214+
edges: [
1215+
{ id: 'e1', source: 'start', target: 'route', condition: "record.title != 'x'" },
1216+
{ id: 'e2', source: 'route', target: 'end', label: 'Big' },
1217+
],
1218+
}],
1219+
});
1220+
expect(issues).toEqual([]);
1221+
});
1222+
11521223
// [#29959] `seedRunVariables` seeds the flow's declared variables before it
11531224
// flattens the record, and the flattening skips a name already bound: a
11541225
// bare `title` here reads the NUMBER variable and `title > 5` runs. Only
@@ -3975,7 +4046,8 @@ const READ_SURFACES: Array<{ receiver: string; expected: string[]; declaredBy: s
39754046
keys: () => shapeKeysOf(ObjectStackSchema.shape.flows),
39764047
},
39774048
{ receiver: 'node', expected: ['config', 'id', 'type'], declaredBy: 'flows[].nodes[]', keys: () => shapeKeysOf(flowShape().nodes) },
3978-
{ receiver: 'startNode', expected: ['config'], declaredBy: 'flows[].nodes[]', keys: () => shapeKeysOf(flowShape().nodes) },
4049+
// [#30026] `id`: the time-relative note names the start node it sits on.
4050+
{ receiver: 'startNode', expected: ['config', 'id'], declaredBy: 'flows[].nodes[]', keys: () => shapeKeysOf(flowShape().nodes) },
39794051
{
39804052
receiver: 'edge',
39814053
expected: ['condition', 'id', 'source', 'target'],

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

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,6 +2198,10 @@ export function validateStackExpressions(stack: AnyRec): ExprIssue[] {
21982198
*/
21992199
export function runStackExpressionPasses(stack: AnyRec, options: StackExpressionOptions): ExprIssue[] {
22002200
const issues: ExprIssue[] = [];
2201+
// [#30026] How many `-run-may-fail` warnings {@link check} has served: a
2202+
// flow whose own count moved is one an author is told why of, where the
2203+
// reason is the flow's own (the time-relative note in the flow walk).
2204+
let runMayFailServed = 0;
22012205
// [#22019] See {@link StackExpressionOptions.runtimeWriteType}: on an object
22022206
// write only the passes over the object's own body judge: the field-formula
22032207
// pass and (#22032) the validation-rule, field-rule-slot, per-option
@@ -2527,7 +2531,10 @@ export function runStackExpressionPasses(stack: AnyRec, options: StackExpression
25272531
if (fieldRuleVerdictIssued && isBareReferenceToAny(e.message, FIELD_RULE_NOWHERE_BOUND_ROOTS)) continue;
25282532
issues.push({ where, message: e.message, source: e.source, severity: 'error' });
25292533
}
2530-
for (const w of res.warnings) issues.push({ where, message: w.message, source: w.source, severity: 'warning' });
2534+
for (const w of res.warnings) {
2535+
if (w.code.endsWith('-run-may-fail')) runMayFailServed++;
2536+
issues.push({ where, message: w.message, source: w.source, severity: 'warning' });
2537+
}
25312538
if (objectName && traversalHydration) refuseStoredMetadataBodyHop(where, raw, objectName, 'rule');
25322539
if (objectName && traversalHydration) refuseUndeclaredHopField(where, raw, objectName, 'rule');
25332540
// [#8116] Provenance rides every object-bound predicate this helper
@@ -2710,10 +2717,22 @@ export function runStackExpressionPasses(stack: AnyRec, options: StackExpression
27102717
// contractually typed `(stack) => Issue[]` — the crash `lintFlowPatterns`
27112718
// was filed for, reached through this rule instead, on the same document.
27122719
const edges = recordsOf(flow.edges);
2713-
// The record-change target object — `record.*` refs resolve against it.
2720+
// The object the flow's conditions are typed against — `record.*` refs and
2721+
// bare names resolve against its fields: the start node's `objectName`
2722+
// (a record-change run's trigger object), else, on a time-relative flow,
2723+
// the object it sweeps. `TimeRelativeTriggerSchema` requires
2724+
// `timeRelative.object`, and `time-relative-trigger.ts` launches each run
2725+
// with a row of that object as `record`, whatever the start node names, so
2726+
// a sweep whose start names no `objectName` is typed against the swept
2727+
// object: its record IS that row.
27142728
const startNode = nodes.find(n => n.type === 'start');
27152729
const startCfg = (startNode?.config ?? {}) as AnyRec;
2716-
const objectName = typeof startCfg.objectName === 'string' ? startCfg.objectName : undefined;
2730+
const startObject = typeof startCfg.objectName === 'string' ? startCfg.objectName : undefined;
2731+
const triggerKind = resolveFlowTriggerKind(flow);
2732+
const sweep = startCfg.timeRelative && typeof startCfg.timeRelative === 'object' ? startCfg.timeRelative as AnyRec : {};
2733+
const sweepObject = triggerKind === 'time_relative' && typeof sweep.object === 'string' ? sweep.object : undefined;
2734+
const objectName = startObject ?? sweepObject;
2735+
const runMayFailBefore = runMayFailServed;
27172736

27182737
// #4347 — every graph in the flow, not just `flow.nodes`/`flow.edges`. An
27192738
// ADR-0031 container keeps a whole sub-graph in its `config`, so the
@@ -2819,18 +2838,20 @@ export function runStackExpressionPasses(stack: AnyRec, options: StackExpression
28192838
* - Outcome `run-fails`: the condition throws, the run fails, and the
28202839
* trigger isolates the write that fired it.
28212840
*/
2822-
const triggerKind = resolveFlowTriggerKind(flow);
28232841
const nullGuardObject = triggerKind === 'record_change' ? objectName : undefined;
28242842
// [#29926] The ledger's flow rows: `run-fails` on a record-triggered flow,
28252843
// for both verdicts, and on a time-relative sweep of the typed object
28262844
// (#29959; the null-guard twin stays on record-triggered flows, the one
28272845
// entrance that binds `previous` too); nothing declared on any other flow.
2828-
// The sweep's object is `timeRelative.object`, else the start node's.
2829-
const sweep = startCfg.timeRelative && typeof startCfg.timeRelative === 'object' ? startCfg.timeRelative as AnyRec : {};
2846+
// The sweep's object is `timeRelative.object` and nothing else: the schema
2847+
// requires the key, and a descriptor without it never binds
2848+
// (`flow-time-relative-descriptor-invalid`), so no run of such a flow
2849+
// binds a row of the typed object and none is declared. The typed object
2850+
// is the swept one wherever the start names no `objectName` (above).
28302851
const flowFaultOutcome = faultOutcomeOf({
28312852
kind: 'flow condition',
28322853
triggerKind,
2833-
sweepsTypedObject: typeof sweep.object !== 'string' || sweep.object === objectName,
2854+
sweepsTypedObject: sweepObject !== undefined && sweepObject === objectName,
28342855
});
28352856
// [#29959] The names the run binds over the flattening — its declared
28362857
// variables and node ids, and the engine's own — which a bare read holds
@@ -3248,6 +3269,28 @@ export function runStackExpressionPasses(stack: AnyRec, options: StackExpression
32483269
}
32493270
}
32503271
}
3272+
3273+
// [#30026] Why a sweep of another object keeps the run-may-fail warning:
3274+
// the conditions are typed against the start node's `objectName`, while
3275+
// each run's `record` is a row of the swept object. Said once per flow, at
3276+
// the key that makes it so, and only where a warning it explains was served.
3277+
if (startObject !== undefined && sweepObject !== undefined && startObject !== sweepObject
3278+
&& runMayFailServed > runMayFailBefore) {
3279+
const startIndex = flowIndex >= 0 && startNode && Array.isArray(flow.nodes) ? flow.nodes.indexOf(startNode) : -1;
3280+
issues.push({
3281+
where: `flow '${flowName}' · node '${String(startNode?.id)}' (start) config.objectName`,
3282+
message:
3283+
`this time-relative flow sweeps '${sweepObject}' (config.timeRelative.object), so each run's record is a ` +
3284+
`row of '${sweepObject}', while its conditions are checked against the fields of '${startObject}' (the ` +
3285+
`start node's config.objectName). That is why a type mismatch in its conditions is a warning here and ` +
3286+
`not a refusal: the names hold a '${sweepObject}' row, not the '${startObject}' fields they were checked ` +
3287+
`against. Set config.objectName to '${sweepObject}', or remove it: the conditions are then checked ` +
3288+
`against the swept object, and a type mismatch in them is refused, as on a record-triggered flow.`,
3289+
source: startObject,
3290+
severity: 'warning',
3291+
...(startIndex >= 0 ? { path: `flows[${flowIndex}].nodes[${startIndex}].config.objectName` } : {}),
3292+
});
3293+
}
32513294
}
32523295

32533296
// ── Object validation-rule + formula predicates ────────────────────

0 commit comments

Comments
 (0)