Skip to content

Commit e569126

Browse files
committed
feat(lint): judge a subflow or map call against the deployment's flows at the publish gate
Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q4EjjQkY5yXZrN9BEA3rEK
1 parent 1208460 commit e569126

7 files changed

Lines changed: 254 additions & 23 deletions

File tree

‎packages/lint/src/authoring-rules.ts‎

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,18 @@ export interface AuthoringRuleContext {
522522
* contradict (AGENTS.md "Startup registry reads").
523523
*/
524524
flowNodeTypeVocabulary?: ReadonlySet<string>;
525+
/**
526+
* The names of every flow the deployment holds: every installed package's
527+
* flows, the stored flow rows in every state (drafts included), the flows
528+
* the written item's publish batch carries, and every flow the automation
529+
* engine holds. Set only by the runtime publish gate, and only when its host
530+
* could read all of them; ABSENT on the three CLI commands, whose stack is
531+
* one app. Read by `validateFlowSubflowTargets` only: present and non-empty,
532+
* a `subflow` / `map` call naming none of them is refused; absent or empty
533+
* at the runtime gate, that rule judges nothing there (the written flow
534+
* alone is no universe).
535+
*/
536+
deploymentFlowNames?: ReadonlySet<string>;
525537
}
526538

527539
export interface AuthoringRule {
@@ -1728,20 +1740,27 @@ export const AUTHORING_RULES: readonly AuthoringRule[] = [
17281740
// GET → edit → PUT passes, and a secretless `api` flow is refused at `/meta`
17291741
// with this id instead of being stored for the engine to refuse at
17301742
// registration.
1731-
// #35143 — a `subflow` / `map` node calling a flow this stack does not
1732-
// define. CLI only: the runtime gate's flow snapshot holds just the written
1733-
// flow, so every call to another stored flow would read as unknown there.
1734-
// Advisory, with the sibling unknown-object hedge: another installed package
1735-
// may ship the called flow.
1743+
// #35143 — a `subflow` / `map` node calling a flow that does not exist.
1744+
// One verdict per door (the rule's docblock): on the CLI a `warning`, since
1745+
// another installed package may ship the called flow; at the runtime publish
1746+
// gate an `error`, judged against the deployment's flow names the host
1747+
// states (`deploymentFlowNames`), because the gate's own flow snapshot holds
1748+
// just the written flow. With no stated names, or none, the runtime gate's
1749+
// pass judges nothing: a call to another stored flow cannot be told from a
1750+
// call to a missing one there.
17361751
{
17371752
name: 'validateFlowSubflowTargets',
1738-
tier: 'advisory',
1753+
tier: 'gating',
17391754
input: 'normalized',
17401755
commands: ALL,
17411756
source: 'packages/lint/src/validate-flow-trigger-readiness.ts',
1742-
surfaces: CLI_ONLY,
1743-
surfaceReason: RUNTIME_NEEDS_FULL_SNAPSHOT,
1744-
run: (stack) => validateFlowSubflowTargets(stack),
1757+
surfaces: CLI_AND_RUNTIME,
1758+
runtimeTypes: ['flow'],
1759+
run: (stack, ctx) => {
1760+
if (ctx.runtimeWriteType === undefined) return validateFlowSubflowTargets(stack);
1761+
if (ctx.deploymentFlowNames === undefined || ctx.deploymentFlowNames.size === 0) return [];
1762+
return validateFlowSubflowTargets(stack, { deploymentFlowNames: ctx.deploymentFlowNames });
1763+
},
17451764
},
17461765
{
17471766
name: 'validateFlowApiTriggerSecret',

‎packages/lint/src/rule-explanations.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7175,9 +7175,11 @@ const FLOW_SUBFLOW_UNKNOWN_EXPLANATION: RuleExplanation = {
71757175
paragraphs: [
71767176
'A `subflow` or `map` step runs another flow by the name in `config.flowName`. When no flow has that ' +
71777177
'name, the step fails on every run, and every run of the calling flow fails with it.',
7178-
'A warning, and on the CLI only. Another installed package may ship the called flow, which this stack ' +
7179-
'cannot see. And the runtime publish gate judges one flow per write, so a call to another stored flow ' +
7180-
'could not be told from a call to a missing one there.',
7178+
'On the CLI it is a warning: another installed package may ship the called flow, which this stack ' +
7179+
'cannot see. At publish it is an error, judged against every flow the deployment holds: every ' +
7180+
'installed package\'s flows, the stored flows in every state (a draft counts), the flows published in ' +
7181+
'the same batch, and the flows the automation engine holds. Save the called flow first, then publish ' +
7182+
'the one that calls it.',
71817183
],
71827184
};
71837185

‎packages/lint/src/runtime-gate.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,15 @@ export function runRuntimeAuthoringRules(args: {
14671467
* same value in every pass. Omitted, a flow node's type is not judged.
14681468
*/
14691469
flowNodeTypeVocabulary?: readonly string[];
1470+
/**
1471+
* On a `flow` write: the names of every flow the deployment holds, as the
1472+
* host read them (every installed package's flows, the stored flow rows in
1473+
* every state, the batch's own flow drafts, the automation engine's flows).
1474+
* Handed to the rules as `AuthoringRuleContext.deploymentFlowNames`, the
1475+
* same value in every pass, so it moves no baseline. Omitted, or empty, a
1476+
* `subflow` / `map` call to another flow is not judged at this door.
1477+
*/
1478+
deploymentFlowNames?: readonly string[];
14701479
}): RuntimeGateResult {
14711480
const rules = runtimeAuthoringRulesFor(args.type);
14721481
const empty: RuntimeGateResult = { errors: [], advisories: [], rulesRun: [] };
@@ -1539,6 +1548,11 @@ export function runRuntimeAuthoringRules(args: {
15391548
...(args.flowNodeTypeVocabulary !== undefined && args.flowNodeTypeVocabulary.length > 0
15401549
? { flowNodeTypeVocabulary: new Set(args.flowNodeTypeVocabulary) }
15411550
: {}),
1551+
// The deployment's flow names, stated by the host on a flow write; the
1552+
// same value in every pass, so it moves no baseline.
1553+
...(args.deploymentFlowNames !== undefined && args.deploymentFlowNames.length > 0
1554+
? { deploymentFlowNames: new Set(args.deploymentFlowNames) }
1555+
: {}),
15421556
};
15431557
const added = addedFindings(rules, snapshots, ctx, args.type, args.item);
15441558

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

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,21 +1920,52 @@ function signatureUnusableFinding(
19201920
* the engine to run that flow by name, and the engine finds none, so the step
19211921
* fails every run (`subflow 'X' failed`).
19221922
*
1923-
* Its OWN exported rule, run on the CLI only, for the reason
1924-
* `flow-trigger-unknown-object` carries a hedge plus one more: the runtime
1925-
* publish gate judges a flow write over a snapshot whose `flows` holds only
1926-
* the written item, so a call to any other stored flow would read as unknown
1927-
* there. On the CLI the stack is one app; a flow another installed package
1928-
* ships is a legitimate target, so the finding is advisory, as that sibling's
1929-
* is without a stated closure.
1923+
* Its OWN exported rule, with one verdict per door, decided by #5762's
1924+
* question (is what this rule reads enough to know?):
1925+
*
1926+
* - On the CLI the stack is one app. A flow another installed package ships
1927+
* is a legitimate target the stack cannot see, so the finding is a
1928+
* `warning`, as `flow-trigger-unknown-object`'s is without a stated closure.
1929+
* - At the runtime publish gate the snapshot's `flows` holds only the written
1930+
* item, so that snapshot alone cannot judge a call. The host states the
1931+
* names of every flow the deployment holds instead
1932+
* (`deploymentFlowNames`: every installed package's flows, the stored rows
1933+
* in every state, drafts included, this publish batch's own drafts, and
1934+
* every flow the automation engine holds). A name in none of those is a
1935+
* flow the engine cannot find, so every run of the step fails: `error`.
1936+
* The set is the whole deployment, never narrowed to the written package's
1937+
* closure, so a call this rule lets through may still be one the engine's
1938+
* package-scoped name resolution refuses (ADR-0150 D3); this rule does not
1939+
* judge that. A draft counts as held, so two new flows that call each
1940+
* other can be published one at a time.
1941+
* - At the runtime gate with no stated names, or an empty set, it judges
1942+
* nothing: no deployment state was handed over, and the written flow
1943+
* alone is no universe (the #24668 reading of an empty set with no
1944+
* closure).
19301945
*/
19311946
export const FLOW_SUBFLOW_UNKNOWN = 'flow-subflow-unknown';
19321947

1933-
export function validateFlowSubflowTargets(stack: AnyRec): FlowTriggerReadinessFinding[] {
1948+
export interface FlowSubflowTargetOptions {
1949+
/**
1950+
* The names of every flow the deployment holds, as the runtime publish
1951+
* gate's host states them (`AuthoringRuleContext.deploymentFlowNames`).
1952+
* Present and non-empty, a call that names none of them, nor a flow of
1953+
* this stack, is an `error`. Absent, the CLI's `warning` verdict applies.
1954+
*/
1955+
readonly deploymentFlowNames?: ReadonlySet<string>;
1956+
}
1957+
1958+
export function validateFlowSubflowTargets(
1959+
stack: AnyRec,
1960+
options: FlowSubflowTargetOptions = {},
1961+
): FlowTriggerReadinessFinding[] {
19341962
const findings: FlowTriggerReadinessFinding[] = [];
19351963
const flows = recordsOf(stack.flows);
19361964
if (flows.length === 0) return findings;
1937-
const known = new Set<string>();
1965+
const deployment = options.deploymentFlowNames !== undefined && options.deploymentFlowNames.size > 0
1966+
? options.deploymentFlowNames
1967+
: undefined;
1968+
const known = new Set<string>(deployment);
19381969
for (const flow of flows) {
19391970
const name = nonEmptyString(flow.name);
19401971
if (name) known.add(name);
@@ -1954,6 +1985,23 @@ export function validateFlowSubflowTargets(stack: AnyRec): FlowTriggerReadinessF
19541985
if (!called || known.has(called)) continue;
19551986
const nodeId = nonEmptyString(node.id) ?? String(node.type);
19561987
const nodeWhere = regionTrail ? `${regionTrail} › node "${nodeId}"` : `node "${nodeId}"`;
1988+
if (deployment) {
1989+
findings.push({
1990+
severity: 'error',
1991+
rule: FLOW_SUBFLOW_UNKNOWN,
1992+
where: `flow "${flowName}" › ${nodeWhere}`,
1993+
path: `${path}.config.flowName`,
1994+
message:
1995+
`${String(node.type)} node '${nodeId}' calls flow '${called}', but no flow of this deployment (an ` +
1996+
`installed package's, a stored flow or a draft) has that name, so the engine finds none and the ` +
1997+
`step fails on every run` +
1998+
suggestName(called, known),
1999+
hint:
2000+
`Set config.flowName to the exact name of a flow this deployment holds, or save the called flow ` +
2001+
`first (a draft is enough) and publish this one after it`,
2002+
});
2003+
continue;
2004+
}
19572005
findings.push({
19582006
severity: 'warning',
19592007
rule: FLOW_SUBFLOW_UNKNOWN,

‎packages/metadata-protocol/src/protocol-authoring-gate.ts‎

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import { withDisabledExtensionsFolded } from './metadata-diagnostics.js';
5959
import { unresolvedConnectorSteps, type ProbeAutomation } from './build-probes.js';
6060
import type { PackagedRuleCopyRefusal, RuntimeAuthoringIssue, RuntimeAuthoringVerdict, RuntimePendingDeclarations } from './runtime-authoring-gate.js';
6161
import { savedItemNameRefusal } from '@objectstack/metadata/view-container-name';
62+
import { isMissingTableError } from '@objectstack/metadata/errors';
6263
import { PLURAL_TO_SINGULAR, SINGULAR_TO_PLURAL } from '@objectstack/spec/shared';
6364
import type { IObjectQLEngine } from '@objectstack/spec/contracts';
6465
import type { StoredObjectExtension } from '@objectstack/lint/runtime';
@@ -110,6 +111,18 @@ export interface AuthoringGateHost {
110111
formRestoreRefusal: (type: string, item: unknown, name: string, binding: string | null) => Promise<Error | undefined>;
111112
/** [#34776] The automation engine's sealed node-type vocabulary ({@link sealedFlowNodeTypeVocabulary}). */
112113
sealedFlowNodeTypeVocabulary: () => readonly string[] | undefined;
114+
/**
115+
* The names of every flow the automation engine holds
116+
* ({@link automationFlowNames}). Optional: a host that does not state it
117+
* gets no `subflow` / `map` target judgement at the publish gate.
118+
*/
119+
automationFlowNames?: () => Promise<readonly string[] | undefined>;
120+
/**
121+
* The environment's `sys_metadata` rows, read as the stored-collection
122+
* fold reads them. Optional for the same reason; read by
123+
* {@link deploymentFlowNamesOf} for the stored flows of every state.
124+
*/
125+
findEnvironmentRows?: ObjectStackProtocolImplementation['findEnvironmentRows'];
113126
}
114127

115128
/** The body of `ObjectStackProtocolImplementation#restoredBodyDerivation`; the method's docblock there is its contract. */
@@ -341,7 +354,7 @@ export function withTargetFoldedWithout(
341354
* The members {@link judgeRuntimeAuthoringRules} reads: the host facts and
342355
* collections every judgement of the shared rules is made against.
343356
*/
344-
type RuntimeRulesHost = Pick<AuthoringGateHost, 'authoringChannel' | 'engine' | 'foldStoredCollection' | 'hierarchyResolverMounted' | 'orgWallEnforced' | 'resolveSduiManifest' | 'resolveWritePackageScope' | 'rlsMembershipResolverKeys' | 'sealedFlowNodeTypeVocabulary' | 'tenancyPostureInForce'>;
357+
type RuntimeRulesHost = Pick<AuthoringGateHost, 'authoringChannel' | 'automationFlowNames' | 'engine' | 'findEnvironmentRows' | 'foldStoredCollection' | 'hierarchyResolverMounted' | 'orgWallEnforced' | 'resolveSduiManifest' | 'resolveWritePackageScope' | 'rlsMembershipResolverKeys' | 'sealedFlowNodeTypeVocabulary' | 'tenancyPostureInForce'>;
345358

346359
/** The body of `ObjectStackProtocolImplementation#assertRuntimeAuthoringRules`; the method's docblock there is its contract. */
347360
export async function assertRuntimeAuthoringRules(
@@ -634,6 +647,14 @@ export async function judgeRuntimeAuthoringRules(
634647
// flow write only, and passed in so the gate stays pure.
635648
const flowNodeTypeVocabulary = singular === 'flow' ? this.sealedFlowNodeTypeVocabulary() : undefined;
636649

650+
// [#35143] Every flow the deployment holds, by name, on a flow write only:
651+
// the universe a `subflow` / `map` call is judged against, since the gate's
652+
// own snapshot holds just the written flow. `undefined` when one source
653+
// could not be read, and then no call is judged.
654+
const deploymentFlowNames = singular === 'flow'
655+
? await deploymentFlowNamesOf.call(this, listRegisteredCollection('flow', 'flows'))
656+
: undefined;
657+
637658
// [#26188] Is a hierarchy resolver mounted — a host fact of the same kind,
638659
// read here per publish and passed in so the gate stays pure.
639660
const hierarchyResolverMounted = this.hierarchyResolverMounted();
@@ -685,6 +706,8 @@ export async function judgeRuntimeAuthoringRules(
685706
...(rlsMembershipResolverKeys !== undefined ? { rlsMembershipResolverKeys } : {}),
686707
// With it the gate refuses a flow node no executor of this deployment runs.
687708
...(flowNodeTypeVocabulary !== undefined ? { flowNodeTypeVocabulary } : {}),
709+
// [#35143] With it the gate refuses a subflow / map call to a flow the deployment does not hold.
710+
...(deploymentFlowNames !== undefined ? { deploymentFlowNames } : {}),
688711
// [#26188] With it the gate refuses a hierarchy scope no resolver enforces.
689712
...(hierarchyResolverMounted !== undefined ? { hierarchyResolverMounted } : {}),
690713
// [#28448] With it the gate refuses a rule copy that changes what the package owns.
@@ -695,6 +718,59 @@ export async function judgeRuntimeAuthoringRules(
695718
return verdict;
696719
}
697720

721+
/**
722+
* [#35143] The names of every flow this deployment holds, the universe the
723+
* publish gate judges a `subflow` / `map` node's `config.flowName` against
724+
* (`validateFlowSubflowTargets`): the registry's flows (`registered`, every
725+
* installed package's, a disabled package's included), the stored flow rows
726+
* of EVERY state (a draft counts, so two new flows that call each other can be
727+
* published one at a time), and every flow the automation engine holds (a flow
728+
* registered straight into the engine has no metadata row). The batch's own
729+
* flow drafts are added by the gate ({@link evaluateRuntimeAuthoringGate}).
730+
*
731+
* Union only: every source can only make more names resolvable, never fewer.
732+
* `undefined`, and so no judgement, when the host cannot state one of the
733+
* sources: no stored-row reader or engine reader, or a read that fails (said
734+
* once, at `warn`). Judging against a partial universe would refuse a call to
735+
* a flow the missing source holds.
736+
*/
737+
async function deploymentFlowNamesOf(
738+
this: Pick<AuthoringGateHost, 'automationFlowNames' | 'findEnvironmentRows'>,
739+
registered: readonly unknown[],
740+
): Promise<readonly string[] | undefined> {
741+
if (typeof this.findEnvironmentRows !== 'function' || typeof this.automationFlowNames !== 'function') return undefined;
742+
const engineNames = await this.automationFlowNames();
743+
if (engineNames === undefined) return undefined;
744+
const names = new Set<string>(engineNames);
745+
for (const item of registered) {
746+
const name = (item as { name?: unknown } | null)?.name;
747+
if (typeof name === 'string' && name !== '') names.add(name);
748+
}
749+
try {
750+
for (const type of ['flow', 'flows']) {
751+
// [ADR-0131 D6] The environment's rows, every state, as the
752+
// stored-collection fold reads them (that fold reads `active` only).
753+
const rows = await this.findEnvironmentRows('sys_metadata', {
754+
where: { type },
755+
context: { isSystem: true },
756+
});
757+
for (const row of (rows ?? []) as Array<{ name?: unknown }>) {
758+
if (typeof row?.name === 'string' && row.name !== '') names.add(row.name);
759+
}
760+
}
761+
} catch (error) {
762+
if (!isMissingTableError(error, 'sys_metadata')) {
763+
console.warn(
764+
`[Protocol] the runtime authoring gate could not read the stored flows: `
765+
+ `${(error as { message?: string } | null)?.message ?? String(error)}. `
766+
+ `This write is NOT blocked; a subflow or map step's called flow is not judged for it.`,
767+
);
768+
return undefined;
769+
}
770+
}
771+
return [...names];
772+
}
773+
698774
/**
699775
* [#32666] The flows of the package an object write belongs to — the subjects
700776
* the object write is judged against (`RuntimeStackContext.flows`).
@@ -982,6 +1058,37 @@ function draftConnectorAdvisories(
9821058
* a type, so "no executor" is not a verdict anyone may act on (AGENTS.md
9831059
* "Startup registry reads").
9841060
*/
1061+
/**
1062+
* [#35143] The names of every flow the automation engine holds
1063+
* (`IAutomationService.listFlows`), read per publish and recorded nowhere.
1064+
* `undefined` when no automation service is registered (no flow runs here at
1065+
* all) or its read fails; then no `subflow` / `map` call is judged.
1066+
*/
1067+
export async function automationFlowNames(
1068+
this: { readonly getServicesRegistry?: () => Map<string, any> },
1069+
): Promise<readonly string[] | undefined> {
1070+
let automation: unknown;
1071+
try {
1072+
automation = this.getServicesRegistry?.().get('automation');
1073+
} catch {
1074+
return undefined;
1075+
}
1076+
const list = (automation as { listFlows?: () => unknown } | undefined)?.listFlows;
1077+
if (typeof list !== 'function') return undefined;
1078+
let names: unknown;
1079+
try {
1080+
names = await list.call(automation);
1081+
} catch (error) {
1082+
console.warn(
1083+
`[Protocol] the runtime authoring gate could not list the automation engine's flows: `
1084+
+ `${(error as { message?: string } | null)?.message ?? String(error)}. `
1085+
+ `This write is NOT blocked; a subflow or map step's called flow is not judged for it.`,
1086+
);
1087+
return undefined;
1088+
}
1089+
return Array.isArray(names) ? names.filter((n): n is string => typeof n === 'string') : undefined;
1090+
}
1091+
9851092
export function sealedFlowNodeTypeVocabulary(
9861093
this: { readonly getServicesRegistry?: () => Map<string, any> },
9871094
): readonly string[] | undefined {

0 commit comments

Comments
 (0)