Skip to content

Commit 9f736f2

Browse files
fix(mcp): validate_expression takes the flow's declared variables at a flow condition (#33255)
Fixes #33176 Clause-②: yes (widening) — `validate_expression` gains an optional input, `variables`, carrying the flow's declared variables at `site: 'flow_condition'`, so the tool accepts a call it refuses today (`strictToolInput`); with no variables the answer is unchanged. Metadata-protocol: none Skills-docs: follow-up #33272 ## 维护者速读 - 改了什么:MCP `validate_expression` 在 `flow_condition` 位点新增可选入参 `variables`(流程声明的变量,形状即 spec 的 `FlowVariableSchema`)。给了它,探针流程声明这些变量,运行时流程门禁自己的"变量遮蔽同名字段"告警会回来;共享校验器把这些裸名当变量,不再按字段判类型。 - 为什么改:流程运行时先绑定声明的变量、再平铺记录字段,所以与字段同名的裸名读到的是变量;工具此前拿不到变量,会按字段给出类型判定,也给不出门禁的遮蔽告警,代理保存流程时才看到不同的答案。 - 风险与回滚:只加一个可选入参;不传时每个调用的答案与之前逐字相同(既有 72 个工具级用例不改动即为对照);门禁接受集不变。回滚即 revert 本 PR。 - 先看:`packages/lint/src/authored-site-expression.ts`(`authoredFlowProbe` 声明变量)、`packages/mcp/src/mcp-http-tools.ts`(`variables` 入参、非流程位点拒收、`flowVariables` 提示、`gatePassFindings` 传参)。 - 两分钟验证:`pnpm --filter @objectstack/mcp exec vitest run src/mcp-validate-expression.test.ts`,预期 77 个测试全过,其中 `title * 2 > 10` 带 `variables` 时无类型错误且得到门禁的遮蔽告警,不带时报 `type-mismatch-run-fails`。 - 需要你做的:无,席位落地。 ## What changes **Input shape: an optional `variables` input, not "read from a named served flow".** I chose the input. At a flow condition the tool reads only the subject object's served definition. Reading a named flow would need a new bridge member on both doors (the runtime bridge and the stdio bridge), a permission fence for flow metadata, and an answer for a flow that is not saved yet, which is the usual case while an agent authors one. That is outside this claim's file surface. The input takes the flow's own shape, `z.array(FlowVariableSchema)` from `@objectstack/spec/automation`, so an agent can hand over a flow's `variables` array as it stands. A malformed variable gets the spec's own refusal (a missing `type`, a `$` name, an alias key such as `input`). No new shape is written by hand, which follows the `AggregationNodeSchema` precedent on `aggregate_records`. With `variables` at `site: 'flow_condition'`: - **Gate half.** `gatePassFindings` hands the variables to `judgeAuthoredSiteExpression`, and `authoredFlowProbe` declares them on the one-flow probe. The gate's own walk then runs `warnShadowedFieldReads` on the probe's edge and gives its warning for a variable named like a field of `objectName` that is read bare. ⛔ Nothing is re-implemented: the probe only declares what the flow declares. - **Validator half.** The shared validator gets the names through its existing option `ExprSchemaHint.flowVariables`, the key the gate hands it on the same flow. A bare read of a declared variable is therefore not typed as the field. "Typed as the variable" means what it means at the gate: the bare name stays `dyn`. Neither the gate nor the validator types a variable by its declared `type`, so the tool does not either. - `record.FIELD` still reads the field and is judged as before. Without `variables` nothing changes. The probe carries no `variables` key and the hint carries no `flowVariables` key, so every call answers byte for byte as before. The tool description and the input description say that the shadowed-read warning needs the variables. `variables` on any other site is refused, as `startCondition` is: `` `variables` describes a flow; it applies only with site "flow_condition" ``. **stdio.** Measured: both transports register one tool definition through `wireBridgeTools`, and the stdio bridge already serves `servedObjectDefinitionOf`. So `stdio-data-bridge.ts` needs no change. A parity pin drives a real `StdioServerTransport` and the HTTP door with the same arguments. **Measured against the dispatch's mechanism notes** (on `origin/main` `295ce127`): - The input schema took `objectName, expression, site, severity, triggerKind, sweepObject, startCondition, field`. Holds. - The probe declared no variables. Holds; the module header said so. - Does the validator have an option for extra bindings? Yes: `flowVariables`, names only. It does not type a variable at all, so a bare variable is left untyped rather than typed by the variable's `type`. - Does stdio serve the same definition? Yes, as described above. ## Tests - `packages/lint/src/authored-site-expression.test.ts` (50 → 57) - **The flow-condition table.** `warnShadowedFieldReads` moves from `pass-silent-here` to `pass`. Its fixture now carries `variables`, and the entry is pinned equal to the runtime gate at the start, `decision` and edge slots of a hand-written flow that declares the same variables (+2 slot cases). - **New block `with the flow's declared variables (#33176)`** (+5): - the probe declares the variables as handed, and has no `variables` key without them; - CONTROL: without variables the gate and the entry both say nothing; - a variable that shadows no field gets no warning; - only the bare read is warned, and `record.title` is not; - every trigger kind, and a condition after a start condition, answers as the gate. - `packages/mcp/src/mcp-validate-expression.test.ts` (72 → 77) - (a) Variable `title` shadows the text field `title`, read bare in `title * 2 > 10`. The tool's whole verdict, validator half included, equals the gate's verdict on a flow declaring it: one shadowed-read warning, no errors, `ok: true`. The finding names `title` and `gp_req`. - (b) CONTROL, the same call without `variables`: `ok: false` with `type-mismatch-run-fails`, the bare name typed as the field, equal to the definition-less bridge's answer. - The warning is the gate's after a `startCondition` too. - (d) A variable that shadows no field (`threshold`) gets `[true, [], []]`, as at the gate. - The spec's own variable refusals: a missing `type`, `$record`, the alias `input`. - (c) The refusal table gains three rows: `variables` at `validation`, at `template` and at the default site. - `packages/mcp/src/mcp-stdio-tools.test.ts` (18 → 20): the same `variables` input schema is advertised on stdio and HTTP. The shadowed call, the CONTROL call and the off-site refusal each give the same `tools/call` result on both transports, and the three answers differ as the input says they should. ## Acceptance notes - **The validator half is handed the declared variables, not every name the run binds.** On a real flow the gate also leaves node ids, `screen` field names and the engine's own names (`record`, `previous`, `vars`, `current_user`) untyped. The tool is handed what the flow declares under `variables`, so a bare name that one of those others holds is still typed as the field here. The gate's shadowed-read warning reads declared variables only, so that warning is exact. - **`type` is required by the spec's shape and read by neither half**, the same as at the gate. - **Ruling grep** (`ruling|Ruling-ref|maintainer|⛔|#33147|#32872`) in the touched functions: - `authored-site-expression.ts`: one binding line, "⛔ Nothing here re-implements a pass", which this change keeps. Every other hit is a provenance tag. - The `validate_expression` handler and `gatePassFindings` in `mcp-http-tools.ts`: no ruling, maintainer or ⛔ line, and 10 provenance tags. - No maintainer ruling on #33176. - **Serial constraint with #33205** (draft, #33154). I read its diff at head `9d07ffa0`. It touches `authored-site-expression.test.ts` at lines 103–153 (`walkPushes`, `CYCLE`) and `validate-expressions.ts`. This PR touches that test file only at lines 455–780 and does not touch `validate-expressions.ts`, so no hunk is shared. - The header of `authored-site-expression.ts` said the shadowed-read warning never fires through the entry. It now says the warning fires when the variables are given, and does not fire, as before, when they are not. ## Verification Head `4cae1f45ab` (`origin/main` `295ce127` plus two commits). Every reading below was taken at that head. <details><summary>Package suites, typecheck and lint</summary> ``` os-verify-lock: pnpm --filter @objectstack/lint --filter @objectstack/mcp run test packages/lint test: Test Files 208 passed (208) Tests 8877 passed (8877) packages/mcp test: Test Files 64 passed (64) Tests 1174 passed (1174) VERDICT command-exit 0 os-verify-lock: pnpm --filter @objectstack/lint --filter @objectstack/mcp run typecheck packages/lint typecheck$ tsc --noEmit && pnpm check:test-typecheck packages/mcp typecheck$ tsc --noEmit && pnpm check:test-typecheck VERDICT command-exit 0 pnpm lint (eslint . --no-inline-config) :: exit 0 ``` </details> <details><summary>Ablations (each through scripts/ablation-replace.mjs; each restored with blob == HEAD and git diff HEAD empty)</summary> ``` L1 lint: the probe declares no variables (the `variables` spread in authoredFlowProbe deleted) lint suite reads src: 6 failed | 51 passed (57) — the 3 warnShadowedFieldReads slot cases, the probe read-back, the bare-read-only case, the trigger-kind/start-condition case A1 mcp: the validator hint loses `flowVariables` 1 failed | 96 passed (97) — "given the flow's variables, a bare read of one is the variable" A2 mcp: `variables` not handed to the gate 3 failed | 94 passed (97) — the whole-verdict pin, the start-condition pin, the stdio parity pin A3 first attempt void: deleting the `if` line alone left a syntax error, no tests ran. Re-done as A3b: A3b mcp: the off-site refusal disabled (`if (false)`) 2 failed | 95 passed (97) — the refusal table, the stdio parity pin ``` The MCP suites read `@objectstack/lint` through its `dist/` (no vitest alias). L1 was read on the lint suite, which imports `src`. The MCP leg of the probe is covered by A2. </details> <details><summary>Derived gate families (dispatch-gates --commands, 67 families; --ran reconciliation)</summary> ``` dispatch-gates: gate list derived from the tree of 'objectstack-ai/objectstack' at commit 4cae1f4 65 families :: exit 0 pnpm check:dual-build-cjs-loads :: exit 3 PREREQUISITE NOT MET (no full build here) — NOT MEASURED, CI's Lint & Repo Gates runs it pnpm check:lean-entry-closure :: exit 3 PREREQUISITE NOT MET (objectql dist absent) — NOT MEASURED, same ✓ dispatch-gates --ran: 67 derived famil(ies) accounted for — 65 run, 2 NOT-MEASURED (2 DERIVED from a recorded exit 3). ``` `test:repo` and the full farm are CI's. No export was renamed or removed, so `check-platform-checklist` is not owed. Full dogfood is not owed: the title has no `!` and the Clause-② arm is `(widening)`. </details> --- _Generated by [Claude Code](https://claude.ai/code/session_017xRBcSZ51q2s3nMfakhdJ3)_ <!-- signed: domain:server#7 · sunhaolin · session_017xRBcSZ51q2s3nMfakhdJ3 --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent fdb76fb commit 9f736f2

6 files changed

Lines changed: 301 additions & 22 deletions
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
'@objectstack/mcp': patch
3+
'@objectstack/lint': patch
4+
---
5+
6+
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
7+
8+
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.
9+
Metadata-protocol: none
10+
11+
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.
12+
13+
- `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`.
14+
- Without `variables`, the answer is byte for byte what it was, and the tool description says the shadowed-read warning needs them.
15+
- `@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.

‎packages/lint/src/authored-site-expression.test.ts‎

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,8 @@ type FlowFixture = {
453453
expression: string;
454454
severity: 'error' | 'warning';
455455
names: string;
456+
/** [#33176] The flow's declared variables, handed to the entry as the caller hands them and declared on the hand-written flow. */
457+
variables?: Array<Record<string, unknown>>;
456458
};
457459

458460
type FlowClassified =
@@ -503,11 +505,17 @@ const FLOW_CALLS: Record<string, FlowClassified> = {
503505
fixture: { objects: [FLOW_OBJ], expression: 'foo.bar == 1' },
504506
buildDoor: {},
505507
},
508+
// [#33176] The flow's declared variables, which the caller hands the entry
509+
// and the probe declares; without them it cannot fire (pinned below).
506510
warnShadowedFieldReads: {
507-
kind: 'pass-silent-here',
508-
why: "needs the flow's declared variables, which the tool is not handed; the probe declares none",
509-
fixture: { objects: [FLOW_OBJ], expression: "title == 'x'" },
510-
buildDoor: { variables: [{ name: 'title', type: 'text' }] },
511+
kind: 'pass',
512+
fixture: {
513+
objects: [FLOW_OBJ],
514+
expression: "title == 'x'",
515+
severity: 'warning',
516+
names: '`title`',
517+
variables: [{ name: 'title', type: 'text' }],
518+
},
511519
},
512520
refuseStoredMetadataBodyHop: {
513521
kind: 'pass-silent-here',
@@ -598,7 +606,7 @@ function flowGateOn(
598606
function flowEntryOn(
599607
objects: Array<Record<string, unknown>>,
600608
expression: string,
601-
opts: { triggerKind?: AuthoredFlowTrigger; startCondition?: string } = {},
609+
opts: { triggerKind?: AuthoredFlowTrigger; startCondition?: string } & HandFlowOverrides = {},
602610
) {
603611
return judgeAuthoredSiteExpression({ site: 'flow_condition', objectName: 'obj', expression, objects, ...opts })
604612
.map((i) => [i.severity ?? 'error', i.message]);
@@ -618,12 +626,13 @@ describe('judgeAuthoredSiteExpression at a flow condition — every gate pass is
618626

619627
for (const [name, classified] of Object.entries(FLOW_CALLS)) {
620628
if (classified.kind === 'pass') {
621-
const { objects, expression, severity, names } = classified.fixture;
629+
const { objects, expression, severity, names, variables } = classified.fixture;
630+
const declared = variables ? { variables } : {};
622631
for (const slot of ['start', 'decision', 'edge'] as const) {
623632
it(`${name} refuses through the entry as the gate does at a flow's ${slot} condition`, () => {
624-
const gate = flowGateOn(objects, expression, { slot });
633+
const gate = flowGateOn(objects, expression, { slot, ...declared });
625634
expect(gate.length, 'the fixture trips the gate').toBeGreaterThan(0);
626-
const entry = flowEntryOn(objects, expression);
635+
const entry = flowEntryOn(objects, expression, declared);
627636
expect(entry).toEqual(gate);
628637
expect(entry.map(([level]) => level)).toEqual(gate.map(() => severity));
629638
expect(entry.some(([, message]) => message!.includes(names))).toBe(true);
@@ -716,6 +725,58 @@ describe('judgeAuthoredSiteExpression at a flow condition — every gate pass is
716725
});
717726
});
718727

728+
// ── [#33176] The flow's declared variables, at a flow condition ───────────
729+
//
730+
// Handed the flow's `variables`, the probe declares them, and the gate's
731+
// shadowed-read warning (`warnShadowedFieldReads`, a `pass` in the table
732+
// above with those variables) comes back through the entry. Handed none, the
733+
// probe is the flow it was before the key, and the warning cannot fire.
734+
735+
describe("judgeAuthoredSiteExpression with the flow's declared variables (#33176)", () => {
736+
const SHADOWING = [{ name: 'title', type: 'text' }];
737+
738+
it('declares the variables it is handed, as handed, and no `variables` key without them', () => {
739+
const variables = [{ name: 'title', type: 'text', isInput: true }, { name: 'limit', type: 'number' }];
740+
expect(authoredFlowProbe({ objectName: 'obj', expression: 'true', variables }).variables).toEqual(variables);
741+
expect(Object.keys(authoredFlowProbe({ objectName: 'obj', expression: 'true' }))).not.toContain('variables');
742+
});
743+
744+
it('CONTROL — without the variables the shadowed read answers nothing, at the gate and through the entry', () => {
745+
// A flow that declares no variable gets no warning at the runtime door…
746+
expect(flowGateOn([FLOW_OBJ], "title == 'x'")).toEqual([]);
747+
// …and the probe, handed none, is that flow.
748+
expect(flowEntryOn([FLOW_OBJ], "title == 'x'")).toEqual([]);
749+
});
750+
751+
it('a variable named like no field of the object gets no warning', () => {
752+
const variables = [{ name: 'threshold', type: 'number' }];
753+
for (const expression of ['threshold > 3', "record.title == 'x' && threshold > 3"]) {
754+
expect(flowGateOn([FLOW_OBJ], expression, { variables }), expression).toEqual([]);
755+
expect(flowEntryOn([FLOW_OBJ], expression, { variables }), expression).toEqual([]);
756+
}
757+
});
758+
759+
it('warns on the bare read alone: `record.FIELD` reads the field, whatever the variables', () => {
760+
expect(flowGateOn([FLOW_OBJ], "record.title == 'x'", { variables: SHADOWING })).toEqual([]);
761+
expect(flowEntryOn([FLOW_OBJ], "record.title == 'x'", { variables: SHADOWING })).toEqual([]);
762+
const both = flowGateOn([FLOW_OBJ], 'record.title == title', { variables: SHADOWING });
763+
expect(both.map(([level]) => level)).toEqual(['warning']);
764+
expect(flowEntryOn([FLOW_OBJ], 'record.title == title', { variables: SHADOWING })).toEqual(both);
765+
});
766+
767+
it('answers as the gate on every trigger kind and after a start condition', () => {
768+
for (const kind of ['record_change', 'time_relative', 'schedule', 'api', 'none'] as const) {
769+
const gate = flowGateOn([FLOW_OBJ], "title == 'x'", { kind, variables: SHADOWING });
770+
expect(gate.map(([level]) => level), kind).toEqual(['warning']);
771+
expect(flowEntryOn([FLOW_OBJ], "title == 'x'", { triggerKind: kind, variables: SHADOWING }), kind).toEqual(gate);
772+
}
773+
const startCondition = 'record.days != null';
774+
const gate = flowGateOn([FLOW_OBJ], "title == 'x'", { startCondition, variables: SHADOWING });
775+
expect(gate.map(([level]) => level)).toEqual(['warning']);
776+
expect(flowEntryOn([FLOW_OBJ], "title == 'x'", { startCondition, variables: SHADOWING })).toEqual(gate);
777+
});
778+
});
779+
719780
// ── [#33147] The field being authored, at the formula site ───────────────
720781
//
721782
// Named as `field`, a field the definition declares keeps its definition with

‎packages/lint/src/authored-site-expression.ts‎

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@
3333
* door gives the null guard on a `record.` / `previous.` read and the
3434
* unprovisioned-anchor warning. It never judges a bare name's null guard or a
3535
* root the run does not bind: those are `os validate`'s whole-flow judgments,
36-
* over the flow's variables, node ids and entrances. The door does warn on a
37-
* bare read of a name a flow variable shares with a field
38-
* (`warnShadowedFieldReads`), but that needs the flow's declared variables,
39-
* and the caller does not hand this entry any, so the probe declares none and
40-
* that warning never fires here. So `[]` at a flow condition is that door's
41-
* verdict, short of the shadowed-read warning, and not `os validate`'s.
36+
* over the flow's variables, node ids and entrances. So `[]` at a flow
37+
* condition is that door's verdict, and not `os validate`'s.
38+
*
39+
* [#33176] The door also warns on a bare read of a name a declared flow
40+
* variable shares with a field of the object (`warnShadowedFieldReads`): the
41+
* variable wins at run time. That reads the flow's declared `variables`, so
42+
* the probe declares the ones the caller hands it, as the flow declares them,
43+
* and the gate's own warning comes back. Handed none, the probe declares none
44+
* and that warning cannot fire, exactly as before this key.
4245
*/
4346

4447
import { analyzeRelationshipTraversals } from '@objectstack/formula';
@@ -96,6 +99,15 @@ export interface AuthoredSiteExpression {
9699
* start condition itself, or follows none (the gate answers both alike).
97100
*/
98101
startCondition?: string;
102+
/**
103+
* [#33176] site `flow_condition` only: the flow's declared `variables`, in
104+
* the shape `FlowSchema.variables` declares them (`FlowVariableSchema`: a
105+
* `name` and a `type` at least). The probe declares them, so the gate binds
106+
* a bare read of one to the variable and gives its shadowed-read warning
107+
* where one shares a name with a field of `objectName`. Absent: the probe
108+
* declares none, as before this key.
109+
*/
110+
variables?: readonly AnyRec[];
99111
/**
100112
* The object definitions as the registry serves them: the subject's, and
101113
* those of the objects its references name that the expression reads
@@ -166,20 +178,26 @@ function probeTrigger(
166178
* [#33147] The flow the gate judges for one flow condition: a start node
167179
* bound to `objectName` with the trigger the caller names (and its start
168180
* condition, when given), and one edge to an end node carrying the
169-
* condition. Exported for the read-back pin; the entry is its one caller.
181+
* condition; [#33176] it declares the flow's `variables` when the caller
182+
* hands them. Exported for the read-back pin; the entry is its one caller.
170183
*/
171184
export function authoredFlowProbe({
172185
objectName,
173186
expression,
174187
triggerKind = 'record_change',
175188
sweepObject,
176189
startCondition,
177-
}: Pick<AuthoredSiteExpression, 'objectName' | 'expression' | 'triggerKind' | 'sweepObject' | 'startCondition'>): AnyRec {
190+
variables,
191+
}: Pick<
192+
AuthoredSiteExpression,
193+
'objectName' | 'expression' | 'triggerKind' | 'sweepObject' | 'startCondition' | 'variables'
194+
>): AnyRec {
178195
const trigger = probeTrigger(triggerKind, objectName, sweepObject);
179196
return {
180197
name: AUTHORED_FLOW_NAME,
181198
label: AUTHORED_FLOW_NAME,
182199
type: trigger.type,
200+
...(variables !== undefined ? { variables: [...variables] } : {}),
183201
nodes: [
184202
{
185203
id: 'start',
@@ -230,6 +248,7 @@ export function judgeAuthoredSiteExpression({
230248
triggerKind,
231249
sweepObject,
232250
startCondition,
251+
variables,
233252
objects,
234253
}: AuthoredSiteExpression): ExprIssue[] {
235254
const authored = objects.map((o) => stripInjectedSystemColumns(o));
@@ -249,7 +268,7 @@ export function judgeAuthoredSiteExpression({
249268
// rules are not what is judged here (left out, as at a validation site).
250269
stack = {
251270
objects: [body, ...others],
252-
flows: [authoredFlowProbe({ objectName, expression, triggerKind, sweepObject, startCondition })],
271+
flows: [authoredFlowProbe({ objectName, expression, triggerKind, sweepObject, startCondition, variables })],
253272
};
254273
runtimeWriteType = 'flow';
255274
// Every finding on the probe's one edge carries that edge's key path.

0 commit comments

Comments
 (0)