Skip to content

Commit a99fdfa

Browse files
committed
fix(lint): warn on a flow start input declared a record of the object-less placeholder, or one id the flow reads as a list
Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016WV4cytLmJxfAmgPVjUbbW
1 parent 06cccd3 commit a99fdfa

2 files changed

Lines changed: 305 additions & 0 deletions

File tree

‎packages/lint/src/validate-flow-trigger-readiness.test.ts‎

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
validateFlowTriggerReadiness as validateFlowTriggerReadinessUnrecorded,
77
FLOW_TRIGGER_UNKNOWN_OBJECT,
88
FLOW_INPUT_UNKNOWN_OBJECT,
9+
FLOW_INPUT_OBJECT_LESS,
10+
FLOW_INPUT_RECORD_READ_AS_LIST,
911
FLOW_DRAFT_STATUS_AMBIGUOUS,
1012
FLOW_TRIGGER_UNKNOWN_EVENT,
1113
FLOW_TIME_RELATIVE_DESCRIPTOR_INVALID,
@@ -1618,6 +1620,49 @@ describe('validateFlowTriggerReadiness', () => {
16181620
flows: [recordFlow()],
16191621
},
16201622
],
1623+
// A declared start input that can never work as written: advisory.
1624+
[
1625+
FLOW_INPUT_OBJECT_LESS,
1626+
'warning',
1627+
{
1628+
objects: [candidateObject],
1629+
flows: [
1630+
{
1631+
name: 'object_less_input',
1632+
type: 'screen',
1633+
status: 'active',
1634+
variables: [{ name: 'recordId', type: 'text', isInput: true, objectName: 'global' }],
1635+
nodes: [{ id: 'start', type: 'start' }],
1636+
},
1637+
],
1638+
},
1639+
],
1640+
[
1641+
FLOW_INPUT_RECORD_READ_AS_LIST,
1642+
'warning',
1643+
{
1644+
objects: [candidateObject],
1645+
flows: [
1646+
{
1647+
name: 'one_id_read_as_list',
1648+
type: 'screen',
1649+
status: 'active',
1650+
variables: [{ name: 'candidateIds', type: 'text', isInput: true, objectName: 'app_candidate' }],
1651+
nodes: [
1652+
{ id: 'start', type: 'start' },
1653+
{
1654+
id: 'load',
1655+
type: 'get_record',
1656+
config: {
1657+
objectName: 'app_candidate',
1658+
filter: { id: { $in: { dialect: 'cel', source: 'candidateIds' } } },
1659+
},
1660+
},
1661+
],
1662+
},
1663+
],
1664+
},
1665+
],
16211666
];
16221667

16231668
for (const [rule, severity, stack] of provoke) {
@@ -1995,6 +2040,128 @@ describe('[#30009] api trigger readiness reads the hook\'s one verifier (config.
19952040
});
19962041
});
19972042

2043+
describe('a start input whose record declaration can never work as written', () => {
2044+
const cel = (source: string) => ({ dialect: 'cel', source });
2045+
const getRow = (filter: Record<string, unknown>, label = 'Load candidates') => ({
2046+
id: 'load',
2047+
type: 'get_record',
2048+
label,
2049+
config: { objectName: 'app_candidate', filter },
2050+
});
2051+
const inputFlow = (variables: unknown[], nodes: unknown[] = []) => ({
2052+
name: 'review_candidates',
2053+
type: 'screen',
2054+
status: 'active',
2055+
variables,
2056+
nodes: [{ id: 'start', type: 'start' }, ...nodes],
2057+
});
2058+
const lint = (flow: Record<string, unknown>, objects: unknown[] = [candidateObject]) =>
2059+
validateFlowTriggerReadiness({ objects, flows: [flow] });
2060+
2061+
describe(`${FLOW_INPUT_OBJECT_LESS} — objectName names the object-less placeholder`, () => {
2062+
it("warns on 'global', at the entry the author wrote, beside the unknown-object verdict it already had", () => {
2063+
const findings = lint(inputFlow([
2064+
{ name: 'note', type: 'text', isInput: true },
2065+
{ name: 'recordId', type: 'text', isInput: true, objectName: 'global' },
2066+
]));
2067+
expect(findings.map((f) => [f.rule, f.severity, f.where, f.path])).toEqual([
2068+
[FLOW_INPUT_UNKNOWN_OBJECT, 'warning', 'flow "review_candidates" › input "recordId"', 'flows[0].variables[1].objectName'],
2069+
[FLOW_INPUT_OBJECT_LESS, 'warning', 'flow "review_candidates" › input "recordId"', 'flows[0].variables[1].objectName'],
2070+
]);
2071+
const objectLess = findings[1];
2072+
expect(objectLess.message).toBe(
2073+
"declares input 'recordId' a record of 'global', the object-less placeholder, so no start loads it " +
2074+
'from any object and its value reaches the run unjudged',
2075+
);
2076+
expect(objectLess.hint).not.toContain('\n');
2077+
});
2078+
2079+
it("warns even where the stack defines an object named 'global': the load skips the key, not the object", () => {
2080+
const findings = lint(
2081+
inputFlow([{ name: 'recordId', type: 'text', isInput: true, objectName: 'global' }]),
2082+
[candidateObject, { name: 'global', label: 'Global', fields: {} }],
2083+
);
2084+
expect(findings.map((f) => [f.rule, f.path])).toEqual([[FLOW_INPUT_OBJECT_LESS, 'flows[0].variables[0].objectName']]);
2085+
});
2086+
2087+
it("warns on the other object-less key, '*', which only a pre-parse stack can carry", () => {
2088+
const findings = lint(inputFlow([{ name: 'recordId', type: 'text', isInput: true, objectName: '*' }]));
2089+
expect(findings.filter((f) => f.rule === FLOW_INPUT_OBJECT_LESS).map((f) => f.path)).toEqual([
2090+
'flows[0].variables[0].objectName',
2091+
]);
2092+
});
2093+
2094+
it('is silent on a declared object the stack defines', () => {
2095+
expect(lint(inputFlow([{ name: 'recordId', type: 'text', isInput: true, objectName: 'app_candidate' }]))).toEqual([]);
2096+
});
2097+
});
2098+
2099+
describe(`${FLOW_INPUT_RECORD_READ_AS_LIST} — one declared id the flow reads as a list`, () => {
2100+
const declared = { name: 'candidateIds', type: 'text', isInput: true, objectName: 'app_candidate' };
2101+
2102+
it('warns at the $in read, naming the input, the node and both objects', () => {
2103+
const findings = lint(inputFlow([declared], [getRow({ id: { $in: cel('candidateIds') } })]));
2104+
expect(findings.map((f) => [f.rule, f.severity, f.where, f.path])).toEqual([
2105+
[
2106+
FLOW_INPUT_RECORD_READ_AS_LIST,
2107+
'warning',
2108+
'flow "review_candidates" › input "candidateIds"',
2109+
'flows[0].nodes[1].config.filter.id.$in',
2110+
],
2111+
]);
2112+
expect(findings[0].message).toBe(
2113+
"declares input 'candidateIds' one record id of 'app_candidate', and get_record \"Load candidates\" " +
2114+
"reads it as a list of 'app_candidate' ids (filter.id.$in), so no value is both and every start " +
2115+
'that supplies the input is refused',
2116+
);
2117+
expect(findings[0].hint).not.toContain('\n');
2118+
});
2119+
2120+
it('reads a `vars.` hand-off and a node nested in a region, located at the nested node', () => {
2121+
const findings = lint(inputFlow([declared], [
2122+
{
2123+
id: 'guard',
2124+
type: 'try_catch',
2125+
config: {
2126+
try: { nodes: [{ id: 'drop', type: 'delete_record', config: { objectName: 'app_candidate', filter: { id: { $in: cel('vars.candidateIds') } } } }] },
2127+
catch: { nodes: [] },
2128+
},
2129+
},
2130+
]));
2131+
expect(findings.map((f) => [f.rule, f.path])).toEqual([
2132+
[FLOW_INPUT_RECORD_READ_AS_LIST, 'flows[0].nodes[1].config.try.nodes[0].config.filter.id.$in'],
2133+
]);
2134+
expect(findings[0].message).toContain('delete_record "drop"');
2135+
});
2136+
2137+
it('is silent where admission reads one id, or does not read the declared input at all', () => {
2138+
const silent: Array<[string, Record<string, unknown>]> = [
2139+
['filter.id is the input', inputFlow([declared], [getRow({ id: cel('candidateIds') })])],
2140+
['filter.id.$eq is the input', inputFlow([declared], [getRow({ id: { $eq: cel('candidateIds') } })])],
2141+
['the $in reads an input with no declaration', inputFlow(
2142+
[{ name: 'candidateIds', type: 'text', isInput: true }],
2143+
[getRow({ id: { $in: cel('candidateIds') } })],
2144+
)],
2145+
['the declared variable is not an input', inputFlow(
2146+
[{ ...declared, isInput: false }],
2147+
[getRow({ id: { $in: cel('candidateIds') } })],
2148+
)],
2149+
['the $in source is computed from the input', inputFlow([declared], [getRow({ id: { $in: cel('[candidateIds]') } })])],
2150+
['a loop rebinds the name before the read', inputFlow([declared], [
2151+
{ id: 'each', type: 'loop', config: { collection: cel('rows'), iteratorVariable: 'candidateIds', body: { nodes: [] } } },
2152+
getRow({ id: { $in: cel('candidateIds') } }),
2153+
])],
2154+
['the node is not a row-addressing CRUD node', inputFlow([declared], [
2155+
{ id: 'mk', type: 'create_record', config: { objectName: 'app_candidate', filter: { id: { $in: cel('candidateIds') } } } },
2156+
])],
2157+
];
2158+
for (const [label, flow] of silent) {
2159+
expect(lint(flow).filter((f) => f.rule === FLOW_INPUT_RECORD_READ_AS_LIST), label).toEqual([]);
2160+
}
2161+
});
2162+
});
2163+
});
2164+
19982165
describe('[#22161] one-line verdicts — the never-fire family', () => {
19992166
it('every verdict the cases above fired is one line of at most 200 characters', () => {
20002167
// The coverage control first: each of the five ids fired at least once, so

‎packages/lint/src/validate-flow-trigger-readiness.ts‎

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@
174174
// start input declared a record reference (`variables[].objectName`) names
175175
// an object this stack does not define. It is not a never-fire verdict —
176176
// a start that omits the input still runs — so it sits with the hedged id.
177+
// - `warning` — `flow-input-object-less` and `flow-input-record-read-as-list`
178+
// (1j), two declarations of a start input that can never work as written:
179+
// one names the object-less placeholder and loads nothing, the other is
180+
// one id the flow also reads as a list, so every start supplying it is
181+
// refused. Neither is a never-fire verdict, and both keep the
182+
// declaration's existing verdicts as they were.
177183
// - `warning` — `flow-draft-status-ambiguous`. Draft flows DO fire, so this is
178184
// an ambiguity of intent, not a dead flow. Gating it would refuse a stack
179185
// whose flows all work.
@@ -207,6 +213,7 @@ import {
207213
apiTriggerVerifierOf,
208214
resolveFlowTriggerKind,
209215
} from '@objectstack/spec/automation';
216+
import { walkFlowNodes } from './flow-walk.js';
210217
import { closureDependencyRemedy, recordsOf, suggestName } from './object-graph.js';
211218

212219
export type FlowTriggerReadinessSeverity = 'error' | 'warning';
@@ -234,6 +241,24 @@ export const FLOW_TRIGGER_UNKNOWN_OBJECT = 'flow-trigger-unknown-object';
234241
* package closure, an error under one.
235242
*/
236243
export const FLOW_INPUT_UNKNOWN_OBJECT = 'flow-input-unknown-object';
244+
/**
245+
* A start input declared a record reference names the object-less
246+
* placeholder (`objectName: 'global'`). The pattern accepts it, but the start
247+
* doors' load treats an object-less key as nothing to load
248+
* (`loadActionSubjectRecord`, `packages/runtime/src/action-execution.ts`,
249+
* through `isObjectLessActionKey`), so the value reaches the run unjudged: a
250+
* declaration that loads nothing. Advisory: it refuses no start.
251+
*/
252+
export const FLOW_INPUT_OBJECT_LESS = 'flow-input-object-less';
253+
/**
254+
* A start input declared ONE record id (`variables[].objectName`) that a
255+
* row-addressing node of the same flow reads as a LIST of ids
256+
* (`filter.id.$in`). Start admission judges the input once per reading
257+
* (`refuseUnreadableFlowStartRecords`): one id for the declaration, a list for
258+
* the `$in` read, and no value is both, so every start that supplies the input
259+
* is refused `400`. Advisory: the failure is loud, only late.
260+
*/
261+
export const FLOW_INPUT_RECORD_READ_AS_LIST = 'flow-input-record-read-as-list';
237262
export const FLOW_DRAFT_STATUS_AMBIGUOUS = 'flow-draft-status-ambiguous';
238263
export const FLOW_TRIGGER_UNKNOWN_EVENT = 'flow-trigger-unknown-event';
239264
/**
@@ -497,6 +522,42 @@ function descriptorRefusal(issues: ReadonlyArray<{ code: string; path: ReadonlyA
497522
}
498523

499524
/** The start node of a flow definition, if any. */
525+
/**
526+
* The object-less action keys, as `isObjectLessActionKey`
527+
* (`packages/objectql/src/action-governance.ts`) answers them for a non-empty
528+
* name: `'global'` (`GLOBAL_ACTION_OBJECT_KEY`) and `'*'`. Spelled here rather
529+
* than imported because lint depends on `@objectstack/spec` and never on a
530+
* runtime package; kept in lockstep with that predicate by hand, as
531+
* `stack.zod.ts`'s `GLOBAL_ACTION_SCOPE` is.
532+
*/
533+
const OBJECT_LESS_KEYS: ReadonlySet<string> = new Set(['global', '*']);
534+
535+
/** The three CRUD node types start admission reads a row id from (`filter.id`). */
536+
const ROW_ADDRESSING_CRUD_NODE_TYPES: ReadonlySet<string> = new Set(['get_record', 'update_record', 'delete_record']);
537+
538+
/**
539+
* The variable a value slot hands on unchanged, or `undefined` — start
540+
* admission's own reading (`handedOnVariable`,
541+
* `packages/runtime/src/flow-start-admission.ts`): a CEL envelope whose whole
542+
* source is one variable name, bare or through `vars`. Anything computed is
543+
* not a hand-off, and is not judged here either.
544+
*/
545+
function handedOnVariable(slot: unknown): string | undefined {
546+
if (slot === null || typeof slot !== 'object' || Array.isArray(slot)) return undefined;
547+
const { dialect, source } = slot as { dialect?: unknown; source?: unknown };
548+
if (dialect !== 'cel' || typeof source !== 'string') return undefined;
549+
const match = /^(?:vars\.)?([A-Za-z_][A-Za-z0-9_]*)$/.exec(source.trim());
550+
return match ? match[1] : undefined;
551+
}
552+
553+
function plainRecord(value: unknown): AnyRec | undefined {
554+
return value !== null && typeof value === 'object' && !Array.isArray(value) ? (value as AnyRec) : undefined;
555+
}
556+
557+
function nonEmptyString(value: unknown): string | undefined {
558+
return typeof value === 'string' && value !== '' ? value : undefined;
559+
}
560+
500561
function startNodeOf(flow: AnyRec): { node: AnyRec; index: number } | undefined {
501562
const nodes = Array.isArray(flow.nodes) ? (flow.nodes as AnyRec[]) : [];
502563
const index = nodes.findIndex((n) => n?.type === 'start');
@@ -1068,6 +1129,83 @@ export function validateFlowTriggerReadiness(
10681129
);
10691130
});
10701131

1132+
// 1j. Two declarations of a start input the schema accepts and the start
1133+
// doors can never honour as written. Both are additive to 1i: its
1134+
// verdict on the same name is left exactly as it was.
1135+
//
1136+
// - The object-less placeholder: the doors' load skips an object-less
1137+
// key, so the declared input is loaded from nothing and its value runs
1138+
// unjudged.
1139+
// - One id the flow also reads as a list: start admission judges the
1140+
// input once per reading — one id for the declaration, a list for a
1141+
// CRUD node's `filter.id.$in` — and no value is both, so every start
1142+
// supplying it is refused `400`. Read the way admission reads it
1143+
// (`flowDeclaredRecordReads`): only an `isInput` variable, only a
1144+
// CRUD node with a literal `objectName`, only a `$in` that hands the
1145+
// input on as it is, and not where a `loop` or `map` rebinds the name.
1146+
// A list read through a `subflow` or `map` hand-off is not judged here.
1147+
const declaredInputs = new Map<string, string>();
1148+
(Array.isArray(flow.variables) ? (flow.variables as unknown[]) : []).forEach((entry, variableIndex) => {
1149+
const variable = plainRecord(entry);
1150+
const objectName = nonEmptyString(variable?.objectName);
1151+
const variableName = nonEmptyString(variable?.name);
1152+
if (!variable || !objectName) return;
1153+
if (OBJECT_LESS_KEYS.has(objectName)) {
1154+
const shown = variableName ?? `#${variableIndex}`;
1155+
findings.push({
1156+
severity: 'warning',
1157+
rule: FLOW_INPUT_OBJECT_LESS,
1158+
where: `flow "${flowName}" › input "${shown}"`,
1159+
path: `flows[${flowIndex}].variables[${variableIndex}].objectName`,
1160+
message:
1161+
`declares input '${shown}' a record of '${objectName}', the object-less placeholder, so no start ` +
1162+
`loads it from any object and its value reaches the run unjudged`,
1163+
hint:
1164+
`Set the input's objectName to the object whose record id the input carries, or remove ` +
1165+
`objectName if the input is not a record id`,
1166+
});
1167+
}
1168+
if (variable.isInput === true && variableName && !declaredInputs.has(variableName)) {
1169+
declaredInputs.set(variableName, objectName);
1170+
}
1171+
});
1172+
if (declaredInputs.size > 0) {
1173+
const walked = walkFlowNodes(flow, `flows[${flowIndex}]`);
1174+
// A `loop` or `map` binds its item (and index) variable over the run's
1175+
// own variables, so a read of that name is not a read of the start input.
1176+
for (const { node } of walked) {
1177+
const config = plainRecord(node.config);
1178+
if (!config || (node.type !== 'loop' && node.type !== 'map')) continue;
1179+
const iterator = nonEmptyString(config.iteratorVariable) ?? (node.type === 'map' ? 'item' : undefined);
1180+
if (iterator !== undefined) declaredInputs.delete(iterator);
1181+
const index = nonEmptyString(config.indexVariable);
1182+
if (index !== undefined) declaredInputs.delete(index);
1183+
}
1184+
for (const { node, path } of walked) {
1185+
if (typeof node.type !== 'string' || !ROW_ADDRESSING_CRUD_NODE_TYPES.has(node.type)) continue;
1186+
const config = plainRecord(node.config);
1187+
const readObject = nonEmptyString(config?.objectName);
1188+
const idOperators = plainRecord(plainRecord(config?.filter)?.id);
1189+
const listed = handedOnVariable(idOperators?.$in);
1190+
if (!readObject || listed === undefined || !declaredInputs.has(listed)) continue;
1191+
const declaredObject = declaredInputs.get(listed);
1192+
const nodeLabel = nonEmptyString(node.label) ?? nonEmptyString(node.id) ?? path;
1193+
findings.push({
1194+
severity: 'warning',
1195+
rule: FLOW_INPUT_RECORD_READ_AS_LIST,
1196+
where: `flow "${flowName}" › input "${listed}"`,
1197+
path: `${path}.config.filter.id.$in`,
1198+
message:
1199+
`declares input '${listed}' one record id of '${declaredObject}', and ${node.type} "${nodeLabel}" ` +
1200+
`reads it as a list of '${readObject}' ids (filter.id.$in), so no value is both and every start ` +
1201+
`that supplies the input is refused`,
1202+
hint:
1203+
`Remove the input's objectName if it carries a list of ids, or read it as one id with filter.id ` +
1204+
`or filter.id.$eq`,
1205+
});
1206+
}
1207+
}
1208+
10711209
// 2. Auto-triggered flow whose status is 'draft' — authored or defaulted
10721210
// (defineFlow parses at definition time, so the two are the same here).
10731211
if (isAutoTriggered && (flow.status == null || flow.status === 'draft')) {

0 commit comments

Comments
 (0)