diff --git a/.changeset/33176-validate-expression-flow-variables.md b/.changeset/33176-validate-expression-flow-variables.md new file mode 100644 index 00000000000..efe2d9932de --- /dev/null +++ b/.changeset/33176-validate-expression-flow-variables.md @@ -0,0 +1,15 @@ +--- +'@objectstack/mcp': patch +'@objectstack/lint': patch +--- + +fix(mcp): `validate_expression` takes the flow's declared `variables` at a `flow_condition`, so a bare name binds as the flow binds it and the shadowed-read warning comes back + +Clause-②: yes (widening) — the MCP `validate_expression` tool accepts a new optional input, `variables`, at `site: 'flow_condition'`, a call its closed input refused before; without it every answer is unchanged. +Metadata-protocol: none + +A flow run binds its declared variables before it flattens the trigger record's fields, so a bare name a variable shares with a field reads the variable, and the runtime flow publish door warns about it (a bare read of a name that is both a declared flow variable and a field). `validate_expression` was never handed the flow's variables: it typed such a bare name as the field, and it could not give that warning. + +- `variables` (`site: 'flow_condition'` only): the flow's declared `variables`, in the flow's own shape (`FlowVariableSchema`: a `name` and a `type` at least), so a flow's `variables` array can be handed over as it stands; a malformed one is refused with the spec's own sentence. Given them, a bare read of a declared variable is not typed as the field (the shared validator's `flowVariables`, as the gate hands it on the same flow), and a variable named like a field of `objectName` read bare gets the gate's own shadowed-read warning. `record.FIELD` still reads the field and is judged as before. On another site the input is refused, like `startCondition`. +- Without `variables`, the answer is byte for byte what it was, and the tool description says the shadowed-read warning needs them. +- `@objectstack/lint`: `judgeAuthoredSiteExpression` takes an optional `variables` (a new optional key on the exported `AuthoredSiteExpression` type), which its one-flow probe declares. The gate's verdicts are unchanged. diff --git a/packages/lint/src/authored-site-expression.test.ts b/packages/lint/src/authored-site-expression.test.ts index 05de1a56095..9a9d09457af 100644 --- a/packages/lint/src/authored-site-expression.test.ts +++ b/packages/lint/src/authored-site-expression.test.ts @@ -452,6 +452,8 @@ type FlowFixture = { expression: string; severity: 'error' | 'warning'; names: string; + /** [#33176] The flow's declared variables, handed to the entry as the caller hands them and declared on the hand-written flow. */ + variables?: Array>; }; type FlowClassified = @@ -502,11 +504,17 @@ const FLOW_CALLS: Record = { fixture: { objects: [FLOW_OBJ], expression: 'foo.bar == 1' }, buildDoor: {}, }, + // [#33176] The flow's declared variables, which the caller hands the entry + // and the probe declares; without them it cannot fire (pinned below). warnShadowedFieldReads: { - kind: 'pass-silent-here', - why: "needs the flow's declared variables, which the tool is not handed; the probe declares none", - fixture: { objects: [FLOW_OBJ], expression: "title == 'x'" }, - buildDoor: { variables: [{ name: 'title', type: 'text' }] }, + kind: 'pass', + fixture: { + objects: [FLOW_OBJ], + expression: "title == 'x'", + severity: 'warning', + names: '`title`', + variables: [{ name: 'title', type: 'text' }], + }, }, refuseStoredMetadataBodyHop: { kind: 'pass-silent-here', @@ -597,7 +605,7 @@ function flowGateOn( function flowEntryOn( objects: Array>, expression: string, - opts: { triggerKind?: AuthoredFlowTrigger; startCondition?: string } = {}, + opts: { triggerKind?: AuthoredFlowTrigger; startCondition?: string } & HandFlowOverrides = {}, ) { return judgeAuthoredSiteExpression({ site: 'flow_condition', objectName: 'obj', expression, objects, ...opts }) .map((i) => [i.severity ?? 'error', i.message]); @@ -617,12 +625,13 @@ describe('judgeAuthoredSiteExpression at a flow condition — every gate pass is for (const [name, classified] of Object.entries(FLOW_CALLS)) { if (classified.kind === 'pass') { - const { objects, expression, severity, names } = classified.fixture; + const { objects, expression, severity, names, variables } = classified.fixture; + const declared = variables ? { variables } : {}; for (const slot of ['start', 'decision', 'edge'] as const) { it(`${name} refuses through the entry as the gate does at a flow's ${slot} condition`, () => { - const gate = flowGateOn(objects, expression, { slot }); + const gate = flowGateOn(objects, expression, { slot, ...declared }); expect(gate.length, 'the fixture trips the gate').toBeGreaterThan(0); - const entry = flowEntryOn(objects, expression); + const entry = flowEntryOn(objects, expression, declared); expect(entry).toEqual(gate); expect(entry.map(([level]) => level)).toEqual(gate.map(() => severity)); expect(entry.some(([, message]) => message!.includes(names))).toBe(true); @@ -715,6 +724,58 @@ describe('judgeAuthoredSiteExpression at a flow condition — every gate pass is }); }); +// ── [#33176] The flow's declared variables, at a flow condition ─────────── +// +// Handed the flow's `variables`, the probe declares them, and the gate's +// shadowed-read warning (`warnShadowedFieldReads`, a `pass` in the table +// above with those variables) comes back through the entry. Handed none, the +// probe is the flow it was before the key, and the warning cannot fire. + +describe("judgeAuthoredSiteExpression with the flow's declared variables (#33176)", () => { + const SHADOWING = [{ name: 'title', type: 'text' }]; + + it('declares the variables it is handed, as handed, and no `variables` key without them', () => { + const variables = [{ name: 'title', type: 'text', isInput: true }, { name: 'limit', type: 'number' }]; + expect(authoredFlowProbe({ objectName: 'obj', expression: 'true', variables }).variables).toEqual(variables); + expect(Object.keys(authoredFlowProbe({ objectName: 'obj', expression: 'true' }))).not.toContain('variables'); + }); + + it('CONTROL — without the variables the shadowed read answers nothing, at the gate and through the entry', () => { + // A flow that declares no variable gets no warning at the runtime door… + expect(flowGateOn([FLOW_OBJ], "title == 'x'")).toEqual([]); + // …and the probe, handed none, is that flow. + expect(flowEntryOn([FLOW_OBJ], "title == 'x'")).toEqual([]); + }); + + it('a variable named like no field of the object gets no warning', () => { + const variables = [{ name: 'threshold', type: 'number' }]; + for (const expression of ['threshold > 3', "record.title == 'x' && threshold > 3"]) { + expect(flowGateOn([FLOW_OBJ], expression, { variables }), expression).toEqual([]); + expect(flowEntryOn([FLOW_OBJ], expression, { variables }), expression).toEqual([]); + } + }); + + it('warns on the bare read alone: `record.FIELD` reads the field, whatever the variables', () => { + expect(flowGateOn([FLOW_OBJ], "record.title == 'x'", { variables: SHADOWING })).toEqual([]); + expect(flowEntryOn([FLOW_OBJ], "record.title == 'x'", { variables: SHADOWING })).toEqual([]); + const both = flowGateOn([FLOW_OBJ], 'record.title == title', { variables: SHADOWING }); + expect(both.map(([level]) => level)).toEqual(['warning']); + expect(flowEntryOn([FLOW_OBJ], 'record.title == title', { variables: SHADOWING })).toEqual(both); + }); + + it('answers as the gate on every trigger kind and after a start condition', () => { + for (const kind of ['record_change', 'time_relative', 'schedule', 'api', 'none'] as const) { + const gate = flowGateOn([FLOW_OBJ], "title == 'x'", { kind, variables: SHADOWING }); + expect(gate.map(([level]) => level), kind).toEqual(['warning']); + expect(flowEntryOn([FLOW_OBJ], "title == 'x'", { triggerKind: kind, variables: SHADOWING }), kind).toEqual(gate); + } + const startCondition = 'record.days != null'; + const gate = flowGateOn([FLOW_OBJ], "title == 'x'", { startCondition, variables: SHADOWING }); + expect(gate.map(([level]) => level)).toEqual(['warning']); + expect(flowEntryOn([FLOW_OBJ], "title == 'x'", { startCondition, variables: SHADOWING })).toEqual(gate); + }); +}); + // ── [#33147] The field being authored, at the formula site ─────────────── // // Named as `field`, a field the definition declares keeps its definition with diff --git a/packages/lint/src/authored-site-expression.ts b/packages/lint/src/authored-site-expression.ts index c4a664943c3..14507c142fc 100644 --- a/packages/lint/src/authored-site-expression.ts +++ b/packages/lint/src/authored-site-expression.ts @@ -33,12 +33,15 @@ * door gives the null guard on a `record.` / `previous.` read and the * unprovisioned-anchor warning. It never judges a bare name's null guard or a * root the run does not bind: those are `os validate`'s whole-flow judgments, - * over the flow's variables, node ids and entrances. The door does warn on a - * bare read of a name a flow variable shares with a field - * (`warnShadowedFieldReads`), but that needs the flow's declared variables, - * and the caller does not hand this entry any, so the probe declares none and - * that warning never fires here. So `[]` at a flow condition is that door's - * verdict, short of the shadowed-read warning, and not `os validate`'s. + * over the flow's variables, node ids and entrances. So `[]` at a flow + * condition is that door's verdict, and not `os validate`'s. + * + * [#33176] The door also warns on a bare read of a name a declared flow + * variable shares with a field of the object (`warnShadowedFieldReads`): the + * variable wins at run time. That reads the flow's declared `variables`, so + * the probe declares the ones the caller hands it, as the flow declares them, + * and the gate's own warning comes back. Handed none, the probe declares none + * and that warning cannot fire, exactly as before this key. */ import { analyzeRelationshipTraversals } from '@objectstack/formula'; @@ -96,6 +99,15 @@ export interface AuthoredSiteExpression { * start condition itself, or follows none (the gate answers both alike). */ startCondition?: string; + /** + * [#33176] site `flow_condition` only: the flow's declared `variables`, in + * the shape `FlowSchema.variables` declares them (`FlowVariableSchema`: a + * `name` and a `type` at least). The probe declares them, so the gate binds + * a bare read of one to the variable and gives its shadowed-read warning + * where one shares a name with a field of `objectName`. Absent: the probe + * declares none, as before this key. + */ + variables?: readonly AnyRec[]; /** * The object definitions as the registry serves them: the subject's, and * those of the objects its references name that the expression reads @@ -166,7 +178,8 @@ function probeTrigger( * [#33147] The flow the gate judges for one flow condition: a start node * bound to `objectName` with the trigger the caller names (and its start * condition, when given), and one edge to an end node carrying the - * condition. Exported for the read-back pin; the entry is its one caller. + * condition; [#33176] it declares the flow's `variables` when the caller + * hands them. Exported for the read-back pin; the entry is its one caller. */ export function authoredFlowProbe({ objectName, @@ -174,12 +187,17 @@ export function authoredFlowProbe({ triggerKind = 'record_change', sweepObject, startCondition, -}: Pick): AnyRec { + variables, +}: Pick< + AuthoredSiteExpression, + 'objectName' | 'expression' | 'triggerKind' | 'sweepObject' | 'startCondition' | 'variables' +>): AnyRec { const trigger = probeTrigger(triggerKind, objectName, sweepObject); return { name: AUTHORED_FLOW_NAME, label: AUTHORED_FLOW_NAME, type: trigger.type, + ...(variables !== undefined ? { variables: [...variables] } : {}), nodes: [ { id: 'start', @@ -230,6 +248,7 @@ export function judgeAuthoredSiteExpression({ triggerKind, sweepObject, startCondition, + variables, objects, }: AuthoredSiteExpression): ExprIssue[] { const authored = objects.map((o) => stripInjectedSystemColumns(o)); @@ -249,7 +268,7 @@ export function judgeAuthoredSiteExpression({ // rules are not what is judged here (left out, as at a validation site). stack = { objects: [body, ...others], - flows: [authoredFlowProbe({ objectName, expression, triggerKind, sweepObject, startCondition })], + flows: [authoredFlowProbe({ objectName, expression, triggerKind, sweepObject, startCondition, variables })], }; runtimeWriteType = 'flow'; // Every finding on the probe's one edge carries that edge's key path. diff --git a/packages/mcp/src/mcp-http-tools.ts b/packages/mcp/src/mcp-http-tools.ts index efe2823be85..218e9312312 100644 --- a/packages/mcp/src/mcp-http-tools.ts +++ b/packages/mcp/src/mcp-http-tools.ts @@ -113,6 +113,11 @@ import { strictUnknownKeyError } from '@objectstack/spec/shared'; // sentence, never a second blank rule written here. import { EvaluatedExpressionInputSchema, TemplateExpressionInputSchema } from '@objectstack/spec/shared'; import { FLOW_TRIGGER_KINDS, type FlowTriggerKind } from '@objectstack/spec/automation'; +// [#33176] A flow's declared variable, as the flow declares it: the shape +// `validate_expression`'s `variables` takes at a flow condition, so a flow's +// own `variables` array is handed over as it stands and judged by the spec's +// own refusals, never a second hand-written shape. +import { FlowVariableSchema } from '@objectstack/spec/automation'; import { METADATA_UNAVAILABLE_CODE, metadataPartialListingSentence, @@ -1846,6 +1851,8 @@ interface GatePassContext { triggerKind?: FlowTriggerOrNone; sweepObject?: string; startCondition?: string; + /** [#33176] The flow's declared variables, as the flow declares them (flow condition). */ + variables?: readonly Record[]; } /** @@ -1891,7 +1898,7 @@ async function gatePassFindings( const def = await bridge.servedObjectDefinitionOf(target); if (def) targets.push(def); } - const { severity, field, triggerKind, sweepObject, startCondition } = context; + const { severity, field, triggerKind, sweepObject, startCondition, variables } = context; const findings = judgeAuthoredSiteExpression({ site, objectName, @@ -1901,6 +1908,8 @@ async function gatePassFindings( ...(triggerKind ? { triggerKind } : {}), ...(sweepObject !== undefined ? { sweepObject } : {}), ...(startCondition !== undefined ? { startCondition } : {}), + // [#33176] The probe declares them, so the gate's shadowed-read warning can fire. + ...(variables !== undefined ? { variables } : {}), objects: [subject, ...targets], }); const errors: GatePassFinding[] = []; @@ -2490,7 +2499,8 @@ export function registerObjectTools( '(text/boolean fields misused in arithmetic, an error at a `validation` site, worded for the rule\'s ' + '`severity`, and at a `flow_condition` of a record-triggered or time-relative flow, where it fails the run; ' + 'a warning on any other flow; date-equality pitfalls; at a `formula`, `validation` or `flow_condition` site, a read ' + - 'of an external object\'s unprovisioned system column; at a `formula` site naming a stored (non-formula) `field`, ' + + 'of an external object\'s unprovisioned system column; at a `flow_condition` given the flow\'s `variables`, a ' + + 'bare read of a variable named like a field; at a `formula` site naming a stored (non-formula) `field`, ' + 'a read of another record, the clock, the user or a permission), plus the fields and stdlib ' + 'functions in scope so you can self-correct. A hop target follows the same system-object fence as ' + '`describe_object`. `site` says where the expression will live: a `formula` ' + @@ -2501,7 +2511,11 @@ export function registerObjectTools( 'another object is refused at publish (`flow-time-relative-object-mismatch`). At a `flow_condition` the verdict ' + 'is the runtime publish gate\'s, which a flow saved through MCP or Studio meets: `os validate` also judges a ' + 'bare name\'s null guard and unbound roots over the whole flow, which this tool is not handed, so write ' + - '`record.` to be judged here. Pass `field` (formula) to be judged as that field: its type, and the ' + + '`record.` to be judged here. Pass the flow\'s declared `variables` (flow_condition), as the flow ' + + 'declares them: a bare read of one is the variable, which the run binds before the record\'s fields, so it ' + + 'is not typed as the field, and a variable named like a field of objectName gets the gate\'s shadowed-read ' + + 'warning. Without them a bare name is typed as the field and that warning cannot be given. ' + + 'Pass `field` (formula) to be judged as that field: its type, and the ' + 'formula cycle it would close; a field not yet declared is judged as a new formula, so a new stored field gets ' + 'its stored-expression warnings only once it is declared.', inputSchema: strictToolInput( @@ -2560,6 +2574,16 @@ export function registerObjectTools( 'branch condition after it. What it proves non-null holds downstream, as at publish. Omit it when ' + 'the expression is the start condition itself.', ), + // [#33176] The third fact the gate reads off the flow. + variables: z + .array(FlowVariableSchema) + .optional() + .describe( + 'site flow_condition only: the flow\'s declared `variables`, as the flow declares them (each a `name` ' + + 'and a `type` at least). A bare read of one is the variable, not the field: it is not typed as the ' + + 'field, and one named like a field of objectName gets the gate\'s shadowed-read warning. Omitted, ' + + 'a bare name is typed as the field and that warning cannot be given.', + ), field: z .string() .regex(/^[a-z_][a-z0-9_]*$/, 'a field name is snake_case, e.g. "due_in_days"') @@ -2573,7 +2597,7 @@ export function registerObjectTools( ), annotations: { readOnlyHint: true, destructiveHint: false, openWorldHint: false }, }, - async ({ objectName, expression, site, severity, triggerKind, sweepObject, startCondition, field }) => { + async ({ objectName, expression, site, severity, triggerKind, sweepObject, startCondition, field, variables }) => { const bad = guard(objectName); if (bad) return errorResult(bad); // [#29959] Each input describes one site; on another it would be @@ -2595,6 +2619,10 @@ export function registerObjectTools( if (startCondition !== undefined && site !== 'flow_condition') { return errorResult(`\`startCondition\` describes a flow; it applies only with site "flow_condition", not "${site ?? 'formula'}".`); } + // [#33176] …and so do its declared variables. + if (variables !== undefined && site !== 'flow_condition') { + return errorResult(`\`variables\` describes a flow; it applies only with site "flow_condition", not "${site ?? 'formula'}".`); + } if (field !== undefined && (site ?? 'formula') !== 'formula') { return errorResult(`\`field\` names the field a formula is authored on; it applies only with site "formula", not "${site}".`); } @@ -2634,6 +2662,12 @@ export function registerObjectTools( // [#30250] A time-relative sweep's run is fired by no write: the // gate's run-fails sentence there names none, and so does this one. ...(site === 'flow_condition' && triggerKind === 'time_relative' ? { timeRelativeSweep: true } : {}), + // [#33176] The flow's declared variables, which its run binds + // before it flattens the record's fields: a bare read of one holds + // the variable, so the shared validator does not type it as the + // field (`ExprSchemaHint.flowVariables`, the key the gate hands it + // on the same flow). Absent, the hint is byte for byte as before. + ...(variables !== undefined ? { flowVariables: variables.map((v) => v.name) } : {}), } as const; // The gate's envelope verdict first: `validateExpression` treats a // blank source as "not authored" and answers ok, while the save gate @@ -2661,6 +2695,7 @@ export function registerObjectTools( triggerKind, sweepObject, startCondition, + variables, }) : NO_GATE_PASS_FINDINGS; const inScope = introspectScope(role, hint); diff --git a/packages/mcp/src/mcp-stdio-tools.test.ts b/packages/mcp/src/mcp-stdio-tools.test.ts index 4d6726c9f1f..e5ef1eb9b7e 100644 --- a/packages/mcp/src/mcp-stdio-tools.test.ts +++ b/packages/mcp/src/mcp-stdio-tools.test.ts @@ -837,3 +837,81 @@ describe('#8034 plugin composition: os serve stdio wiring', () => { } }); }); + +// [#33176] `validate_expression` takes the flow's declared `variables` at a +// flow condition. One tool definition serves both transports +// (`wireBridgeTools`); this pins that the input is advertised and answered +// alike on the stdio wire and over HTTP, with the warning it exists for. +describe('#33176 validate_expression: the flow variables input answers alike on stdio and over HTTP', () => { + const TASK = { name: 'task', fields: { title: { type: 'text', label: 'Title', required: true } } }; + function flowBridge(): McpDataBridge { + return { + ...makeBridge(), + async describeObject(name: string) { + return name === 'task' ? { name: 'task', fields: [{ name: 'title', type: 'text', label: 'Title', required: true }] } : null; + }, + async servedObjectDefinitionOf(name: string) { + return name === 'task' ? TASK : null; + }, + }; + } + + async function overStdio(method: string, params?: unknown): Promise { + const runtime = new MCPServerRuntime({ name: 'parity', version: '1.0.0' }); + runtime.bridgeDataTools(flowBridge()); + const session = await openStdio(runtime.server); + try { + await handshake(session); + return await session.rpc(method, params); + } finally { + await session.close(); + } + } + + async function overHttp(method: string, params?: unknown): Promise { + const runtime = new MCPServerRuntime({ name: 'parity', version: '1.0.0' }); + const body = { jsonrpc: '2.0', id: 1, method, ...(params ? { params } : {}) }; + const res = await runtime.handleHttpRequest( + new Request('http://localhost/api/v1/mcp', { + method: 'POST', + headers: { 'content-type': 'application/json', accept: 'application/json, text/event-stream' }, + body: JSON.stringify(body), + }), + { bridge: flowBridge(), parsedBody: body }, + ); + return (await res.json()) as JsonRpcFrame; + } + + it('advertises the same `variables` input on both transports', async () => { + const schemaOf = (frame: JsonRpcFrame) => + (frame.result.tools as Array<{ name: string; inputSchema: any }>).find((t) => t.name === 'validate_expression')!.inputSchema; + const stdio = schemaOf(await overStdio('tools/list')); + expect(stdio.properties.variables.type).toBe('array'); + expect(stdio.properties.variables.items.required).toEqual(expect.arrayContaining(['name', 'type'])); + expect(stdio).toEqual(schemaOf(await overHttp('tools/list'))); + }); + + it('gives the shadowed-read warning, and refuses the input off a flow condition, alike', async () => { + const variables = [{ name: 'title', type: 'text' }]; + const shadowed = { objectName: 'task', expression: "title == 'x'", site: 'flow_condition', variables }; + const control = { objectName: 'task', expression: "title == 'x'", site: 'flow_condition' }; + const offSite = { objectName: 'task', expression: "record.title == 'x'", site: 'validation', variables }; + for (const args of [shadowed, control, offSite]) { + const stdio = await overStdio('tools/call', { name: 'validate_expression', arguments: args }); + expect(stdio.error, JSON.stringify(args)).toBeUndefined(); + const http = await overHttp('tools/call', { name: 'validate_expression', arguments: args }); + expect(stdio.result, JSON.stringify(args)).toEqual(http.result); + } + // Non-vacuous: the three answers differ in the way the input says. + const read = async (args: Record) => + (await overStdio('tools/call', { name: 'validate_expression', arguments: args })).result; + const withVariables = JSON.parse((await read(shadowed)).content[0].text); + expect(withVariables.warnings.map((w: { message: string }) => w.message)).toEqual([ + expect.stringContaining('`title`'), + ]); + expect(JSON.parse((await read(control)).content[0].text).warnings).toEqual([]); + const refused = await read(offSite); + expect(refused.isError).toBe(true); + expect(refused.content[0].text).toContain('`variables`'); + }); +}); diff --git a/packages/mcp/src/mcp-validate-expression.test.ts b/packages/mcp/src/mcp-validate-expression.test.ts index 1c826bbcc94..8d741036e50 100644 --- a/packages/mcp/src/mcp-validate-expression.test.ts +++ b/packages/mcp/src/mcp-validate-expression.test.ts @@ -235,6 +235,10 @@ describe('validate_expression MCP tool (#1928)', () => { // [#33147] The start condition describes a flow; the field, a formula's. { site: 'validation', startCondition: 'true' }, { startCondition: 'true' }, + // [#33176] …and so do the flow's declared variables. + { site: 'validation', variables: [{ name: 'title', type: 'text' }] }, + { site: 'template', variables: [{ name: 'title', type: 'text' }] }, + { variables: [] }, { site: 'validation', field: 'title' }, { site: 'flow_condition', field: 'title' }, { site: 'template', field: 'title' }, @@ -655,7 +659,11 @@ describe("validate_expression answers the gate's own passes at validation and fo const flowGateOn = ( objectName: string, expression: string, - { kind = 'record', startCondition }: { kind?: 'record' | 'schedule'; startCondition?: string } = {}, + { + kind = 'record', + startCondition, + variables, + }: { kind?: 'record' | 'schedule'; startCondition?: string; variables?: Array> } = {}, ) => { const { validations: _own, ...subject } = OBJECTS[objectName]!; const others = Object.keys(OBJECTS).filter((n) => n !== objectName).map((n) => OBJECTS[n]!); @@ -667,6 +675,7 @@ describe("validate_expression answers the gate's own passes at validation and fo flows: [{ name: 'g', type: 'autolaunched', + ...(variables ? { variables } : {}), nodes: [ { id: 'start', @@ -733,6 +742,68 @@ describe("validate_expression answers the gate's own passes at validation and fo expect([r.ok, r.errors, r.warnings]).toEqual([true, [], []]); }); + // ── [#33176] The flow's declared variables ── + + /** A variable named like the text field `title`, which the run binds before it flattens the record. */ + const SHADOWING = [{ name: 'title', type: 'number' }]; + + it("site flow_condition: given the flow's variables, a bare read of one is the variable — the gate's whole verdict", async () => { + const expression = 'title * 2 > 10'; + // The gate, on a flow declaring `title`, its shared validator included: + // the shadowed-read warning, and no type verdict, since the bare name + // holds the variable, not the text field. + const gate = flowGateOn('gp_req', expression, { variables: SHADOWING }); + expect(gate.map(([level]) => level)).toEqual(['warning']); + const r = await validate(runtime, gateBridge(), { objectName: 'gp_req', expression, site: 'flow_condition', variables: SHADOWING }); + expect(r.ok).toBe(true); + expect(r.errors).toEqual([]); + expect(verdicts(r)).toEqual(gate); + // The finding names the variable and the object whose field it shadows. + expect([r.warnings[0].message.includes('`title`'), r.warnings[0].message.includes('`gp_req`')]).toEqual([true, true]); + }); + + it('site flow_condition: CONTROL — without the variables the bare name is the field, as before', async () => { + const args = { objectName: 'gp_req', expression: 'title * 2 > 10', site: 'flow_condition' }; + const r = await validate(runtime, gateBridge(), args); + expect(r.ok).toBe(false); + expect(r.errors.map((e: { code: string }) => e.code)).toEqual(['type-mismatch-run-fails']); + expect(r.warnings).toEqual([]); + // …the answer the shared validator alone gives, the gate adding nothing. + expect(r).toEqual(await validate(runtime, gateBridge(false), args)); + }); + + it('site flow_condition: the shadowed-read warning is the gate\'s after a start condition too', async () => { + const startCondition = 'record.days != null'; + const gate = flowGateOn('gp_req', "title == 'x'", { startCondition, variables: SHADOWING }); + expect(gate.map(([level]) => level)).toEqual(['warning']); + const r = await validate(runtime, gateBridge(), { + objectName: 'gp_req', expression: "title == 'x'", site: 'flow_condition', startCondition, variables: SHADOWING, + }); + expect(verdicts(r)).toEqual(gate); + }); + + it('site flow_condition: a variable named like no field of the object gets no warning', async () => { + const variables = [{ name: 'threshold', type: 'number' }]; + for (const expression of ['threshold > 3', "record.title == 'x' && threshold > 3"]) { + expect(flowGateOn('gp_req', expression, { variables }), expression).toEqual([]); + const r = await validate(runtime, gateBridge(), { objectName: 'gp_req', expression, site: 'flow_condition', variables }); + expect([r.ok, r.errors, r.warnings], expression).toEqual([true, [], []]); + } + }); + + it('site flow_condition: a variable is judged by the spec\'s own flow-variable shape', async () => { + for (const variable of [{ name: 'title' }, { name: '$record', type: 'object' }, { name: 'title', type: 'text', input: true }]) { + const { json } = await call( + runtime, + toolsCall(1, 'validate_expression', { objectName: 'gp_req', expression: "title == 'x'", site: 'flow_condition', variables: [variable] }), + gateBridge(), + ); + const { result } = json as { result: { isError?: boolean; content: { text: string }[] } }; + expect(result.isError, JSON.stringify(variable)).toBe(true); + } + expect(reads).toEqual([]); + }); + it('site flow_condition: CONTROL — a clean condition answers as the definition-less bridge does', async () => { for (const expression of ["record.title == 'x'", 'record.days != null && record.days > 3', "title == 'x'"]) { expect(flowGateOn('gp_req', expression)).toEqual([]);