From 48596f84723c4d728135c9c1ba67857f94f99040 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Mon, 30 Nov 2026 23:56:36 -0800 Subject: [PATCH 1/2] fix(workflow-code): warn on unwired container bodies and unhandled guardrails - W_BODY_UNWIRED: a loop or parallel with blocks inside but no edge from its start; hint p.to() - W_GUARDRAILS_UNHANDLED: a guardrails block with no error edge; hint check.onError() - a nested member wiring source (known.route.yes.to) hints the branch form instead of the top-level-statement hint - executor: an unreached loop/parallel body reports no block wired from its start - child-results reuses the iterative stronglyConnected from compile/edges --- .../docs/workflows/code/diagnostics.mdx | 2 + apps/docs/openapi-v2-workflows.json | 20 ++++ apps/sim/executor/orchestrators/loop.ts | 2 +- apps/sim/executor/orchestrators/parallel.ts | 2 +- packages/sim-cli/src/generated/v2-api.ts | 18 +++ packages/workflow-code/src/child-results.ts | 43 +------ packages/workflow-code/src/compile/SYNTAX.md | 2 + .../src/compile/catalog.test-helpers.ts | 5 + .../workflow-code/src/compile/checks.test.ts | 97 +++++++++++++++- packages/workflow-code/src/compile/edges.ts | 108 ++++++++++++++++-- packages/workflow-code/src/compile/index.ts | 2 + .../workflow-code/src/diagnostics/codes.ts | 11 ++ 12 files changed, 262 insertions(+), 50 deletions(-) diff --git a/apps/docs/content/docs/workflows/code/diagnostics.mdx b/apps/docs/content/docs/workflows/code/diagnostics.mdx index a6e30ebc5be..fc3242fdbb4 100644 --- a/apps/docs/content/docs/workflows/code/diagnostics.mdx +++ b/apps/docs/content/docs/workflows/code/diagnostics.mdx @@ -66,6 +66,8 @@ Reading and writing `workflow.ts`. | `E_EDGE_CYCLE` | Wiring that closes a cycle among enabled blocks, a self-loop included: no block on it ever runs. Framed at the edge back to a block declared earlier, the one to remove; repeat work with `loop()`/`parallel()`. | | `W_EDGE_CYCLE` | A cycle the stored workflow already holds, kept so it round-trips; none of it runs. An offline check reports it for a cycle among blocks the lock binds, which an apply keeps only if the stored workflow holds every edge of it. | | `W_BRANCH_UNWIRED` | A condition or router branch no edge leaves, so a run that takes it ends there and completes: right for an `else` that should do nothing, so it is never more than a warning; the hint names the wiring. Not reported for a branch the stored workflow already leaves unwired. | +| `W_BODY_UNWIRED` | A loop or parallel with blocks inside but no edge from its start, so a run that reaches it fails; the hint names `p.to()`. | +| `W_GUARDRAILS_UNHANDLED` | A guardrails block no error edge leaves: a failed check leaves only through the error port, so the run ends there with no output (a condition on `.passed` never runs); the hint names `check.onError()`. Not reported for a block the stored workflow already holds without one. | | `E_UNKNOWN_BUILDER` | A builder path the catalog does not have, a bare builder-shaped call (`apii('Name', …)`) nothing binds, or a namespace the SDK lacks or the file did not import; `hint` names the builder a common other name stands for (`http.request` → `api`), then prefers a synonymous verb (`postMessage` → `sendMessage`), then the closest path, then the namespace's builders. | | `E_UNKNOWN_PARAM` | A param the block does not have; the hint has a did-you-mean, or for block meta written there (`retry`, `disabled`, `locked`, `errorPort`) the third argument that takes it. | | `E_FIXED_PARAM` | A param the builder already pins. | diff --git a/apps/docs/openapi-v2-workflows.json b/apps/docs/openapi-v2-workflows.json index 361a1ae1552..01eb0b3162f 100644 --- a/apps/docs/openapi-v2-workflows.json +++ b/apps/docs/openapi-v2-workflows.json @@ -19876,6 +19876,8 @@ "E_EDGE_CYCLE", "W_EDGE_CYCLE", "W_BRANCH_UNWIRED", + "W_BODY_UNWIRED", + "W_GUARDRAILS_UNHANDLED", "E_UNKNOWN_BUILDER", "E_UNKNOWN_PARAM", "E_FIXED_PARAM", @@ -20363,6 +20365,8 @@ "E_EDGE_CYCLE", "W_EDGE_CYCLE", "W_BRANCH_UNWIRED", + "W_BODY_UNWIRED", + "W_GUARDRAILS_UNHANDLED", "E_UNKNOWN_BUILDER", "E_UNKNOWN_PARAM", "E_FIXED_PARAM", @@ -21359,6 +21363,8 @@ "E_EDGE_CYCLE", "W_EDGE_CYCLE", "W_BRANCH_UNWIRED", + "W_BODY_UNWIRED", + "W_GUARDRAILS_UNHANDLED", "E_UNKNOWN_BUILDER", "E_UNKNOWN_PARAM", "E_FIXED_PARAM", @@ -22331,6 +22337,8 @@ "E_EDGE_CYCLE", "W_EDGE_CYCLE", "W_BRANCH_UNWIRED", + "W_BODY_UNWIRED", + "W_GUARDRAILS_UNHANDLED", "E_UNKNOWN_BUILDER", "E_UNKNOWN_PARAM", "E_FIXED_PARAM", @@ -23113,6 +23121,8 @@ "E_EDGE_CYCLE", "W_EDGE_CYCLE", "W_BRANCH_UNWIRED", + "W_BODY_UNWIRED", + "W_GUARDRAILS_UNHANDLED", "E_UNKNOWN_BUILDER", "E_UNKNOWN_PARAM", "E_FIXED_PARAM", @@ -28183,6 +28193,8 @@ "E_EDGE_CYCLE", "W_EDGE_CYCLE", "W_BRANCH_UNWIRED", + "W_BODY_UNWIRED", + "W_GUARDRAILS_UNHANDLED", "E_UNKNOWN_BUILDER", "E_UNKNOWN_PARAM", "E_FIXED_PARAM", @@ -28775,6 +28787,8 @@ "E_EDGE_CYCLE", "W_EDGE_CYCLE", "W_BRANCH_UNWIRED", + "W_BODY_UNWIRED", + "W_GUARDRAILS_UNHANDLED", "E_UNKNOWN_BUILDER", "E_UNKNOWN_PARAM", "E_FIXED_PARAM", @@ -29192,6 +29206,8 @@ "E_EDGE_CYCLE", "W_EDGE_CYCLE", "W_BRANCH_UNWIRED", + "W_BODY_UNWIRED", + "W_GUARDRAILS_UNHANDLED", "E_UNKNOWN_BUILDER", "E_UNKNOWN_PARAM", "E_FIXED_PARAM", @@ -30066,6 +30082,8 @@ "E_EDGE_CYCLE", "W_EDGE_CYCLE", "W_BRANCH_UNWIRED", + "W_BODY_UNWIRED", + "W_GUARDRAILS_UNHANDLED", "E_UNKNOWN_BUILDER", "E_UNKNOWN_PARAM", "E_FIXED_PARAM", @@ -30483,6 +30501,8 @@ "E_EDGE_CYCLE", "W_EDGE_CYCLE", "W_BRANCH_UNWIRED", + "W_BODY_UNWIRED", + "W_GUARDRAILS_UNHANDLED", "E_UNKNOWN_BUILDER", "E_UNKNOWN_PARAM", "E_FIXED_PARAM", diff --git a/apps/sim/executor/orchestrators/loop.ts b/apps/sim/executor/orchestrators/loop.ts index 5450cb09a71..11616b8d711 100644 --- a/apps/sim/executor/orchestrators/loop.ts +++ b/apps/sim/executor/orchestrators/loop.ts @@ -115,7 +115,7 @@ export class LoopOrchestrator { if (loopConfig.nodes.length === 0) { const errorMessage = - 'Loop has no executable blocks inside. Add or enable at least one block in the loop.' + 'Loop has no block wired from its start. Add or enable a block in the loop and connect the loop start to it.' const loopType = loopConfig.loopType || 'for' logger.error(errorMessage, { loopId }) await this.addLoopErrorLog(ctx, loopId, loopType, errorMessage, {}) diff --git a/apps/sim/executor/orchestrators/parallel.ts b/apps/sim/executor/orchestrators/parallel.ts index 7e449ad7d02..6a7647d3716 100644 --- a/apps/sim/executor/orchestrators/parallel.ts +++ b/apps/sim/executor/orchestrators/parallel.ts @@ -59,7 +59,7 @@ export class ParallelOrchestrator { if (parallelConfig.nodes.length === 0) { const errorMessage = - 'Parallel has no executable blocks inside. Add or enable at least one block in the parallel.' + 'Parallel has no block wired from its start. Add or enable a block in the parallel and connect the parallel start to it.' logger.error(errorMessage, { parallelId }) await this.addParallelErrorLog(ctx, parallelId, errorMessage, {}) this.setErrorScope(ctx, parallelId, errorMessage) diff --git a/packages/sim-cli/src/generated/v2-api.ts b/packages/sim-cli/src/generated/v2-api.ts index 5e6465a8637..e3c971a45d7 100644 --- a/packages/sim-cli/src/generated/v2-api.ts +++ b/packages/sim-cli/src/generated/v2-api.ts @@ -877,6 +877,8 @@ export type ApplyWorkflowSourceResponse = { | 'E_EDGE_CYCLE' | 'W_EDGE_CYCLE' | 'W_BRANCH_UNWIRED' + | 'W_BODY_UNWIRED' + | 'W_GUARDRAILS_UNHANDLED' | 'E_UNKNOWN_BUILDER' | 'E_UNKNOWN_PARAM' | 'E_FIXED_PARAM' @@ -1386,6 +1388,8 @@ export type ApplyWorkspaceSourceResponse = { | 'E_EDGE_CYCLE' | 'W_EDGE_CYCLE' | 'W_BRANCH_UNWIRED' + | 'W_BODY_UNWIRED' + | 'W_GUARDRAILS_UNHANDLED' | 'E_UNKNOWN_BUILDER' | 'E_UNKNOWN_PARAM' | 'E_FIXED_PARAM' @@ -1684,6 +1688,8 @@ export type ApplyWorkspaceSourceResponse = { | 'E_EDGE_CYCLE' | 'W_EDGE_CYCLE' | 'W_BRANCH_UNWIRED' + | 'W_BODY_UNWIRED' + | 'W_GUARDRAILS_UNHANDLED' | 'E_UNKNOWN_BUILDER' | 'E_UNKNOWN_PARAM' | 'E_FIXED_PARAM' @@ -5364,6 +5370,8 @@ export type CreateWorkflowFromSourceResponse = { | 'E_EDGE_CYCLE' | 'W_EDGE_CYCLE' | 'W_BRANCH_UNWIRED' + | 'W_BODY_UNWIRED' + | 'W_GUARDRAILS_UNHANDLED' | 'E_UNKNOWN_BUILDER' | 'E_UNKNOWN_PARAM' | 'E_FIXED_PARAM' @@ -10596,6 +10604,8 @@ export type GetWorkflowSourceResponse = { | 'E_EDGE_CYCLE' | 'W_EDGE_CYCLE' | 'W_BRANCH_UNWIRED' + | 'W_BODY_UNWIRED' + | 'W_GUARDRAILS_UNHANDLED' | 'E_UNKNOWN_BUILDER' | 'E_UNKNOWN_PARAM' | 'E_FIXED_PARAM' @@ -11295,6 +11305,8 @@ export type GetWorkspaceSourceResponse = { | 'E_EDGE_CYCLE' | 'W_EDGE_CYCLE' | 'W_BRANCH_UNWIRED' + | 'W_BODY_UNWIRED' + | 'W_GUARDRAILS_UNHANDLED' | 'E_UNKNOWN_BUILDER' | 'E_UNKNOWN_PARAM' | 'E_FIXED_PARAM' @@ -14633,6 +14645,8 @@ export type PlanWorkflowSourceResponse = { | 'E_EDGE_CYCLE' | 'W_EDGE_CYCLE' | 'W_BRANCH_UNWIRED' + | 'W_BODY_UNWIRED' + | 'W_GUARDRAILS_UNHANDLED' | 'E_UNKNOWN_BUILDER' | 'E_UNKNOWN_PARAM' | 'E_FIXED_PARAM' @@ -15096,6 +15110,8 @@ export type PlanWorkspaceSourceResponse = { | 'E_EDGE_CYCLE' | 'W_EDGE_CYCLE' | 'W_BRANCH_UNWIRED' + | 'W_BODY_UNWIRED' + | 'W_GUARDRAILS_UNHANDLED' | 'E_UNKNOWN_BUILDER' | 'E_UNKNOWN_PARAM' | 'E_FIXED_PARAM' @@ -15394,6 +15410,8 @@ export type PlanWorkspaceSourceResponse = { | 'E_EDGE_CYCLE' | 'W_EDGE_CYCLE' | 'W_BRANCH_UNWIRED' + | 'W_BODY_UNWIRED' + | 'W_GUARDRAILS_UNHANDLED' | 'E_UNKNOWN_BUILDER' | 'E_UNKNOWN_PARAM' | 'E_FIXED_PARAM' diff --git a/packages/workflow-code/src/child-results.ts b/packages/workflow-code/src/child-results.ts index 67746c750de..c5b4b02cc7b 100644 --- a/packages/workflow-code/src/child-results.ts +++ b/packages/workflow-code/src/child-results.ts @@ -6,6 +6,7 @@ import type { BlockCatalog } from './catalog/types' import { type CompileInput, type CompileResult, compileWorkflow } from './compile' +import { stronglyConnected } from './compile/edges' import type { ChildResultFields } from './json-fields' import { SOURCE_ENTRY_FILE } from './limits' import type { WorkflowLockV1 } from './lock' @@ -88,7 +89,8 @@ export function resolveChildResults( nodes: ReadonlyMap ): Map { const resolved = new Map() - for (const component of stronglyConnected(nodes)) { + const calls = (key: string) => (nodes.get(key)?.calls ?? []).filter((call) => nodes.has(call)) + for (const component of stronglyConnected([...nodes.keys()], calls)) { const members = new Set(component) for (const key of component) { const node = nodes.get(key) @@ -104,42 +106,3 @@ export function resolveChildResults( } return resolved } - -/** - * The strongly connected components of the call graph `nodes` holds (Tarjan's algorithm), each - * listed after every component it calls into. - */ -function stronglyConnected(nodes: ReadonlyMap): string[][] { - const index = new Map() - const low = new Map() - const stack: string[] = [] - const onStack = new Set() - const components: string[][] = [] - const visit = (key: string) => { - index.set(key, index.size) - low.set(key, index.size - 1) - stack.push(key) - onStack.add(key) - for (const call of nodes.get(key)?.calls ?? []) { - if (!nodes.has(call)) continue - if (!index.has(call)) { - visit(call) - low.set(key, Math.min(low.get(key) ?? 0, low.get(call) ?? 0)) - } else if (onStack.has(call)) { - low.set(key, Math.min(low.get(key) ?? 0, index.get(call) ?? 0)) - } - } - if (low.get(key) !== index.get(key)) return - const component: string[] = [] - let member: string | undefined - do { - member = stack.pop() - if (member === undefined) break - onStack.delete(member) - component.push(member) - } while (member !== key) - components.push(component) - } - for (const key of nodes.keys()) if (!index.has(key)) visit(key) - return components -} diff --git a/packages/workflow-code/src/compile/SYNTAX.md b/packages/workflow-code/src/compile/SYNTAX.md index 24343120274..7adb0445c8d 100644 --- a/packages/workflow-code/src/compile/SYNTAX.md +++ b/packages/workflow-code/src/compile/SYNTAX.md @@ -446,6 +446,8 @@ Errors refuse the whole apply. Warnings are saved as written; info is informatio | `E_EDGE_CYCLE` | Wiring that closes a cycle among enabled blocks, a self-loop included: no block on it ever runs. Framed at the edge back to a block declared earlier, the one to remove; repeat work with `loop()`/`parallel()`. | | `W_EDGE_CYCLE` | A cycle the stored workflow already holds, kept so it round-trips; none of it runs. An offline check reports it for a cycle among blocks the lock binds, which an apply keeps only if the stored workflow holds every edge of it. | | `W_BRANCH_UNWIRED` | A condition or router branch no edge leaves, so a run that takes it ends there and completes: right for an `else` that should do nothing, so it is never more than a warning; the hint names the wiring. Not reported for a branch the stored workflow already leaves unwired. | +| `W_BODY_UNWIRED` | A loop or parallel with blocks inside but no edge from its start, so a run that reaches it fails; the hint names `p.to()`. | +| `W_GUARDRAILS_UNHANDLED` | A guardrails block no error edge leaves: a failed check leaves only through the error port, so the run ends there with no output (a condition on `.passed` never runs); the hint names `check.onError()`. Not reported for a block the stored workflow already holds without one. | | `E_UNKNOWN_BUILDER` | A builder path the catalog does not have, a bare builder-shaped call (`apii('Name', …)`) nothing binds, or a namespace the SDK lacks or the file did not import; `hint` names the builder a common other name stands for (`http.request` → `api`), then prefers a synonymous verb (`postMessage` → `sendMessage`), then the closest path, then the namespace's builders. | | `E_UNKNOWN_PARAM` | A param the block does not have; the hint has a did-you-mean, or for block meta written there (`retry`, `disabled`, `locked`, `errorPort`) the third argument that takes it. | | `E_FIXED_PARAM` | A param the builder already pins. | diff --git a/packages/workflow-code/src/compile/catalog.test-helpers.ts b/packages/workflow-code/src/compile/catalog.test-helpers.ts index 3fb77e1cd79..49e7a9b5dbc 100644 --- a/packages/workflow-code/src/compile/catalog.test-helpers.ts +++ b/packages/workflow-code/src/compile/catalog.test-helpers.ts @@ -77,6 +77,7 @@ export const TEST_CATALOG: BlockCatalog = { condition: { type: 'condition' }, router: { type: 'router_v2' }, fn: { type: 'function' }, + guardrails: { type: 'guardrails' }, note: { type: 'note' }, 'slack.sendMessage': { type: 'slack', operation: 'send' }, 'slack.listChannels': { type: 'slack', operation: 'list_channels' }, @@ -164,6 +165,10 @@ export const TEST_CATALOG: BlockCatalog = { { result: MAY_BE_TEXT, stdout: TEXT }, { errorOutputs: ['errorMessage'] } ), + guardrails: block('guardrails', [sub('input', 'long-input', 'string', { required: true })], { + passed: { type: 'boolean' }, + error: TEXT, + }), note: block( 'note', [sub('content', 'long-input', 'string')], diff --git a/packages/workflow-code/src/compile/checks.test.ts b/packages/workflow-code/src/compile/checks.test.ts index afc0dffee7b..b1e6b5ddfc8 100644 --- a/packages/workflow-code/src/compile/checks.test.ts +++ b/packages/workflow-code/src/compile/checks.test.ts @@ -11,7 +11,7 @@ import type { CompileResult } from './index' import { compileWorkflow } from './index' const IMPORTS = - "import { agent, api, block, condition, fn, loop, slack, t, trigger, workflow } from '@sim/workflow-sdk'" + "import { agent, api, block, condition, fn, guardrails, loop, parallel, slack, t, trigger, workflow } from '@sim/workflow-sdk'" function source(body: string): string { return `// @sim-sdk 1\n${IMPORTS}\n\nexport default workflow('Test', {}, () => {\n${body}\n})\n` @@ -519,6 +519,101 @@ describe('unwired branches', () => { }) }) +describe('unwired container bodies', () => { + const ORPHAN = [ + START, + "const fan = parallel.collection('Fan', { collection: [1, 2] }, (p) => {", + " const orphan = api('Orphan', { url: 'https://x' })", + '})', + 'start.to(fan)', + ] + + it('warns on a body no edge starts, naming the block to start it with', () => { + const [unwired, ...rest] = findings(run(ORPHAN.join('\n')), 'W_BODY_UNWIRED') + expect(rest).toEqual([]) + expect(unwired?.message).toBe( + 'Fan has no block wired from its start, so a run fails when it reaches the parallel.' + ) + expect(unwired?.hint).toBe('Start the body with p.to(orphan).') + }) + + it('stays quiet when the start is wired, the body is an arrow expression, or it is empty', () => { + const wired = [...ORPHAN.slice(0, 3), ' p.to(orphan)', ...ORPHAN.slice(3)] + const implied = [ + START, + "loop.forEach('L', { collection: [1] }, (p) => api('In', { url: 'x' }))", + ] + const empty = [START, "const each = loop.forEach('L', { collection: [1] }, (p) => {})"] + for (const body of [wired, implied, empty]) { + expect(findings(run(body.join('\n')), 'W_BODY_UNWIRED')).toEqual([]) + } + }) +}) + +describe('unhandled guardrails', () => { + const CHECK = [ + START, + "const check = guardrails('Check', { input: 'x' })", + "const ok = api('Ok', { url: 'https://x' })", + 'start.to(check)', + 'check.to(ok)', + ] + + it('warns on a guardrails block with no error edge, naming onError', () => { + const [unhandled, ...rest] = findings(run(CHECK.join('\n')), 'W_GUARDRAILS_UNHANDLED') + expect(rest).toEqual([]) + expect(unhandled?.message).toBe( + 'Check has no error edge, so a failed check ends the run there with no output.' + ) + expect(unhandled?.hint).toBe( + 'Handle a failed check with check.onError(); the handler reads why as check.error.' + ) + }) + + it('stays quiet when an error edge leaves it', () => { + const body = [...CHECK, "const fail = api('Fail', { url: 'https://x' })", 'check.onError(fail)'] + expect(findings(run(body.join('\n')), 'W_GUARDRAILS_UNHANDLED')).toEqual([]) + }) + + it('stays quiet for a block the stored workflow already holds without one', () => { + const stored = run(CHECK.join('\n')).workflow + if (!stored) throw new Error('expected a workflow') + expect(findings(run(CHECK.join('\n'), stored), 'W_GUARDRAILS_UNHANDLED')).toEqual([]) + }) +}) + +describe('nested member wiring sources', () => { + it('points a branch read through a member at the branch on the block', () => { + const result = run( + [ + START, + "const known = condition('Known', { yes: 'x > 1', else: true })", + "const a = api('A', { url: 'https://x' })", + 'start.to(known)', + 'known.route.yes.to(a)', + ].join('\n') + ) + const [unsupported] = findings(result, 'E_UNSUPPORTED_SYNTAX') + expect(unsupported?.hint).toBe( + 'Wire a branch as known..to(...), e.g. known.yes.to(...).' + ) + }) + + it('points a member of a block without branches at the block itself', () => { + const result = run( + [ + START, + "const a = api('A', { url: 'https://x' })", + "const b = api('B', { url: 'https://x' })", + 'start.to(a)', + 'a.data.id.to(b)', + ].join('\n') + ) + const [unsupported] = findings(result, 'E_UNSUPPORTED_SYNTAX') + expect(unsupported?.hint).toBe('Wire the block itself: a.to(...).') + }) +}) + describe('unknown builders', () => { it.each([ ["const s = slack.postMessage('S', {})", 'Did you mean slack.sendMessage?'], diff --git a/packages/workflow-code/src/compile/edges.ts b/packages/workflow-code/src/compile/edges.ts index 2a2b816520f..dbfaf4853b4 100644 --- a/packages/workflow-code/src/compile/edges.ts +++ b/packages/workflow-code/src/compile/edges.ts @@ -48,6 +48,9 @@ function portForHandle(handle: string, from: BlockDecl): Port { return parsed.kind === 'branch' ? { kind: 'raw', handle } : parsed } +/** The block type whose failed check leaves only through its error port. */ +const GUARDRAILS_BLOCK_TYPE = 'guardrails' + /** Implicit branch keys of a never-persisted branch list. */ const IMPLICIT_BRANCHES = { condition: ['if', 'else'], router: ['route0'] } as const @@ -321,6 +324,77 @@ export class EdgeCollector { } } + /** + * Reports each enabled loop or parallel with enabled blocks inside but no edge from its start: + * the executor runs only the body blocks its start reaches, so a run that reaches it fails. + */ + reportUnwiredBodies(): void { + const started = new Set( + this.edges.flatMap((edge) => (edge.port.kind === 'subflowStart' ? [edge.from] : [])) + ) + const entered = new Set(this.edges.map((edge) => edge.to)) + for (const decl of this.ctx.decls) { + const kind = containerKindOf(decl) + if (!kind || started.has(decl.key) || !this.runs(decl)) continue + const body = this.ctx.decls.filter((child) => child.parent === decl && this.runs(child)) + const first = body.find((child) => !entered.has(child.key)) ?? body[0] + if (!first) continue + const callback = decl.call.arguments[2] + const param = + callback?.type === 'ArrowFunctionExpression' && callback.params[0]?.type === 'Identifier' + ? callback.params[0].name + : 'p' + this.ctx.reporter.report( + 'W_BODY_UNWIRED', + `${decl.name} has no block wired from its start, so a run fails when it reaches the ${kind}.`, + rangeOf(this.ctx, decl.anchorNode), + { + hint: `Start the body with ${param}.to(${first.ident ?? ''}).`, + blockKey: decl.key, + } + ) + } + } + + /** + * Reports each enabled guardrails block no error edge leaves: a failed check leaves through the + * error port, so without one the run ends there with no output (a Condition on `.passed` never + * runs). A block the stored workflow already holds without an error edge stays quiet. + */ + reportUnhandledGuardrails(stored: CanonicalWorkflow | undefined): void { + const handled = (edges: readonly CanonicalEdge[]) => + new Set(edges.flatMap((edge) => (edge.port.kind === 'error' ? [edge.from] : []))) + const wired = handled(this.edges) + const storedWired = handled(stored?.edges ?? []) + for (const [key, block] of this.blocks) { + const decl = this.ctx.declsByKey.get(key) + if (block.type !== GUARDRAILS_BLOCK_TYPE || !decl || !block.enabled || wired.has(key)) + continue + if (stored?.blocks[key]?.type === GUARDRAILS_BLOCK_TYPE && !storedWired.has(key)) continue + const ident = decl.ident ?? 'check' + this.ctx.reporter.report( + 'W_GUARDRAILS_UNHANDLED', + `${decl.name} has no error edge, so a failed check ends the run there with no output.`, + rangeOf(this.ctx, decl.anchorNode), + { + hint: `Handle a failed check with ${ident}.onError(); the handler reads why as ${ident}.error.`, + blockKey: key, + } + ) + } + } + + /** Whether a declared block is in the run graph: compiled, enabled and not a note. */ + private runs(decl: BlockDecl): boolean { + const block = this.blocks.get(decl.key) + return ( + !decl.duplicate && + block !== undefined && + block.enabled && + catalogBlockOf(decl)?.graph.annotationOnly !== true + ) + } + /** * Whether an edge may be one the stored workflow holds although this compile cannot read it * (`CompileInput.storedUnread`): both ends are blocks its lock binds, and with @@ -360,19 +434,22 @@ export class EdgeCollector { return binding.kind === 'block' ? binding.decl : null } + /** The branch keys of a condition or router, or `null` for a block without branches. */ + private branchKeys(decl: BlockDecl): readonly string[] | null { + const branches = this.blocks.get(decl.key)?.branches + if (!branches) return null + return branches.rows ? branches.rows.map((row) => row.key) : IMPLICIT_BRANCHES[branches.kind] + } + private branchPort(decl: BlockDecl, key: string, node: Node): Port | null { - const block = this.blocks.get(decl.key) - const branches = block?.branches - if (!block || !branches) { + const keys = this.branchKeys(decl) + if (!keys) { this.error('E_UNKNOWN_BRANCH', `${decl.name} has no branches.`, node, { hint: 'Only condition and router blocks have branches.', blockKey: decl.key, }) return null } - const keys: readonly string[] = branches.rows - ? branches.rows.map((row) => row.key) - : IMPLICIT_BRANCHES[branches.kind] if (!keys.includes(key)) { // The branch was refused where it was declared; that error already covers this wiring. if (this.ctx.refusedBranches.get(decl.key)?.has(key)) return null @@ -425,11 +502,28 @@ export class EdgeCollector { return result } this.error('E_UNSUPPORTED_SYNTAX', `${nodeLabel(node)} is not a wiring source.`, node, { - hint: hintForNode(node), + hint: this.nestedMemberHint(scope, node) ?? hintForNode(node), }) return null } + /** + * The hint for `x.a.b.to(...)` on a block binding `x`: a branch is read off the block itself + * (`route.yes.to(...)`), never through an output or another member. + */ + private nestedMemberHint(scope: Scope, node: Expression): string | null { + if (node.type !== 'MemberExpression' || node.property.type !== 'Identifier') return null + const object = node.object + if (object.type !== 'MemberExpression' || object.object.type !== 'Identifier') return null + const binding = lookup(scope, object.object.name) + if (binding?.kind !== 'block') return null + const ident = object.object.name + const keys = this.branchKeys(binding.decl) + if (!keys) return `Wire the block itself: ${ident}.to(...).` + const example = keys.includes(node.property.name) ? node.property.name : keys[0] + return `Wire a branch as ${ident}..to(...), e.g. ${ident}.${example}.to(...).` + } + private targets( scope: Scope, call: CallExpression, diff --git a/packages/workflow-code/src/compile/index.ts b/packages/workflow-code/src/compile/index.ts index f5484c60e1e..61ddc723a67 100644 --- a/packages/workflow-code/src/compile/index.ts +++ b/packages/workflow-code/src/compile/index.ts @@ -879,6 +879,8 @@ function compileEntry( for (const implied of ctx.implied) edges.implied(implied.container, implied.child, implied.body) edges.reportCycles(input.stored) edges.reportUnwiredBranches(input.stored) + edges.reportUnwiredBodies() + edges.reportUnhandledGuardrails(input.stored) const erroring = new Set() for (const edge of edges.edges) { const source = compiled.get(edge.from) diff --git a/packages/workflow-code/src/diagnostics/codes.ts b/packages/workflow-code/src/diagnostics/codes.ts index 2540cdc1f3e..4d7aefa0c6b 100644 --- a/packages/workflow-code/src/diagnostics/codes.ts +++ b/packages/workflow-code/src/diagnostics/codes.ts @@ -187,6 +187,17 @@ const COMPILE_EDGE_CODES = [ * stored workflow already leaves unwired is not reported. */ 'W_BRANCH_UNWIRED', + /** + * A loop or parallel with blocks inside but no edge from its start (`p.to(...)`): the executor + * runs only the body blocks its start reaches, so a run that reaches the container fails. + */ + 'W_BODY_UNWIRED', + /** + * A guardrails block no error edge leaves: a failed check leaves only through the error port, so + * the run ends there with no output. A block the stored workflow already holds without one is + * not reported. + */ + 'W_GUARDRAILS_UNHANDLED', ] as const /** Checks against the block catalog. */ From 60b5e2ed87ce205e4cbb49137cf698e0ff4162f8 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Tue, 1 Dec 2026 00:17:42 -0800 Subject: [PATCH 2/2] fix(workflow-code): count only enabled targets and honor the offline lock in wiring warnings --- .../workflow-code/src/compile/checks.test.ts | 44 ++++++++++++- packages/workflow-code/src/compile/edges.ts | 64 ++++++++++++++----- 2 files changed, 91 insertions(+), 17 deletions(-) diff --git a/packages/workflow-code/src/compile/checks.test.ts b/packages/workflow-code/src/compile/checks.test.ts index b1e6b5ddfc8..cfd4c094a1f 100644 --- a/packages/workflow-code/src/compile/checks.test.ts +++ b/packages/workflow-code/src/compile/checks.test.ts @@ -7,7 +7,7 @@ import type { BlockCatalog, CatalogBlock } from '../catalog/types' import { FINDING_LIMITS } from '../diagnostics/bound' import type { CanonicalWorkflow } from '../model' import { TEST_CATALOG } from './catalog.test-helpers' -import type { CompileResult } from './index' +import type { CompileResult, StoredEdgeRef } from './index' import { compileWorkflow } from './index' const IMPORTS = @@ -548,6 +548,16 @@ describe('unwired container bodies', () => { expect(findings(run(body.join('\n')), 'W_BODY_UNWIRED')).toEqual([]) } }) + + it('warns when the start reaches only a disabled block', () => { + const body = [ + ...ORPHAN.slice(0, 3), + " const off = api('Off', { url: 'https://x' }, { disabled: true })", + ' p.to(off)', + ...ORPHAN.slice(3), + ] + expect(findings(run(body.join('\n')), 'W_BODY_UNWIRED')).toHaveLength(1) + }) }) describe('unhandled guardrails', () => { @@ -575,11 +585,43 @@ describe('unhandled guardrails', () => { expect(findings(run(body.join('\n')), 'W_GUARDRAILS_UNHANDLED')).toEqual([]) }) + it('warns when the only error edge reaches a disabled block', () => { + const body = [ + ...CHECK, + "const off = api('Off', { url: 'https://x' }, { disabled: true })", + 'check.onError(off)', + ] + expect(findings(run(body.join('\n')), 'W_GUARDRAILS_UNHANDLED')).toHaveLength(1) + }) + it('stays quiet for a block the stored workflow already holds without one', () => { const stored = run(CHECK.join('\n')).workflow if (!stored) throw new Error('expected a workflow') expect(findings(run(CHECK.join('\n'), stored), 'W_GUARDRAILS_UNHANDLED')).toEqual([]) }) + + it('offline, stays quiet for a bound block the get printed without one', () => { + const offline = (storedUnreadEdges: StoredEdgeRef[]) => + compileWorkflow({ + entry: { path: 'workflow.ts', text: source(CHECK.join('\n')) }, + files: new Map(), + catalog: TEST_CATALOG, + sdkMajor: 1, + storedUnread: { + version: 1, + blocks: { + start: { id: 'b-1', parent: null, ident: 'start', order: 0 }, + check: { id: 'b-2', parent: null, ident: 'check', order: 1 }, + ok: { id: 'b-3', parent: null, ident: 'ok', order: 2 }, + }, + variables: {}, + }, + storedUnreadEdges, + }) + expect(findings(offline([]), 'W_GUARDRAILS_UNHANDLED')).toEqual([]) + const removed = offline([{ from: 'check', port: 'error', to: 'ok' }]) + expect(findings(removed, 'W_GUARDRAILS_UNHANDLED')).toHaveLength(1) + }) }) describe('nested member wiring sources', () => { diff --git a/packages/workflow-code/src/compile/edges.ts b/packages/workflow-code/src/compile/edges.ts index dbfaf4853b4..9988523fb4f 100644 --- a/packages/workflow-code/src/compile/edges.ts +++ b/packages/workflow-code/src/compile/edges.ts @@ -229,10 +229,9 @@ export class EdgeCollector { * error. */ reportCycles(stored: CanonicalWorkflow | undefined): void { - const enabled = (key: string) => this.blocks.get(key)?.enabled !== false const outgoing = new Map() this.edges.forEach((edge, index) => { - if (!enabled(edge.from) || !enabled(edge.to)) return + if (!this.enabled(edge.from) || !this.enabled(edge.to)) return const list = outgoing.get(edge.from) if (list) list.push(index) else outgoing.set(edge.from, [index]) @@ -325,18 +324,27 @@ export class EdgeCollector { } /** - * Reports each enabled loop or parallel with enabled blocks inside but no edge from its start: - * the executor runs only the body blocks its start reaches, so a run that reaches it fails. + * Reports each enabled loop or parallel with enabled blocks inside but no edge from its start to + * an enabled one: the executor runs only the body blocks its start reaches, so a run that + * reaches it fails. */ reportUnwiredBodies(): void { const started = new Set( - this.edges.flatMap((edge) => (edge.port.kind === 'subflowStart' ? [edge.from] : [])) + this.edges.flatMap((edge) => + edge.port.kind === 'subflowStart' && this.enabled(edge.to) ? [edge.from] : [] + ) ) const entered = new Set(this.edges.map((edge) => edge.to)) - for (const decl of this.ctx.decls) { + const bodies = new Map() + for (const child of this.ctx.decls) { + if (!child.parent || !this.runs(child)) continue + const body = bodies.get(child.parent) + if (body) body.push(child) + else bodies.set(child.parent, [child]) + } + for (const [decl, body] of bodies) { const kind = containerKindOf(decl) if (!kind || started.has(decl.key) || !this.runs(decl)) continue - const body = this.ctx.decls.filter((child) => child.parent === decl && this.runs(child)) const first = body.find((child) => !entered.has(child.key)) ?? body[0] if (!first) continue const callback = decl.call.arguments[2] @@ -357,20 +365,22 @@ export class EdgeCollector { } /** - * Reports each enabled guardrails block no error edge leaves: a failed check leaves through the - * error port, so without one the run ends there with no output (a Condition on `.passed` never - * runs). A block the stored workflow already holds without an error edge stays quiet. + * Reports each enabled guardrails block no error edge to an enabled block leaves: a failed check + * leaves through the error port, so without a handler the run ends there with no output (a + * Condition on `.passed` never runs). A block the stored workflow already holds that way stays + * quiet. */ reportUnhandledGuardrails(stored: CanonicalWorkflow | undefined): void { - const handled = (edges: readonly CanonicalEdge[]) => - new Set(edges.flatMap((edge) => (edge.port.kind === 'error' ? [edge.from] : []))) - const wired = handled(this.edges) - const storedWired = handled(stored?.edges ?? []) + const handled = new Set( + this.edges.flatMap((edge) => + edge.port.kind === 'error' && this.enabled(edge.to) ? [edge.from] : [] + ) + ) for (const [key, block] of this.blocks) { const decl = this.ctx.declsByKey.get(key) - if (block.type !== GUARDRAILS_BLOCK_TYPE || !decl || !block.enabled || wired.has(key)) + if (block.type !== GUARDRAILS_BLOCK_TYPE || !decl || !block.enabled || handled.has(key)) continue - if (stored?.blocks[key]?.type === GUARDRAILS_BLOCK_TYPE && !storedWired.has(key)) continue + if (this.storedUnhandledGuardrails(key, stored)) continue const ident = decl.ident ?? 'check' this.ctx.reporter.report( 'W_GUARDRAILS_UNHANDLED', @@ -384,6 +394,28 @@ export class EdgeCollector { } } + /** + * Whether the stored workflow holds `key` as a guardrails block no error edge leaves. Unread + * (`CompileInput.storedUnread`): a block its lock binds, from which the get printed no error edge + * (any bound block without `CompileInput.storedUnreadEdges`). + */ + private storedUnhandledGuardrails(key: BlockKey, stored: CanonicalWorkflow | undefined): boolean { + if (stored) { + return ( + stored.blocks[key]?.type === GUARDRAILS_BLOCK_TYPE && + !stored.edges.some((edge) => edge.from === key && edge.port.kind === 'error') + ) + } + const lockKey = this.unreadBound?.lockKeys.get(key) + if (lockKey === undefined) return false + const errorEdge = edgeTupleKey(lockKey, portKey({ kind: 'error' }), '') + return ![...(this.unreadBound?.edges ?? [])].some((tuple) => tuple.startsWith(errorEdge)) + } + + private enabled(key: BlockKey): boolean { + return this.blocks.get(key)?.enabled !== false + } + /** Whether a declared block is in the run graph: compiled, enabled and not a note. */ private runs(decl: BlockDecl): boolean { const block = this.blocks.get(decl.key)