diff --git a/apps/docs/content/docs/cli/reference.mdx b/apps/docs/content/docs/cli/reference.mdx index 8957d0e13fe..c2b42c6fa1b 100644 --- a/apps/docs/content/docs/cli/reference.mdx +++ b/apps/docs/content/docs/cli/reference.mdx @@ -7746,7 +7746,7 @@ sim workflows operations apply [options] | --- | --- | --- | | `--dry-run` | No | Validate and lint without writing, auditing, or notifying collaborators. Returns the same validation, preparation warnings, lint findings, and ID-ownership conflicts (`409`) as a committed write. `needsRedeployment` describes the pre-write state. For semantic operations, `mintedBlockIds` is empty; `previewBlockIds` contains provisional IDs with a warning, since committing mints new IDs. | | `--no-dry-run` | No | Send --dry-run as false. | -| `--operations ` | Yes | Edits to apply, in a single batch, keyed by operation_type: [{"operation_type":"add","block_id":"my-fn","params":{"type":"function","name":"My Fn","inputs":{"code":"return {ok:true}"}}},{"operation_type":"edit","block_id":"<uuid>","params":{"name":"Renamed","connections":{"success":"my-fn"}}},{"operation_type":"delete","block_id":"<uuid>"}]. Also extract_from_subflow, whose params carry {"subflowId":"<loop-id>"}, and insert_into_subflow, which creates a block and so takes an add’s params plus that subflowId (JSON, or @path / @- to read a file or stdin). | +| `--operations ` | Yes | Edits to apply, in a single batch, keyed by operation_type: [{"operation_type":"add","block_id":"my-fn","params":{"type":"function","name":"My Fn","inputs":{"code":"return {ok:true}"}}},{"operation_type":"edit","block_id":"<uuid>","params":{"name":"Renamed","connections":{"success":"my-fn"}}},{"operation_type":"delete","block_id":"<uuid>"}]. Also extract_from_subflow, whose params carry {"subflowId":"<loop-id>"}, and insert_into_subflow, which creates a block and so takes an add’s params plus that subflowId. To move an existing block into a loop or parallel (keeping its id), edit it with params {"parentId":"<loop-id>"}; {"parentId":null} moves it out (JSON, or @path / @- to read a file or stdin). | | `--atomic` | No | Fail the whole batch when any operation is declined or any block input would be dropped. The default applies what it can and reports the rest in `skipped` and `inputValidationErrors`; `true` writes nothing and answers `409` instead. | | `--no-atomic` | No | Send --atomic as false. | | `--layout ` | No | Whether to reposition blocks the batch touched. `targeted` (default) nudges only the affected subgraph; `none` leaves every position exactly as supplied. Accepted values: `targeted`, `none`. | @@ -8368,6 +8368,7 @@ sim workflows deploy [options] | --- | --- | --- | | `--name ` | No | Optional label for the deployment version. | | `--description ` | No | Optional release note for the deployment version. | +| `-y, --yes` | No | Accepted and ignored: this command needs no confirmation. | @@ -8639,7 +8640,7 @@ Compiles workflow.ts (and the function files it names) with the compiler the ser Exit codes: 0 no errors; 1 errors (each printed as file:line:column with its frame and hint), or no cached catalog (run the get once). With --output json, stdout is one document that also lists every finding under diagnostics. -These warnings, which an apply saves as written, fail the check as errors on every block the lock does not bind (a block added since the get, or every block of a new workflow): W_REQUIRED_MISSING, W_UNKNOWN_OPTION, W_UNKNOWN_OUTPUT_PATH, W_VALUE_KIND, W_OUT_OF_RANGE, W_LITERAL_SECRET, W_TABLE_UNKNOWN_COLUMN, W_CHILD_INPUT_UNKNOWN, W_BRANCH_UNWIRED, W_UNRESOLVED_REF. --strict fails on them in every block, except W_BRANCH_UNWIRED on a block the lock binds (the stored workflow may hold it, and the server then stays quiet); --no-strict never fails on a warning. W_JSON_REF_UNTYPED is reported only on blocks the lock does not bind, and with --strict on every block. +These warnings, which an apply saves as written, fail the check as errors on every block the lock does not bind (a block added since the get, or every block of a new workflow): W_REQUIRED_MISSING, W_UNKNOWN_OPTION, W_UNKNOWN_OUTPUT_PATH, W_VALUE_KIND, W_OUT_OF_RANGE, W_LITERAL_SECRET, W_TABLE_UNKNOWN_COLUMN, W_CHILD_INPUT_UNKNOWN, W_BRANCH_UNWIRED, W_UNRESOLVED_REF. --strict fails on them in every block, except W_BRANCH_UNWIRED on a block the lock binds (the stored workflow may hold it, and the server then stays quiet); --no-strict fails on none of them. In a function file changed since the get, W_UNRESOLVED_REF is the error E_APPLY_UNRESOLVED_REF in every mode, as the server refuses it. W_JSON_REF_UNTYPED is reported only on blocks the lock does not bind, and with --strict on every block. ### sim workflows source status @@ -10017,7 +10018,7 @@ Compiles every workflows/<key>/workflow.ts (and the function files each na Exit codes: 0 no errors; 1 errors (each printed as file:line:column with its frame and hint), or no cached catalog (run the get once). With --output json, stdout is one document that also lists every finding under diagnostics. -These warnings, which an apply saves as written, fail the check as errors on every block the lock does not bind (a block added since the get, or every block of a new workflow): W_REQUIRED_MISSING, W_UNKNOWN_OPTION, W_UNKNOWN_OUTPUT_PATH, W_VALUE_KIND, W_OUT_OF_RANGE, W_LITERAL_SECRET, W_TABLE_UNKNOWN_COLUMN, W_CHILD_INPUT_UNKNOWN, W_BRANCH_UNWIRED, W_UNRESOLVED_REF. --strict fails on them in every block, except W_BRANCH_UNWIRED on a block the lock binds (the stored workflow may hold it, and the server then stays quiet); --no-strict never fails on a warning. W_JSON_REF_UNTYPED is reported only on blocks the lock does not bind, and with --strict on every block. +These warnings, which an apply saves as written, fail the check as errors on every block the lock does not bind (a block added since the get, or every block of a new workflow): W_REQUIRED_MISSING, W_UNKNOWN_OPTION, W_UNKNOWN_OUTPUT_PATH, W_VALUE_KIND, W_OUT_OF_RANGE, W_LITERAL_SECRET, W_TABLE_UNKNOWN_COLUMN, W_CHILD_INPUT_UNKNOWN, W_BRANCH_UNWIRED, W_UNRESOLVED_REF. --strict fails on them in every block, except W_BRANCH_UNWIRED on a block the lock binds (the stored workflow may hold it, and the server then stays quiet); --no-strict fails on none of them. In a function file changed since the get, W_UNRESOLVED_REF is the error E_APPLY_UNRESOLVED_REF in every mode, as the server refuses it. W_JSON_REF_UNTYPED is reported only on blocks the lock does not bind, and with --strict on every block. Exit 10 (busy, retry): another sim workspaces source command is using this directory (SOURCE_DIR_BUSY); nothing was read or written. Run this one again once it finished. diff --git a/apps/docs/content/docs/cli/workflows.mdx b/apps/docs/content/docs/cli/workflows.mdx index f90bb57ad7e..3a3a705f905 100644 --- a/apps/docs/content/docs/cli/workflows.mdx +++ b/apps/docs/content/docs/cli/workflows.mdx @@ -326,7 +326,7 @@ sim workflows operations apply [options] | --- | --- | --- | | `--dry-run` | No | Validate and lint without writing, auditing, or notifying collaborators. Returns the same validation, preparation warnings, lint findings, and ID-ownership conflicts (`409`) as a committed write. `needsRedeployment` describes the pre-write state. For semantic operations, `mintedBlockIds` is empty; `previewBlockIds` contains provisional IDs with a warning, since committing mints new IDs. | | `--no-dry-run` | No | Send --dry-run as false. | -| `--operations ` | Yes | Edits to apply, in a single batch, keyed by operation_type: [{"operation_type":"add","block_id":"my-fn","params":{"type":"function","name":"My Fn","inputs":{"code":"return {ok:true}"}}},{"operation_type":"edit","block_id":"<uuid>","params":{"name":"Renamed","connections":{"success":"my-fn"}}},{"operation_type":"delete","block_id":"<uuid>"}]. Also extract_from_subflow, whose params carry {"subflowId":"<loop-id>"}, and insert_into_subflow, which creates a block and so takes an add’s params plus that subflowId (JSON, or @path / @- to read a file or stdin). | +| `--operations ` | Yes | Edits to apply, in a single batch, keyed by operation_type: [{"operation_type":"add","block_id":"my-fn","params":{"type":"function","name":"My Fn","inputs":{"code":"return {ok:true}"}}},{"operation_type":"edit","block_id":"<uuid>","params":{"name":"Renamed","connections":{"success":"my-fn"}}},{"operation_type":"delete","block_id":"<uuid>"}]. Also extract_from_subflow, whose params carry {"subflowId":"<loop-id>"}, and insert_into_subflow, which creates a block and so takes an add’s params plus that subflowId. To move an existing block into a loop or parallel (keeping its id), edit it with params {"parentId":"<loop-id>"}; {"parentId":null} moves it out (JSON, or @path / @- to read a file or stdin). | | `--atomic` | No | Fail the whole batch when any operation is declined or any block input would be dropped. The default applies what it can and reports the rest in `skipped` and `inputValidationErrors`; `true` writes nothing and answers `409` instead. | | `--no-atomic` | No | Send --atomic as false. | | `--layout ` | No | Whether to reposition blocks the batch touched. `targeted` (default) nudges only the affected subgraph; `none` leaves every position exactly as supplied. Accepted values: `targeted`, `none`. | @@ -396,6 +396,7 @@ sim workflows deploy [options] | --- | --- | --- | | `--name ` | No | Optional label for the deployment version. | | `--description ` | No | Optional release note for the deployment version. | +| `-y, --yes` | No | Accepted and ignored: this command needs no confirmation. | @@ -1443,7 +1444,7 @@ Compiles workflow.ts (and the function files it names) with the compiler the ser Exit codes: 0 no errors; 1 errors (each printed as file:line:column with its frame and hint), or no cached catalog (run the get once). With --output json, stdout is one document that also lists every finding under diagnostics. -These warnings, which an apply saves as written, fail the check as errors on every block the lock does not bind (a block added since the get, or every block of a new workflow): W_REQUIRED_MISSING, W_UNKNOWN_OPTION, W_UNKNOWN_OUTPUT_PATH, W_VALUE_KIND, W_OUT_OF_RANGE, W_LITERAL_SECRET, W_TABLE_UNKNOWN_COLUMN, W_CHILD_INPUT_UNKNOWN, W_BRANCH_UNWIRED, W_UNRESOLVED_REF. --strict fails on them in every block, except W_BRANCH_UNWIRED on a block the lock binds (the stored workflow may hold it, and the server then stays quiet); --no-strict never fails on a warning. W_JSON_REF_UNTYPED is reported only on blocks the lock does not bind, and with --strict on every block. +These warnings, which an apply saves as written, fail the check as errors on every block the lock does not bind (a block added since the get, or every block of a new workflow): W_REQUIRED_MISSING, W_UNKNOWN_OPTION, W_UNKNOWN_OUTPUT_PATH, W_VALUE_KIND, W_OUT_OF_RANGE, W_LITERAL_SECRET, W_TABLE_UNKNOWN_COLUMN, W_CHILD_INPUT_UNKNOWN, W_BRANCH_UNWIRED, W_UNRESOLVED_REF. --strict fails on them in every block, except W_BRANCH_UNWIRED on a block the lock binds (the stored workflow may hold it, and the server then stays quiet); --no-strict fails on none of them. In a function file changed since the get, W_UNRESOLVED_REF is the error E_APPLY_UNRESOLVED_REF in every mode, as the server refuses it. W_JSON_REF_UNTYPED is reported only on blocks the lock does not bind, and with --strict on every block. ### Show the content of the changes in a workflow source directory diff --git a/apps/docs/content/docs/cli/workspaces.mdx b/apps/docs/content/docs/cli/workspaces.mdx index 7143068365b..4be791a4c0b 100644 --- a/apps/docs/content/docs/cli/workspaces.mdx +++ b/apps/docs/content/docs/cli/workspaces.mdx @@ -788,7 +788,7 @@ Compiles every workflows/<key>/workflow.ts (and the function files each na Exit codes: 0 no errors; 1 errors (each printed as file:line:column with its frame and hint), or no cached catalog (run the get once). With --output json, stdout is one document that also lists every finding under diagnostics. -These warnings, which an apply saves as written, fail the check as errors on every block the lock does not bind (a block added since the get, or every block of a new workflow): W_REQUIRED_MISSING, W_UNKNOWN_OPTION, W_UNKNOWN_OUTPUT_PATH, W_VALUE_KIND, W_OUT_OF_RANGE, W_LITERAL_SECRET, W_TABLE_UNKNOWN_COLUMN, W_CHILD_INPUT_UNKNOWN, W_BRANCH_UNWIRED, W_UNRESOLVED_REF. --strict fails on them in every block, except W_BRANCH_UNWIRED on a block the lock binds (the stored workflow may hold it, and the server then stays quiet); --no-strict never fails on a warning. W_JSON_REF_UNTYPED is reported only on blocks the lock does not bind, and with --strict on every block. +These warnings, which an apply saves as written, fail the check as errors on every block the lock does not bind (a block added since the get, or every block of a new workflow): W_REQUIRED_MISSING, W_UNKNOWN_OPTION, W_UNKNOWN_OUTPUT_PATH, W_VALUE_KIND, W_OUT_OF_RANGE, W_LITERAL_SECRET, W_TABLE_UNKNOWN_COLUMN, W_CHILD_INPUT_UNKNOWN, W_BRANCH_UNWIRED, W_UNRESOLVED_REF. --strict fails on them in every block, except W_BRANCH_UNWIRED on a block the lock binds (the stored workflow may hold it, and the server then stays quiet); --no-strict fails on none of them. In a function file changed since the get, W_UNRESOLVED_REF is the error E_APPLY_UNRESOLVED_REF in every mode, as the server refuses it. W_JSON_REF_UNTYPED is reported only on blocks the lock does not bind, and with --strict on every block. Exit 10 (busy, retry): another sim workspaces source command is using this directory (SOURCE_DIR_BUSY); nothing was read or written. Run this one again once it finished. diff --git a/apps/docs/openapi-v2-workflows.json b/apps/docs/openapi-v2-workflows.json index e5bf63509a0..880fc7ad740 100644 --- a/apps/docs/openapi-v2-workflows.json +++ b/apps/docs/openapi-v2-workflows.json @@ -11980,7 +11980,7 @@ } } ], - "description": "Patch only supplied fields: `inputs`, `name`, `connections`, `removeEdges`, `nestedNodes`, `retry`, `triggerMode`, and `advancedMode`. `inputs` maps sub-block ids directly to values, never through `subBlocks`. Keep `retry`, `triggerMode`, and `advancedMode` beside `inputs`. `connections` maps source handles to target ids, `{ block, handle }`, or arrays; `success` aliases `source`. Re-sending `connections` replaces outgoing edges; use `removeEdges` to delete selected edges." + "description": "Patch only supplied fields: `inputs`, `name`, `connections`, `removeEdges`, `nestedNodes`, `retry`, `triggerMode`, `advancedMode`, and `parentId`. `inputs` maps sub-block ids directly to values, never through `subBlocks`. Keep `retry`, `triggerMode`, and `advancedMode` beside `inputs`. `connections` maps source handles to target ids, `{ block, handle }`, or arrays; `success` aliases `source`. Re-sending `connections` replaces outgoing edges; use `removeEdges` to delete selected edges. `parentId` moves the block, keeping its id, into the loop or parallel it names, or with `null` out of its container." } }, "required": ["operation_type", "block_id", "params"], @@ -18918,6 +18918,7 @@ "W_APPLY_CATALOG_MOVED", "W_APPLY_PROBABLE_RENAME", "E_APPLY_LINT", + "E_APPLY_UNRESOLVED_REF", "W_APPLY_LINT", "W_TABLE_UNKNOWN_COLUMN", "E_APPLY_INVALID_SCHEDULE", @@ -19362,6 +19363,7 @@ "W_APPLY_CATALOG_MOVED", "W_APPLY_PROBABLE_RENAME", "E_APPLY_LINT", + "E_APPLY_UNRESOLVED_REF", "W_APPLY_LINT", "W_TABLE_UNKNOWN_COLUMN", "E_APPLY_INVALID_SCHEDULE", @@ -20323,6 +20325,7 @@ "W_APPLY_CATALOG_MOVED", "W_APPLY_PROBABLE_RENAME", "E_APPLY_LINT", + "E_APPLY_UNRESOLVED_REF", "W_APPLY_LINT", "W_TABLE_UNKNOWN_COLUMN", "E_APPLY_INVALID_SCHEDULE", @@ -25365,6 +25368,7 @@ "W_APPLY_CATALOG_MOVED", "W_APPLY_PROBABLE_RENAME", "E_APPLY_LINT", + "E_APPLY_UNRESOLVED_REF", "W_APPLY_LINT", "W_TABLE_UNKNOWN_COLUMN", "E_APPLY_INVALID_SCHEDULE", @@ -25931,6 +25935,7 @@ "W_APPLY_CATALOG_MOVED", "W_APPLY_PROBABLE_RENAME", "E_APPLY_LINT", + "E_APPLY_UNRESOLVED_REF", "W_APPLY_LINT", "W_TABLE_UNKNOWN_COLUMN", "E_APPLY_INVALID_SCHEDULE", @@ -26340,6 +26345,7 @@ "W_APPLY_CATALOG_MOVED", "W_APPLY_PROBABLE_RENAME", "E_APPLY_LINT", + "E_APPLY_UNRESOLVED_REF", "W_APPLY_LINT", "W_TABLE_UNKNOWN_COLUMN", "E_APPLY_INVALID_SCHEDULE", diff --git a/apps/sim/blocks/blocks/condition.ts b/apps/sim/blocks/blocks/condition.ts index a9b180ef47f..58d0d555def 100644 --- a/apps/sim/blocks/blocks/condition.ts +++ b/apps/sim/blocks/blocks/condition.ts @@ -10,6 +10,7 @@ export const ConditionBlock: BlockConfig = { bestPractices: ` - Write the conditions using standard javascript syntax except referencing the outputs of previous blocks using <> syntax, and keep them as simple as possible. No hacky fallbacks. - Can reference workflow variables using syntax as usual within conditions. + - Branch handles (a connection's source handle in workflow operations): \`if\` for the first condition, \`else-if-0\`, \`else-if-1\`, … for each else-if in order, and \`else\`. Workflow source names each branch by its key instead (\`x..to(...)\`). `, docsLink: 'https://docs.sim.ai/workflows/blocks/condition', bgColor: '#FF752F', diff --git a/apps/sim/lib/api/contracts/v2/workflows.ts b/apps/sim/lib/api/contracts/v2/workflows.ts index eee136a7ecb..1fca08bda37 100644 --- a/apps/sim/lib/api/contracts/v2/workflows.ts +++ b/apps/sim/lib/api/contracts/v2/workflows.ts @@ -3908,8 +3908,10 @@ const v2WorkflowOperationParamsSchema = z ) .describe( 'Patch only supplied fields: `inputs`, `name`, `connections`, `removeEdges`, `nestedNodes`, ' + - `\`retry\`, \`triggerMode\`, and \`advancedMode\`. ${WORKFLOW_OPERATION_PARAM_ENVELOPE} ` + - 'Re-sending `connections` replaces outgoing edges; use `removeEdges` to delete selected edges.' + `\`retry\`, \`triggerMode\`, \`advancedMode\`, and \`parentId\`. ${WORKFLOW_OPERATION_PARAM_ENVELOPE} ` + + 'Re-sending `connections` replaces outgoing edges; use `removeEdges` to delete selected edges. ' + + '`parentId` moves the block, keeping its id, into the loop or parallel it names, or with ' + + '`null` out of its container.' ) const v2AddWorkflowBlockParamsSchema = z diff --git a/apps/sim/lib/workflows/application/apply-workflow-source.integration.ts b/apps/sim/lib/workflows/application/apply-workflow-source.integration.ts index 9be934a0bd2..de20f2c9239 100644 --- a/apps/sim/lib/workflows/application/apply-workflow-source.integration.ts +++ b/apps/sim/lib/workflows/application/apply-workflow-source.integration.ts @@ -2214,7 +2214,7 @@ export default workflow('', {}, () => { describe('renames in source', () => { /** Start → Boom → Tally → Report, each a function; Report's code reads Boom and Tally by $ref. */ - async function seedChain() { + async function seedChain(reportCode = 'return + ') { const workflowId = generateId() const now = new Date() await db.insert(workflow).values({ @@ -2238,7 +2238,7 @@ describe('renames in source', () => { const start: BlockState = { ...fn('Start', ''), type: 'start_trigger', subBlocks: {} } const boom = fn('Boom', 'return 1') const tally = fn('Tally', 'return 2') - const report = fn('Report', 'return + ') + const report = fn('Report', reportCode) const chain = [start, boom, tally, report] const saved = await saveWorkflowToNormalizedTables(workflowId, { blocks: Object.fromEntries(chain.map((block) => [block.id, block])), @@ -2264,6 +2264,59 @@ describe('renames in source', () => { } } + /** Start → Calc (a function whose result nothing types) → Reply, whose JSON data reads Calc. */ + async function seedUntypedReply() { + const seeded = await seedChain() + const source = await getSource(seeded.workflowId) + const written = await apply( + source, + { + 'workflow.ts': [ + '// @sim-sdk 1', + "import { codeFile, fn, response, trigger, workflow } from '@sim/workflow-sdk'", + '', + `export default workflow('Untyped reply ${seeded.workflowId}', {}, () => {`, + " const start = trigger.start('Start', {})", + " const calc = fn('Calc', { code: codeFile('./functions/calc.js') })", + " const reply = response('Reply', { dataMode: 'json', data: { total: calc.result.total } })", + ' start.to(calc)', + ' calc.to(reply)', + '})', + '', + ].join('\n'), + 'functions/calc.js': 'return JSON.parse($ref.start.input)\n', + }, + { allowDelete: true } + ) + expect(written.applied).toBe(true) + const calcId = written.lock.blocks.calc?.id + return { workflowId: seeded.workflowId, calcId } + } + + check( + 'a rename does not report W_JSON_REF_UNTYPED on a JSON field only its reference rewrite changed', + async () => { + const displayOnly = await seedUntypedReply() + const source = await getSource(displayOnly.workflowId) + expect(codes((await apply(source, source.files, { dryRun: true })).diagnostics)).toEqual([]) + + const renamed = await apply(source, edit(source.files, "fn('Calc'", "fn('Order Calc'"), { + dryRun: true, + }) + expect(renamed.diff.blocks.renamed).toEqual([ + { id: displayOnly.calcId, from: 'calc', to: 'ordercalc' }, + ]) + expect(codes(renamed.diagnostics)).toEqual([]) + + const both = await seedUntypedReply() + const bothSource = await getSource(both.workflowId) + const files = edit(renameIdent(bothSource.files, 'calc', 'total'), "fn('Calc'", "fn('Total'") + const inferred = await apply(bothSource, files, { dryRun: true }) + expect(inferred.diff.blocks.renamed).toEqual([{ id: both.calcId, from: 'calc', to: 'total' }]) + expect(codes(inferred.diagnostics)).not.toContain('W_JSON_REF_UNTYPED') + } + ) + check('two display-name renames in one save keep both ids and resolve their $refs', async () => { const seeded = await seedChain() const source = await getSource(seeded.workflowId) @@ -2462,7 +2515,7 @@ describe('renames in source', () => { }) check( - 'a $ref to a missing block in a function file is the same warning in check and dry run, at the file', + 'a $ref to a missing block the edit wrote into a function file is refused, at the file', async () => { const seeded = await seedChain() const source = await getSource(seeded.workflowId) @@ -2472,7 +2525,26 @@ describe('renames in source', () => { expect( dryRun.diagnostics.map((d) => ({ code: d.code, severity: d.severity, file: d.file })) - ).toEqual([{ code: 'W_UNRESOLVED_REF', severity: 'warning', file: 'functions/boom.js' }]) + ).toEqual([{ code: 'E_APPLY_UNRESOLVED_REF', severity: 'error', file: 'functions/boom.js' }]) + await expectRejected(apply(source, files), 'E_APPLY_UNRESOLVED_REF') + } + ) + + check( + 'a dangling reference the stored code already holds stays a warning, whatever else the apply changes', + async () => { + const seeded = await seedChain('return ') + const source = await getSource(seeded.workflowId) + expect(source.files['functions/report.js']).toBe('return $ref.ghost.result') + + const result = await apply(source, edit(source.files, "fn('Boom'", "fn('Bang'")) + + expect(result.applied).toBe(true) + expect( + result.diagnostics + .filter((d) => d.code === 'W_UNRESOLVED_REF') + .map((d) => ({ severity: d.severity, file: d.file })) + ).toEqual([{ severity: 'warning', file: 'functions/report.js' }]) } ) }) diff --git a/apps/sim/lib/workflows/application/source-evaluate.ts b/apps/sim/lib/workflows/application/source-evaluate.ts index d794cb00cc3..0d8c8cd031b 100644 --- a/apps/sim/lib/workflows/application/source-evaluate.ts +++ b/apps/sim/lib/workflows/application/source-evaluate.ts @@ -14,6 +14,8 @@ import { type Diagnostic, diffCanonical, encodeAnchor, + getOwn, + type LocatedDiagnostic, type LockIdentity, type MaterializeResult, materialize, @@ -33,6 +35,7 @@ import { WORKFLOW_RESOURCE_KINDS } from '@sim/workflow-types/resources' import type { BlockState, WorkflowState } from '@sim/workflow-types/workflow' import type { WorkspaceKind } from '@sim/workspace-code' import { and, inArray, ne } from 'drizzle-orm' +import { isEqual } from 'es-toolkit' import { V2_WORKFLOW_DESCRIPTION_MAX_LENGTH, V2_WORKFLOW_NAME_MAX_LENGTH, @@ -237,18 +240,18 @@ function withoutPendingReferences( /** * `lint` without the block-output references compile already reported in a function's code - * file (`W_UNRESOLVED_REF` on that block, located at the file line): the same dangling `$ref` - * is one warning there, as an offline check reports it, not also a lint error on the - * `codeFile()` call. + * file (`W_UNRESOLVED_REF`, or `E_APPLY_UNRESOLVED_REF` on code the edit changed, on that block, + * located at the file line): the same dangling `$ref` is one finding there, as an offline check + * reports it, not also a lint finding on the `codeFile()` call. */ function withoutCodeFileReferences( lint: WorkflowLintReport, - compiled: CompileResult['diagnostics'], + compiled: readonly LocatedDiagnostic[], keyOf: (id: string) => string | undefined ): WorkflowLintReport { const reported = new Set( compiled.flatMap((diagnostic) => - diagnostic.code === 'W_UNRESOLVED_REF' && + (diagnostic.code === 'W_UNRESOLVED_REF' || diagnostic.code === 'E_APPLY_UNRESOLVED_REF') && diagnostic.blockKey !== undefined && diagnostic.span.file !== SOURCE_ENTRY_FILE ? [diagnostic.blockKey] @@ -271,28 +274,60 @@ function withoutCodeFileReferences( } /** - * `diagnostics` without the code-file `W_UNRESOLVED_REF` findings a block rename resolves: an - * apply rewrites every `` to the new key, so `$ref.` runs. The compile - * already knew the renames the lock names by ident; a rename materialize inferred (name and - * const both changed) is known only now, so the source is compiled again with every rename and + * Findings a block rename can settle: a code-file `W_UNRESOLVED_REF` (an apply rewrites every + * `` to the new key, so `$ref.` runs) and a `W_JSON_REF_UNTYPED` on a JSON + * field whose only change is that rewrite. + */ +const RENAME_SETTLED_CODES: ReadonlySet = new Set([ + 'W_UNRESOLVED_REF', + 'W_JSON_REF_UNTYPED', +]) + +/** + * `diagnostics` without the {@link RENAME_SETTLED_CODES} findings a block rename settles. The + * compile already knew the renames the lock names by ident; a rename materialize inferred (name + * and const both changed) is known only now, so the source is compiled again with every rename and * a finding stays only where that compile still reports it. */ -function withoutRenamedCodeFileRefs( +function withoutRenameSettledFindings( diagnostics: CompileResult['diagnostics'], renames: ReadonlyMap, recompile: () => CompileResult ): CompileResult['diagnostics'] { - if (renames.size === 0 || !diagnostics.some((d) => d.code === 'W_UNRESOLVED_REF')) { + if (renames.size === 0 || !diagnostics.some((d) => RENAME_SETTLED_CODES.has(d.code))) { return diagnostics } const at = (d: CompileResult['diagnostics'][number]) => - `${d.span.file}:${d.span.line}:${d.span.column}` + `${d.code}@${d.span.file}:${d.span.line}:${d.span.column}` const still = new Set( recompile() - .diagnostics.filter((d) => d.code === 'W_UNRESOLVED_REF') + .diagnostics.filter((d) => RENAME_SETTLED_CODES.has(d.code)) .map(at) ) - return diagnostics.filter((d) => d.code !== 'W_UNRESOLVED_REF' || still.has(at(d))) + return diagnostics.filter((d) => !RENAME_SETTLED_CODES.has(d.code) || still.has(at(d))) +} + +/** + * `diagnostics` with each function code file `W_UNRESOLVED_REF` on a block whose code the source + * changed (`codeChanged`, by block key) refused as `E_APPLY_UNRESOLVED_REF`: a `$ref` the edit + * wrote that names no block fails at run time. On code the stored workflow already holds it stays + * a warning, so a workflow saved with a dangling reference still applies unchanged. + */ +function withEditedCodeFileRefsRefused( + diagnostics: CompileResult['diagnostics'], + codeChanged: (blockKey: string) => boolean +): LocatedDiagnostic[] { + return diagnostics.map((diagnostic): LocatedDiagnostic => { + if ( + diagnostic.code !== 'W_UNRESOLVED_REF' || + diagnostic.span.file === SOURCE_ENTRY_FILE || + diagnostic.blockKey === undefined || + !codeChanged(diagnostic.blockKey) + ) { + return diagnostic + } + return { ...diagnostic, code: 'E_APPLY_UNRESOLVED_REF', severity: 'error' } + }) } /** `{ span }` when there is one, to spread into a diagnostic. */ @@ -755,8 +790,21 @@ export async function evaluateSource(input: SourceEvaluationInput): Promise [storedKey, key])) - const compileDiagnostics = withoutRenamedCodeFileRefs(compiled.diagnostics, renamedKeys, () => - compileWorkflow({ ...compileInput, stored: compiled.stored, renamedKeys }) + const storedKeyOf = new Map([...renamedKeys].map(([storedKey, key]) => [key, storedKey])) + const storedCode = (key: string) => { + const storedBlock = + getOwn(compiled.stored.blocks, key) ?? + getOwn(compiled.stored.blocks, storedKeyOf.get(key) ?? key) + return storedBlock && getOwn(storedBlock.params, 'code') + } + const compileDiagnostics = withEditedCodeFileRefsRefused( + withoutRenameSettledFindings(compiled.diagnostics, renamedKeys, () => + compileWorkflow({ ...compileInput, stored: compiled.stored, renamedKeys }) + ), + (key) => { + const compiledBlock = getOwn(compiledWorkflow.blocks, key) + return !isEqual(compiledBlock && getOwn(compiledBlock.params, 'code'), storedCode(key)) + } ) const sourceRefusals: Diagnostic[] = [] const headerSpan = compiled.spans[encodeAnchor({ kind: 'header' })] diff --git a/apps/sim/lib/workflows/editing/builders.ts b/apps/sim/lib/workflows/editing/builders.ts index 5fd82f44eee..4efc86132f5 100644 --- a/apps/sim/lib/workflows/editing/builders.ts +++ b/apps/sim/lib/workflows/editing/builders.ts @@ -1055,6 +1055,10 @@ export function normalizeBlockIdsInOperations(operations: EditWorkflowOperation[ normalized.params.subflowId = replaceId(normalized.params.subflowId) } + if (typeof normalized.params.parentId === 'string') { + normalized.params.parentId = replaceId(normalized.params.parentId) + } + // Update connection references if (normalized.params.connections) { const normalizedConnections: Record = {} diff --git a/apps/sim/lib/workflows/editing/operations.test.ts b/apps/sim/lib/workflows/editing/operations.test.ts index ee2897479af..cf8c4f02e4e 100644 --- a/apps/sim/lib/workflows/editing/operations.test.ts +++ b/apps/sim/lib/workflows/editing/operations.test.ts @@ -1683,6 +1683,177 @@ describe('connection shape validation', () => { }) }) +describe('moving an existing block with edit params.parentId', () => { + function workflowWithLoop() { + const block = (id: string, type: string, data: Record = {}) => ({ + id, + type, + name: id, + position: { x: 400, y: 200 }, + enabled: true, + subBlocks: {}, + outputs: {}, + data, + }) + return { + blocks: { + 'loop-1': { + ...block('loop-1', 'loop', { loopType: 'for', count: 2 }), + position: { x: 50, y: 60 }, + }, + 'fn-1': block('fn-1', 'function'), + 'fn-2': { + ...block('fn-2', 'function', { parentId: 'loop-1', extent: 'parent' }), + position: { x: 10, y: 20 }, + }, + }, + edges: [], + loops: {}, + parallels: {}, + } + } + + it('moves a top-level block into a loop, keeping its id', () => { + const { state, skippedItems } = applyOperationsToWorkflowState(workflowWithLoop(), [ + { operation_type: 'edit', block_id: 'fn-1', params: { parentId: 'loop-1' } }, + ]) + + expect(skippedItems).toEqual([]) + expect(state.blocks['fn-1'].data).toMatchObject({ parentId: 'loop-1', extent: 'parent' }) + expect(state.blocks['fn-1'].position).toEqual({ x: 0, y: 0 }) + }) + + it('moves a block out of its loop with a null parentId, where it sat on the canvas', () => { + const { state, skippedItems } = applyOperationsToWorkflowState(workflowWithLoop(), [ + { operation_type: 'edit', block_id: 'fn-2', params: { parentId: null } }, + ]) + + expect(skippedItems).toEqual([]) + expect(state.blocks['fn-2'].data?.parentId).toBeUndefined() + expect(state.blocks['fn-2'].position).toEqual({ x: 60, y: 80 }) + }) + + it('declines a parentId that names no loop or parallel, saying so', () => { + const { state, skippedItems } = applyOperationsToWorkflowState(workflowWithLoop(), [ + { operation_type: 'edit', block_id: 'fn-1', params: { parentId: 'fn-2' } }, + ]) + + expect(state.blocks['fn-1'].data?.parentId).toBeUndefined() + expect(skippedItems).toEqual([ + expect.objectContaining({ + type: 'invalid_subflow_parent', + operationType: 'edit', + blockId: 'fn-1', + reason: expect.stringContaining('not a loop or parallel container'), + }), + ]) + }) + it.each([ + ['itself', 'loop-1', 'loop-1'], + ['a container nested inside it', 'loop-1', 'loop-2'], + ])('refuses to make a container a child of %s', (_label, blockId, parentId) => { + const workflow = workflowWithLoop() + const nested = { + ...workflow.blocks['loop-1'], + id: 'loop-2', + name: 'loop-2', + data: { loopType: 'for', count: 2, parentId: 'loop-1', extent: 'parent' }, + position: { x: 5, y: 5 }, + } + const { state, skippedItems } = applyOperationsToWorkflowState( + { ...workflow, blocks: { ...workflow.blocks, 'loop-2': nested } }, + [{ operation_type: 'edit', block_id: blockId, params: { parentId } }] + ) + + expect(state.blocks['loop-1'].data?.parentId).toBeUndefined() + expect(skippedItems).toEqual([ + expect.objectContaining({ + type: 'invalid_subflow_parent', + operationType: 'edit', + blockId, + reason: expect.stringContaining('inside itself'), + }), + ]) + }) + + it('refuses to insert an existing container into a container nested inside it', () => { + const OUTER = '7c7c7c7c-7c7c-4c7c-8c7c-7c7c7c7c7c7c' + const INNER = '8d8d8d8d-8d8d-4d8d-8d8d-8d8d8d8d8d8d' + const workflow = workflowWithLoop() + const loop = workflow.blocks['loop-1'] + const blocks = { + [OUTER]: { ...loop, id: OUTER, name: 'outer' }, + [INNER]: { + ...loop, + id: INNER, + name: 'inner', + data: { loopType: 'for', count: 2, parentId: OUTER, extent: 'parent' }, + position: { x: 5, y: 5 }, + }, + } + const { state, skippedItems } = applyOperationsToWorkflowState({ ...workflow, blocks }, [ + { + operation_type: 'insert_into_subflow', + block_id: OUTER, + params: { subflowId: INNER, type: 'loop', name: 'outer' }, + }, + ]) + + expect(state.blocks[OUTER].data?.parentId).toBeUndefined() + expect(skippedItems).toEqual([ + expect.objectContaining({ + type: 'invalid_subflow_parent', + operationType: 'insert_into_subflow', + blockId: OUTER, + reason: expect.stringContaining('inside itself'), + }), + ]) + }) + + it('moves a block into a loop the same batch adds, through its normalized id', () => { + const { state, skippedItems } = applyOperationsToWorkflowState(workflowWithLoop(), [ + { + operation_type: 'add', + block_id: 'new-loop', + params: { type: 'loop', name: 'New Loop', inputs: { loopType: 'for', iterations: 2 } }, + }, + { operation_type: 'edit', block_id: 'fn-1', params: { parentId: 'new-loop' } }, + ]) + + expect(skippedItems).toEqual([]) + const newLoopId = Object.keys(state.blocks).find((id) => state.blocks[id].name === 'New Loop') + expect(newLoopId).toBeDefined() + expect(newLoopId).not.toBe('new-loop') + expect(state.blocks['fn-1'].data?.parentId).toBe(newLoopId) + }) + + it('moves a block out of a nested loop to where it sat on the canvas', () => { + const workflow = workflowWithLoop() + const inner = { + ...workflow.blocks['loop-1'], + id: 'loop-2', + name: 'loop-2', + data: { loopType: 'for', count: 2, parentId: 'loop-1', extent: 'parent' }, + position: { x: 100, y: 200 }, + } + const child = { + ...workflow.blocks['fn-2'], + id: 'fn-3', + name: 'fn-3', + data: { parentId: 'loop-2', extent: 'parent' }, + position: { x: 1, y: 2 }, + } + const { state, skippedItems } = applyOperationsToWorkflowState( + { ...workflow, blocks: { ...workflow.blocks, 'loop-2': inner, 'fn-3': child } }, + [{ operation_type: 'edit', block_id: 'fn-3', params: { parentId: null } }] + ) + + expect(skippedItems).toEqual([]) + expect(state.blocks['fn-3'].data?.parentId).toBeUndefined() + expect(state.blocks['fn-3'].position).toEqual({ x: 151, y: 262 }) + }) +}) + describe('edge cycles', () => { const BLOCK_A = '5a5a5a5a-5a5a-4a5a-8a5a-5a5a5a5a5a5a' const BLOCK_B = '6b6b6b6b-6b6b-4b6b-8b6b-6b6b6b6b6b6b' diff --git a/apps/sim/lib/workflows/editing/operations.ts b/apps/sim/lib/workflows/editing/operations.ts index f6100fd5cb8..c0879c35537 100644 --- a/apps/sim/lib/workflows/editing/operations.ts +++ b/apps/sim/lib/workflows/editing/operations.ts @@ -1,6 +1,7 @@ import { createLogger } from '@sim/logger' import { isRecordLike } from '@sim/utils/object' import { isNonEmptyValue } from '@sim/workflow-code/catalog/canonical-index' +import type { BlockState } from '@sim/workflow-types/workflow' import { isEqual } from 'es-toolkit' import { checkBlockPlacement } from '@/lib/workflows/editing/gates' import { isValidKey } from '@/lib/workflows/sanitization/key-validation' @@ -483,6 +484,10 @@ export function handleEditOperation(op: EditWorkflowOperation, ctx: OperationCon else block.type = params.type } + if (params && Object.hasOwn(params, 'parentId')) { + moveToParent(block, block_id, params.parentId, ctx) + } + if (params?.inputs) { if (!block.subBlocks) block.subBlocks = {} @@ -740,6 +745,81 @@ export function handleEditOperation(op: EditWorkflowOperation, ctx: OperationCon } } +/** + * An edit's `params.parentId`: moves existing block `block` into the loop or parallel it names, + * or with `null` (or an empty string) out of its container onto the canvas, keeping its id, as + * `insert_into_subflow` and `extract_from_subflow` move one. A parentId that is no container is + * declined with the reason. + */ +function moveToParent( + block: BlockState, + blockId: string, + parentId: unknown, + ctx: OperationContext +): void { + const currentParent = block.data?.parentId + if (parentId === null || parentId === '') { + if (currentParent) placeAtTopLevel(block, currentParent, ctx.modifiedState.blocks) + return + } + if (typeof parentId !== 'string') { + logSkippedItem(ctx.skippedItems, { + type: 'invalid_subflow_parent', + operationType: 'edit', + blockId, + reason: `params.parentId of block "${blockId}" must be a loop or parallel id, or null to move it out of its container`, + details: { parentId }, + }) + return + } + if (parentId === currentParent) return + if (nestsInItself(blockId, parentId, 'edit', ctx)) return + if (!subflowContainer(parentId, blockId, 'edit', ctx)) return + placeInSubflow(block, parentId) +} + +/** + * Whether placing existing block `blockId` in container `parentId` would put it inside itself + * (`parentId` is the block or sits anywhere inside it, a parent cycle), after recording why + * `operationType` skips it. + */ +function nestsInItself( + blockId: string, + parentId: string, + operationType: string, + ctx: OperationContext +): boolean { + if (!isSelfOrDescendant(parentId, blockId, ctx.modifiedState.blocks)) return false + logSkippedItem(ctx.skippedItems, { + type: 'invalid_subflow_parent', + operationType, + blockId, + reason: `Cannot place block "${blockId}" in "${parentId}" - that would place the block inside itself`, + details: { parentId }, + }) + return true +} + +/** + * Whether block `candidateId` is block `ancestorId` itself or sits anywhere inside it, following + * the `parentId` chain up from `candidateId`. Making `ancestorId` a child of such a block would + * create a parent cycle. + */ +function isSelfOrDescendant( + candidateId: string, + ancestorId: string, + blocks: Record +): boolean { + const seen = new Set() + let current: string | undefined = candidateId + while (current && !seen.has(current)) { + if (current === ancestorId) return true + seen.add(current) + current = blocks[current]?.data?.parentId + } + return false +} + export function handleAddOperation(op: EditWorkflowOperation, ctx: OperationContext): void { const { modifiedState, skippedItems, validationErrors, permissionConfig, deferredConnections } = ctx @@ -859,39 +939,7 @@ export function handleInsertIntoSubflowOperation( return } - const subflowBlock = modifiedState.blocks[subflowId] - if (!subflowBlock) { - logSkippedItem(skippedItems, { - type: 'invalid_subflow_parent', - operationType: 'insert_into_subflow', - blockId: block_id, - reason: `Subflow block "${subflowId}" not found - block "${block_id}" not inserted`, - details: { subflowId }, - }) - return - } - - if (subflowBlock.locked) { - logSkippedItem(skippedItems, { - type: 'block_locked', - operationType: 'insert_into_subflow', - blockId: block_id, - reason: `Subflow "${subflowId}" is locked - cannot insert block "${block_id}"`, - details: { subflowId }, - }) - return - } - - if (subflowBlock.type !== 'loop' && subflowBlock.type !== 'parallel') { - logSkippedItem(skippedItems, { - type: 'invalid_subflow_parent', - operationType: 'insert_into_subflow', - blockId: block_id, - reason: `Cannot insert block "${block_id}" into "${subflowId}" - the target is a "${subflowBlock.type}" block, not a loop or parallel container`, - details: { subflowId, subflowType: subflowBlock.type }, - }) - return - } + if (!subflowContainer(subflowId, block_id, 'insert_into_subflow', ctx)) return const existingBlock = modifiedState.blocks[block_id] @@ -905,17 +953,9 @@ export function handleInsertIntoSubflowOperation( }) return } + if (nestsInItself(block_id, subflowId, 'insert_into_subflow', ctx)) return - // Moving existing block into subflow — update parent and reset position. - // Position must be reset because React Flow uses coordinates relative to - // the parent container; keeping the old absolute position would place the - // block far outside the container's bounds. - existingBlock.data = { - ...existingBlock.data, - parentId: subflowId, - extent: 'parent' as const, - } - existingBlock.position = { x: 0, y: 0 } + placeInSubflow(existingBlock, subflowId) if (params.inputs) { const validationResult = validateInputsForBlock( @@ -1092,23 +1132,86 @@ export function handleExtractFromSubflowOperation( }) } - // Convert from relative (to container) to absolute position so the block - // appears at roughly the same visual location after extraction. This avoids - // needing targeted layout to reposition it — extracted blocks often lose - // their edges to siblings still in the container, making them disconnected - // and causing layout to stack them at layer 0. - const container = modifiedState.blocks[subflowId] - if (container?.position && block.position) { - block.position = { - x: (container.position.x ?? 0) + (block.position.x ?? 0), - y: (container.position.y ?? 0) + (block.position.y ?? 0), - } - } else { - // Fallback to (0,0) which signals to blocksNeedingLayout in index.ts - // that this block requires targeted layout repositioning. - block.position = { x: 0, y: 0 } + placeAtTopLevel(block, subflowId, modifiedState.blocks) +} + +/** + * The loop or parallel container `subflowId` names, for `operationType` to place block `blockId` + * in, or `null` after recording why it cannot: no such block, locked, or not a container. + */ +function subflowContainer( + subflowId: string, + blockId: string, + operationType: string, + ctx: OperationContext +): BlockState | null { + const container: BlockState | undefined = ctx.modifiedState.blocks[subflowId] + if (!container) { + logSkippedItem(ctx.skippedItems, { + type: 'invalid_subflow_parent', + operationType, + blockId, + reason: `Subflow block "${subflowId}" not found - block "${blockId}" not placed in it`, + details: { subflowId }, + }) + return null + } + if (container.locked) { + logSkippedItem(ctx.skippedItems, { + type: 'block_locked', + operationType, + blockId, + reason: `Subflow "${subflowId}" is locked - cannot place block "${blockId}" in it`, + details: { subflowId }, + }) + return null + } + if (container.type !== 'loop' && container.type !== 'parallel') { + logSkippedItem(ctx.skippedItems, { + type: 'invalid_subflow_parent', + operationType, + blockId, + reason: `Cannot place block "${blockId}" in "${subflowId}" - the target is a "${container.type}" block, not a loop or parallel container`, + details: { subflowId, subflowType: container.type }, + }) + return null } + return container +} + +/** + * Makes `block` a child of container `subflowId`. Its position resets because React Flow reads a + * child's coordinates relative to its container; the old absolute position would place it far + * outside the container's bounds. + */ +function placeInSubflow(block: BlockState, subflowId: string): void { + block.data = { ...block.data, parentId: subflowId, extent: 'parent' } + block.position = { x: 0, y: 0 } +} +/** + * Takes `block` out of container `containerId` onto the canvas, at the absolute position it showed + * at (its own offset plus that of every enclosing container), so it needs no targeted layout (an + * extracted block often loses its edges to siblings left in the container, and layout would stack + * it at layer 0). Without every position it goes to (0,0), which marks it for targeted layout. + */ +function placeAtTopLevel( + block: BlockState, + containerId: string, + blocks: Record +): void { + let position = block.position ? { x: block.position.x ?? 0, y: block.position.y ?? 0 } : null + const seen = new Set() + let current: string | undefined = containerId + while (position && current && !seen.has(current)) { + seen.add(current) + const container: BlockState | undefined = blocks[current] + position = container?.position + ? { x: position.x + (container.position.x ?? 0), y: position.y + (container.position.y ?? 0) } + : null + current = container?.data?.parentId + } + block.position = position ?? { x: 0, y: 0 } if (block.data) { block.data.parentId = undefined block.data.extent = undefined diff --git a/packages/sim-cli/src/commands/protocol/source-primer.ts b/packages/sim-cli/src/commands/protocol/source-primer.ts index 3d77d91b74a..fc9d577cdec 100644 --- a/packages/sim-cli/src/commands/protocol/source-primer.ts +++ b/packages/sim-cli/src/commands/protocol/source-primer.ts @@ -37,7 +37,7 @@ const PARAMS_COMMAND = 'sim workflows source params' /** The rules every source directory shares, after the kind's own edit loop. */ function sharedRules(group: string): string { return `- **Identity**: a block is its entry in \`${SOURCE_LOCK_FILE}\`, which binds it by its const name or its display name. Change the display name (first argument) freely: the block keeps its id. Renaming the const too keeps the id when its settings, neighbours or references still tie it to the old block; otherwise the apply refuses it as a delete (\`E_APPLY_BLOCK_DELETE\`), and the dry run names it. Safest: keep the const in the same apply. -- **References**: in \`${SOURCE_ENTRY_FILE}\` read an output as \`x.content\`, a loop or parallel item as \`p.currentItem\` (\`p.index\`), text as \`\` t\`…\` \`\`, env as \`env.NAME\`. In a function code file (\`codeFile('./functions/x.js')\`) read an output as \`$ref..\`, the key being the display name lowercased with whitespace and dots removed ("Triage Agent" → \`$ref.triageagent.content\`); in a loop body \`$ref.loop.currentItem\` / \`$ref.loop.index\`, in a parallel \`$ref.parallel.currentItem\` / \`$ref.parallel.index\`; env as \`$env.NAME\`. +- **References**: in \`${SOURCE_ENTRY_FILE}\` read an output as \`x.content\`, a loop or parallel item as \`p.currentItem\` (\`p.index\`), text as \`\` t\`…\` \`\`, env as \`env.NAME\`. In a function code file (\`codeFile('./functions/x.js')\`) read an output as \`$ref..\`, the key being the display name lowercased with whitespace and dots removed ("Triage Agent" → \`$ref.triageagent.content\`); in a loop body \`$ref.loop.currentItem\` / \`$ref.loop.index\`, in a parallel \`$ref.parallel.currentItem\` / \`$ref.parallel.index\`; env as \`$env.NAME\`. A rename keeps its references working: the apply rewrites every \`$ref.\` in the function files to the new key (and the files it writes back hold it), so leave them or write the new key yourself. A \`$ref\` that names no block is an error in a function file you edited (\`E_APPLY_UNRESOLVED_REF\` in \`check\` and the dry run). - **Look up a builder**: \`${PARAMS_COMMAND} \` (\`table.upsertRow\`, \`agent\`, \`trigger.schedule\`; a namespace such as \`gmail\` lists its builders), offline. - **Exit codes**: 0 ok, 1 errors (fix what it printed), ${SOURCE_STALE_EXIT_CODE} stale (\`${group} get --dir \`, redo the edit there), ${RATE_LIMITED_EXIT_CODE} rate limited, ${SOURCE_BUSY_EXIT_CODE} busy, ${SOURCE_LOCAL_CHANGES_EXIT_CODE}/${SOURCE_DRIFT_EXIT_CODE} \`status\`: your edits / the server changed. Every code: \`${SOURCE_AGENTS_FILE}\`.` } diff --git a/packages/sim-cli/src/commands/protocol/source-status.ts b/packages/sim-cli/src/commands/protocol/source-status.ts index 820dbcc78e1..ed4ab27d1ce 100644 --- a/packages/sim-cli/src/commands/protocol/source-status.ts +++ b/packages/sim-cli/src/commands/protocol/source-status.ts @@ -21,10 +21,29 @@ import { noAnswer, SimApiError } from '../../http/client' import { printLine } from '../../output/io' import { styles } from '../../output/presentation' import { bytes, printDocument, sanitize } from '../../output/render' +import { type Diagnostic, findingsOutput } from './source-ui' /** Whether the server was asked: `checked`, `unreachable` (degraded to local), or `offline`. */ export type ServerCheck = 'checked' | 'unreachable' | 'offline' +/** + * The dry run's findings a status's plan shows: its errors and warnings, less those `listed` + * says the status reports as a change of its own (a stale base is drift or a conflict), and the + * info `I_FINDINGS_TRUNCATED` that says the list is incomplete (counted as neither). + */ +export function planFindings( + diagnostics: readonly Diagnostic[], + listed: (code: Diagnostic['code']) => boolean +): Diagnostic[] { + return diagnostics.filter( + (finding) => + (finding.severity === 'error' || + finding.severity === 'warning' || + finding.code === 'I_FINDINGS_TRUNCATED') && + !listed(finding.code) + ) +} + /** The next command a status recommends, and why. */ export interface NextStep { command: string @@ -113,19 +132,24 @@ const SERVER_WORDS = { } as const /** - * Prints a status: the machine shape for `json` and `yaml`, else one screen naming each local - * change, each drifted and conflicting resource, the dry run's summary line and the next command. + * Prints a status: the machine shape for `json` and `yaml`, with the plan's `findings` (from + * {@link planFindings}) under `diagnostics`, else the findings on stderr as an apply prints them + * and one screen naming each local change, each drifted and conflicting resource, the dry run's + * summary line and the next command. */ export function printStatusReport( format: OutputFormat, heading: string, report: StatusReport, - planLine: (plan: Plan) => string + planLine: (plan: Plan) => string, + findings: readonly Diagnostic[] ): void { + const shown = findingsOutput(format) if (format === 'json' || format === 'yaml') { - printDocument(format, report) + printDocument(format, findings.length > 0 ? { ...report, ...shown.result(findings) } : report) return } + shown.print(findings) const s = styles() const lines = [s.bold(sanitize(heading))] if (report.local.length === 0) lines.push('No local changes.') diff --git a/packages/sim-cli/src/commands/protocol/workflow-source-check.test.ts b/packages/sim-cli/src/commands/protocol/workflow-source-check.test.ts index e2989388885..a03bba7e97d 100644 --- a/packages/sim-cli/src/commands/protocol/workflow-source-check.test.ts +++ b/packages/sim-cli/src/commands/protocol/workflow-source-check.test.ts @@ -6,6 +6,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import type { SimApiError } from '../../http/client' import { cacheCatalogText } from '../../test/catalog-cache' import { contextMockFns, mockedProfile } from '../../test/context-mock' +import { digest } from './source-dir' import { attachWorkflowSourceCheck } from './workflow-source-check' vi.mock('../../context', async () => (await import('../../test/context-mock')).contextMock) @@ -197,6 +198,17 @@ function writeLock(keys: readonly string[]): void { ) } +/** Records `files` in `.sim/written.json` as a get leaves them. */ +function writeWritten(files: Record): void { + writeFileSync( + join(dir, '.sim/written.json'), + JSON.stringify({ + version: 1, + files: Object.fromEntries(Object.entries(files).map(([path, text]) => [path, digest(text)])), + }) + ) +} + const TSCONFIG_MARKER_LINE = '// Written by `sim workflows source get`; remove this line to keep your own edits.' @@ -486,7 +498,7 @@ export default workflow('W', {}, () => { await check() expect(result().diagnostics).toContainEqual( expect.objectContaining({ - code: 'W_UNRESOLVED_REF', + code: 'E_APPLY_UNRESOLVED_REF', severity: 'error', file: 'functions/total.js', line: 1, @@ -497,6 +509,7 @@ export default workflow('W', {}, () => { process.exitCode = undefined writeLock(['start', 'total']) + writeWritten({ 'functions/total.js': 'return $ref.ordertotals.sum\n' }) await check() expect(result()).toMatchObject({ errors: 0, warnings: 1 }) expect(process.exitCode).toBeUndefined() @@ -506,6 +519,82 @@ export default workflow('W', {}, () => { expect(process.exitCode).toBe(1) }) + it('fails on a dangling $ref the edit wrote into a bound function file, whatever the strict mode', async () => { + cacheCatalog() + mkdirSync(join(dir, 'functions'), { recursive: true }) + writeFileSync( + join(dir, 'workflow.ts'), + `// @sim-sdk 1 +import { codeFile, fn, trigger, workflow } from '@sim/workflow-sdk' + +export default workflow('W', {}, () => { + const start = trigger.start('Start', {}) + const total = fn('Total', { code: codeFile('./functions/total.js') }) + start.to(total) +}) +` + ) + writeLock(['start', 'total']) + writeWritten({ 'functions/total.js': 'return 1\n' }) + writeFileSync(join(dir, 'functions/total.js'), 'return $ref.ordertotals.sum\n') + + for (const flags of [[], ['--no-strict']]) { + process.exitCode = undefined + await checkIn(dir, ...flags) + expect(result().diagnostics).toContainEqual( + expect.objectContaining({ + code: 'E_APPLY_UNRESOLVED_REF', + severity: 'error', + file: 'functions/total.js', + }) + ) + expect(process.exitCode).toBe(1) + } + }) + + it('keeps a stored dangling $ref a warning when only its function file was renamed', async () => { + cacheCatalog() + mkdirSync(join(dir, 'functions'), { recursive: true }) + writeFileSync( + join(dir, 'workflow.ts'), + `// @sim-sdk 1 +import { codeFile, fn, trigger, workflow } from '@sim/workflow-sdk' + +export default workflow('W', {}, () => { + const start = trigger.start('Start', {}) + const total = fn('Total', { code: codeFile('./functions/sum.js') }) + start.to(total) +}) +` + ) + writeFileSync( + join(dir, '.sim/lock.json'), + JSON.stringify({ + version: 1, + blocks: { + start: { id: 'id-start', parent: null, ident: 'start', order: 0 }, + total: { + id: 'id-total', + parent: null, + ident: 'total', + order: 1, + codeFile: 'functions/total.js', + }, + }, + variables: {}, + }) + ) + writeWritten({ 'functions/total.js': 'return $ref.ordertotals.sum\n' }) + writeFileSync(join(dir, 'functions/sum.js'), 'return $ref.ordertotals.sum\n') + + for (const flags of [[], ['--no-strict']]) { + process.exitCode = undefined + await checkIn(dir, ...flags) + expect(result()).toMatchObject({ errors: 0, warnings: 1 }) + expect(process.exitCode).toBeUndefined() + } + }) + it('fails on a bound block with --strict and on nothing with --no-strict', async () => { cacheCatalog() writeFileSync(join(dir, 'workflow.ts'), UNKNOWN_METHOD) diff --git a/packages/sim-cli/src/commands/protocol/workflow-source-check.ts b/packages/sim-cli/src/commands/protocol/workflow-source-check.ts index c8bb5177c82..2b451892029 100644 --- a/packages/sim-cli/src/commands/protocol/workflow-source-check.ts +++ b/packages/sim-cli/src/commands/protocol/workflow-source-check.ts @@ -25,7 +25,9 @@ import { SimApiError } from '../../http/client' import { printProtocolResult } from './result' import { absoluteDirOption, + digest, directoryWorkspace, + readFolderWritten, readSourceJson, readSourceText, readStarterWorkspace, @@ -88,6 +90,18 @@ const STORED_EXEMPT_CODES: ReadonlySet = new Set(['W_BRANCH_UNWIRED']) */ const EDIT_SCOPED_CODES: ReadonlySet = new Set(['W_JSON_REF_UNTYPED']) +/** + * Warnings in a function code file a check refuses under the server's error code, whatever the + * strict mode, when that file changed since the get wrote it (or is new; a file only moved from + * the path the lock bound its block to is unchanged while its text matches that path's digest): + * the server refuses them on code the edit changed, and keeps them as warnings only on code the + * stored workflow already holds. `W_UNRESOLVED_REF`, a `$ref` naming no block or variable, which fails at run time, is + * `E_APPLY_UNRESOLVED_REF`. + */ +const EDITED_FILE_REFUSALS: ReadonlyMap = new Map([ + ['W_UNRESOLVED_REF', 'E_APPLY_UNRESOLVED_REF'], +]) + /** * Which {@link STRICT_CODES} warnings fail a check: `new` those on a block the lock does not bind * (one added since the get, or every block of a workflow that has no lock yet), `all` every one, @@ -125,8 +139,12 @@ These warnings, which an apply saves as written, fail the check as errors on eve does not bind (a block added since the get, or every block of a new workflow): ${[...STRICT_CODES].map((code) => ` ${code}`).join(',\n')}. --strict fails on them in every block, except ${[...STORED_EXEMPT_CODES].join(', ')} on a block the -lock binds (the stored workflow may hold it, and the server then stays quiet); --no-strict never -fails on a warning. ${[...EDIT_SCOPED_CODES].join(', ')} is reported only on blocks the lock does not +lock binds (the stored workflow may hold it, and the server then stays quiet); --no-strict fails +on none of them. In a function file changed since the get, ${[...EDITED_FILE_REFUSALS] + .map(([warning, refusal]) => `${warning} is the error ${refusal}`) + .join( + ', ' + )} in every mode, as the server refuses it. ${[...EDIT_SCOPED_CODES].join(', ')} is reported only on blocks the lock does not bind, and with --strict on every block.` /** Adds the `--strict` / `--no-strict` pair to a check command. */ @@ -479,6 +497,22 @@ export async function checkWorkflowDir( : [] const bound = (blockKey: string | undefined) => lock !== undefined && blockKey !== undefined && Object.hasOwn(lock.blocks, blockKey) + const written = await readFolderWritten(dir) + const edited = (file: string, blockKey: string | undefined) => { + const text = files[file] + if (text === undefined) return true + const textDigest = digest(text) + if (written.digests[file] === textDigest) return false + const previousFile = + lock && blockKey !== undefined && Object.hasOwn(lock.blocks, blockKey) + ? lock.blocks[blockKey].codeFile + : undefined + return previousFile === undefined || written.digests[previousFile] !== textDigest + } + const editedRefusal = (code: string, file: string | undefined, blockKey: string | undefined) => + file !== undefined && file !== SOURCE_ENTRY_FILE && edited(file, blockKey) + ? EDITED_FILE_REFUSALS.get(code) + : undefined const fails = (code: string, blockKey: string | undefined) => STRICT_CODES.has(code) && !(STORED_EXEMPT_CODES.has(code) && bound(blockKey)) && @@ -491,9 +525,10 @@ export async function checkWorkflowDir( ...compiled.diagnostics.filter((diagnostic) => reported(diagnostic.code, diagnostic.blockKey)), ...declared, ].map(({ code, severity, message, hint, frame, span, blockKey }): CheckFinding => { - const raised = severity === 'warning' && fails(code, blockKey) + const refusal = severity === 'warning' ? editedRefusal(code, span?.file, blockKey) : undefined + const raised = refusal !== undefined || (severity === 'warning' && fails(code, blockKey)) return { - code, + code: refusal ?? code, severity: raised ? 'error' : severity, message: raised ? message.replace(KEPT_AS_WRITTEN, '.') : message, ...(hint !== undefined ? { hint } : {}), diff --git a/packages/sim-cli/src/commands/protocol/workflow-source-status.test.ts b/packages/sim-cli/src/commands/protocol/workflow-source-status.test.ts index 1f210b95193..0248985e201 100644 --- a/packages/sim-cli/src/commands/protocol/workflow-source-status.test.ts +++ b/packages/sim-cli/src/commands/protocol/workflow-source-status.test.ts @@ -160,6 +160,58 @@ describe('workflows source status', () => { expect(process.exitCode ?? 0).toBe(0) }) + it('lists the warnings its plan counts, in the document and on stderr', async () => { + const warning = { + code: 'W_UNRESOLVED_REF', + severity: 'warning', + message: '$ref.nosuch.x names no block, so it does not resolve at run time.', + file: 'functions/score.js', + line: 1, + column: 8, + frame: '> 1 | return $ref.nosuch.x', + } + mockRequest.mockResolvedValue(dryRun([warning, STALE])) + + await run(root, 'status') + + const { frame: _frame, ...listed } = warning + expect(printed()).toMatchObject({ plan: { warnings: 1 }, diagnostics: [listed] }) + + contextMockState.output = 'text' + mockProfileFrom.mockReturnValue({ + ...mockedProfile(), + workspaceId: WORKSPACE_ID, + output: 'text', + }) + await run(root, 'status') + + const stderr = vi.mocked(console.error).mock.calls.flat().join('\n') + expect(stderr).toContain('functions/score.js:1:8 warning W_UNRESOLVED_REF') + expect(stderr).not.toContain('E_APPLY_STALE') + }) + + it('keeps the marker that its listed findings were truncated, counting it as neither', async () => { + const warning = { + code: 'W_UNRESOLVED_REF', + severity: 'warning', + message: '$ref.nosuch.x names no block, so it does not resolve at run time.', + file: 'functions/score.js', + } + const truncated = { + code: 'I_FINDINGS_TRUNCATED', + severity: 'info', + message: '40 more findings were left out.', + } + mockRequest.mockResolvedValue(dryRun([warning, truncated])) + + await run(root, 'status') + + expect(printed()).toMatchObject({ + plan: { errors: 0, warnings: 1 }, + diagnostics: [warning, truncated], + }) + }) + it('reports an edit, a deleted function file and a new one, exit 11', async () => { write(root, 'workflow.ts', `${ENTRY}// edited\n`) unlinkSync(join(root, 'functions/score.js')) diff --git a/packages/sim-cli/src/commands/protocol/workflow-source-status.ts b/packages/sim-cli/src/commands/protocol/workflow-source-status.ts index 4bb903d4e7f..cfcf68d860c 100644 --- a/packages/sim-cli/src/commands/protocol/workflow-source-status.ts +++ b/packages/sim-cli/src/commands/protocol/workflow-source-status.ts @@ -37,6 +37,7 @@ import { loginCommand, nextStep, pathFilter, + planFindings, printDiffReport, printStatusReport, type ServerCheck, @@ -47,6 +48,7 @@ import { unreachableReason, } from './source-status' import { withSourceExitCode } from './source-tree' +import type { Diagnostic } from './source-ui' import { readBase, readLock, readSourceFiles } from './workflow-source-apply' type ApplyData = ApplyWorkflowSourceResponse['data'] @@ -102,11 +104,15 @@ interface WorkflowPlanSummary { warnings: number } +/** The dry run's findings the plan counts: all but `E_APPLY_STALE`, which the status lists. */ +function workflowPlanFindings(data: ApplyData): Diagnostic[] { + return planFindings(data.diagnostics, (code) => code === 'E_APPLY_STALE') +} + function planSummary(data: ApplyData): WorkflowPlanSummary { + const planned = workflowPlanFindings(data) const findings = (severity: string) => - data.diagnostics.filter( - (finding) => finding.severity === severity && finding.code !== 'E_APPLY_STALE' - ).length + planned.filter((finding) => finding.severity === severity).length return { blocksAdded: data.diff.blocks.added.length, blocksRemoved: data.diff.blocks.removed.length, @@ -214,7 +220,13 @@ export function attachWorkflowSourceStatus(source: Command): void { login, }), } - printStatusReport(output, `Workflow ${workflowId} in ${options.dir}`, report, planLine) + printStatusReport( + output, + `Workflow ${workflowId} in ${options.dir}`, + report, + planLine, + data ? workflowPlanFindings(data) : [] + ) setSoftExitCode(statusExitCode(status.state, login)) }) } diff --git a/packages/sim-cli/src/commands/protocol/workspace-source-status.test.ts b/packages/sim-cli/src/commands/protocol/workspace-source-status.test.ts index 68858293f3a..c88872ebf49 100644 --- a/packages/sim-cli/src/commands/protocol/workspace-source-status.test.ts +++ b/packages/sim-cli/src/commands/protocol/workspace-source-status.test.ts @@ -134,6 +134,24 @@ describe('workspaces source status', () => { expect(process.exitCode ?? 0).toBe(0) }) + it('lists the warnings its plan counts', async () => { + const warning = { + code: 'W_UNRESOLVED_REF', + severity: 'warning', + message: '$ref.nosuch.x names no block, so it does not resolve at run time.', + file: 'workflows/ingest/functions/f.js', + line: 1, + column: 8, + kind: 'workflow', + key: 'ingest', + } + mockRequest.mockResolvedValue(dryRun([], [warning])) + + await run('status') + + expect(printed()).toMatchObject({ plan: { warnings: 1 }, diagnostics: [warning] }) + }) + it('waits for no writer: a status while another command holds the directory exits 10, reading nothing', async () => { write('.sim/command.lock/owner.json', JSON.stringify({ pid: process.pid, host: hostname() })) mockRequest.mockResolvedValue(dryRun()) diff --git a/packages/sim-cli/src/commands/protocol/workspace-source-status.ts b/packages/sim-cli/src/commands/protocol/workspace-source-status.ts index 2bd45bf9cf5..58ea1ddd7cd 100644 --- a/packages/sim-cli/src/commands/protocol/workspace-source-status.ts +++ b/packages/sim-cli/src/commands/protocol/workspace-source-status.ts @@ -53,6 +53,7 @@ import { type NextStep, nextStep, pathFilter, + planFindings, printDiffReport, printStatusReport, type ServerCheck, @@ -63,6 +64,7 @@ import { unreachableReason, } from './source-status' import { withSourceExitCode } from './source-tree' +import type { Diagnostic } from './source-ui' import { destructiveChanges } from './workspace-source-apply' import { assertWorkspaceDir, @@ -220,13 +222,17 @@ function planAsApplied( ) } +/** The dry run's findings the plan counts: all but the server changes the status lists. */ +function workspacePlanFindings(data: ApplyData): Diagnostic[] { + return planFindings(data.diagnostics, isServerChangeFinding) +} + function planSummary(data: ApplyData, plan: readonly PlannedChange[]): WorkspacePlanSummary { const count = (...actions: string[]) => plan.filter((change) => actions.includes(change.action)).length + const planned = workspacePlanFindings(data) const findings = (severity: string) => - data.diagnostics.filter( - (finding) => finding.severity === severity && !isServerChangeFinding(finding.code) - ).length + planned.filter((finding) => finding.severity === severity).length return { create: count('create'), update: count('update', 'adopt'), @@ -401,7 +407,8 @@ export function attachWorkspaceSourceStatus(source: Command): void { output, `Workspace ${workspaceId ?? '(no base)'} in ${options.dir}`, report, - planLine + planLine, + data ? workspacePlanFindings(data) : [] ) setSoftExitCode(statusExitCode(status.state, login)) }, diff --git a/packages/sim-cli/src/contract/commands.ts b/packages/sim-cli/src/contract/commands.ts index 0502634cd7b..1d03f234e2c 100644 --- a/packages/sim-cli/src/contract/commands.ts +++ b/packages/sim-cli/src/contract/commands.ts @@ -62,7 +62,7 @@ const FILE_EDIT_HELP = * shape guessable, the same way `TABLE_FILTER_HELP` does for the predicate. */ const WORKFLOW_OPERATIONS_HELP = - 'Edits to apply, in a single batch, keyed by operation_type: [{"operation_type":"add","block_id":"my-fn","params":{"type":"function","name":"My Fn","inputs":{"code":"return {ok:true}"}}},{"operation_type":"edit","block_id":"","params":{"name":"Renamed","connections":{"success":"my-fn"}}},{"operation_type":"delete","block_id":""}]. Also extract_from_subflow, whose params carry {"subflowId":""}, and insert_into_subflow, which creates a block and so takes an add’s params plus that subflowId' + 'Edits to apply, in a single batch, keyed by operation_type: [{"operation_type":"add","block_id":"my-fn","params":{"type":"function","name":"My Fn","inputs":{"code":"return {ok:true}"}}},{"operation_type":"edit","block_id":"","params":{"name":"Renamed","connections":{"success":"my-fn"}}},{"operation_type":"delete","block_id":""}]. Also extract_from_subflow, whose params carry {"subflowId":""}, and insert_into_subflow, which creates a block and so takes an add’s params plus that subflowId. To move an existing block into a loop or parallel (keeping its id), edit it with params {"parentId":""}; {"parentId":null} moves it out' const WORKFLOW_SET_BLOCK_ENABLED_HELP = 'Blocks to enable or disable, applied after --operations: [{"block_id":"","enabled":false}]. Disabling a loop or parallel cascades to its unlocked descendants; enabling a block whose container is disabled is declined' const WORKFLOW_VARIABLE_OPERATIONS_HELP = @@ -697,6 +697,7 @@ export const CLI_CONTRACT: CliContract = { revertWorkflowVersion: { confirm: 'This overwrites the draft graph with the selected version and cannot be undone.', }, + deployWorkflow: { acceptsYes: true }, // A rollback is the deployed counterpart of that revert, and the more // consequential of the two: a revert only rewrites the draft, while this // changes which version production serves. Gating the draft write and not the diff --git a/packages/sim-cli/src/contract/types.ts b/packages/sim-cli/src/contract/types.ts index 09c451df619..779e1b1f251 100644 --- a/packages/sim-cli/src/contract/types.ts +++ b/packages/sim-cli/src/contract/types.ts @@ -333,6 +333,11 @@ export interface CommandSpec { * the point is that the caller can tell whether they meant it. */ confirm?: string + /** + * Accept `-y/--yes` without a `confirm` gate, as a no-op: a write callers run beside confirmed + * ones (a deploy after `workflows source apply --yes`) takes the flag they pass there. + */ + acceptsYes?: true /** * Discover table columns from inside this nested field as well as from the * row's own scalars. diff --git a/packages/sim-cli/src/generated/v2-api.ts b/packages/sim-cli/src/generated/v2-api.ts index f4c2aac3294..32de8c5040a 100644 --- a/packages/sim-cli/src/generated/v2-api.ts +++ b/packages/sim-cli/src/generated/v2-api.ts @@ -953,6 +953,7 @@ export type ApplyWorkflowSourceResponse = { | 'W_APPLY_CATALOG_MOVED' | 'W_APPLY_PROBABLE_RENAME' | 'E_APPLY_LINT' + | 'E_APPLY_UNRESOLVED_REF' | 'W_APPLY_LINT' | 'W_TABLE_UNKNOWN_COLUMN' | 'E_APPLY_INVALID_SCHEDULE' @@ -1439,6 +1440,7 @@ export type ApplyWorkspaceSourceResponse = { | 'W_APPLY_CATALOG_MOVED' | 'W_APPLY_PROBABLE_RENAME' | 'E_APPLY_LINT' + | 'E_APPLY_UNRESOLVED_REF' | 'W_APPLY_LINT' | 'W_TABLE_UNKNOWN_COLUMN' | 'E_APPLY_INVALID_SCHEDULE' @@ -1720,6 +1722,7 @@ export type ApplyWorkspaceSourceResponse = { | 'W_APPLY_CATALOG_MOVED' | 'W_APPLY_PROBABLE_RENAME' | 'E_APPLY_LINT' + | 'E_APPLY_UNRESOLVED_REF' | 'W_APPLY_LINT' | 'W_TABLE_UNKNOWN_COLUMN' | 'E_APPLY_INVALID_SCHEDULE' @@ -5381,6 +5384,7 @@ export type CreateWorkflowFromSourceResponse = { | 'W_APPLY_CATALOG_MOVED' | 'W_APPLY_PROBABLE_RENAME' | 'E_APPLY_LINT' + | 'E_APPLY_UNRESOLVED_REF' | 'W_APPLY_LINT' | 'W_TABLE_UNKNOWN_COLUMN' | 'E_APPLY_INVALID_SCHEDULE' @@ -10476,6 +10480,7 @@ export type GetWorkflowSourceResponse = { | 'W_APPLY_CATALOG_MOVED' | 'W_APPLY_PROBABLE_RENAME' | 'E_APPLY_LINT' + | 'E_APPLY_UNRESOLVED_REF' | 'W_APPLY_LINT' | 'W_TABLE_UNKNOWN_COLUMN' | 'E_APPLY_INVALID_SCHEDULE' @@ -11157,6 +11162,7 @@ export type GetWorkspaceSourceResponse = { | 'W_APPLY_CATALOG_MOVED' | 'W_APPLY_PROBABLE_RENAME' | 'E_APPLY_LINT' + | 'E_APPLY_UNRESOLVED_REF' | 'W_APPLY_LINT' | 'W_TABLE_UNKNOWN_COLUMN' | 'E_APPLY_INVALID_SCHEDULE' diff --git a/packages/sim-cli/src/runtime/build.test.ts b/packages/sim-cli/src/runtime/build.test.ts index 86a1bae54b6..97365419980 100644 --- a/packages/sim-cli/src/runtime/build.test.ts +++ b/packages/sim-cli/src/runtime/build.test.ts @@ -201,6 +201,18 @@ describe('commands parsed through commander', () => { }) }) + it.each(['--yes', '-y'])( + 'accepts %s on a deploy, which needs no confirmation, as other writes do', + async (flag) => { + const [path, options] = await run(['workflows', 'deploy', 'wf-1', flag], { + data: { isDeployed: true }, + }) + expect(path).toBe('/api/v2/workflows/wf-1/deploy') + expect(options.method).toBe('POST') + expect(options.body ?? {}).not.toHaveProperty('yes') + } + ) + it('carries a multi-word flag all the way to the request', async () => { // The regression: commander stores this as `minDurationMs`, so a lookup by // `min-duration-ms` found nothing and the filter never reached the API. diff --git a/packages/sim-cli/src/runtime/options.ts b/packages/sim-cli/src/runtime/options.ts index 1ee43fe68cf..0dce478b0df 100644 --- a/packages/sim-cli/src/runtime/options.ts +++ b/packages/sim-cli/src/runtime/options.ts @@ -359,5 +359,7 @@ export function addOperationOptions( ? 'Confirm this operation (required unless --dry-run)' : 'Confirm this operation (required)' ) + } else if (commandSpec.acceptsYes) { + command.option('-y, --yes', 'Accepted and ignored: this command needs no confirmation') } } diff --git a/packages/workflow-code/src/compile/grammar.test.ts b/packages/workflow-code/src/compile/grammar.test.ts index cb7792dac36..ba04ecf26a6 100644 --- a/packages/workflow-code/src/compile/grammar.test.ts +++ b/packages/workflow-code/src/compile/grammar.test.ts @@ -1104,6 +1104,44 @@ describe('errors, each with its code, location and hint', () => { expect(codes(compile({}))).toEqual(['W_UNRESOLVED_REF 8', 'W_UNRESOLVED_REF 27']) }) + it('names the probable new key of a block the lock binds that source renamed past recognition', () => { + const text = source( + [ + START, + "const fetcher = api('Fetch Page', { url: 'x' })", + "const f = fn('F', { code: codeFile('./fns/f.js') })", + ].join('\n') + ) + const lockBlock = (id: string, ident: string, order: number) => ({ + id, + parent: null, + ident, + order, + }) + const storedUnread = { + version: 1 as const, + blocks: { + start: lockBlock('b-start', 'start', 0), + lookup: lockBlock('b-lookup', 'lookup', 1), + f: lockBlock('b-f', 'f', 2), + }, + variables: {}, + } + const result = compileWorkflow({ + entry: { path: ENTRY, text }, + files: new Map([['workflows/fns/f.js', 'return $ref.lookup.data']]), + catalog: TEST_CATALOG, + sdkMajor: 1, + knownKeys: new Set(Object.keys(storedUnread.blocks)), + storedUnread, + }) + + const finding = result.diagnostics.find((d) => d.code === 'W_UNRESOLVED_REF') + expect(finding?.hint).toBe( + '$ref.lookup is a block this workflow had that workflow.ts no longer declares under that key: if Fetch Page is it renamed, write $ref.fetchpage.data.' + ) + }) + it('reports a syntax error inside a code file at its own location', () => { const result = run(source("const f = fn('F', { code: codeFile('./fns/bad.js') })"), { 'workflows/fns/bad.js': 'return (', diff --git a/packages/workflow-code/src/compile/index.ts b/packages/workflow-code/src/compile/index.ts index 06024dd4426..a005fa6ebc2 100644 --- a/packages/workflow-code/src/compile/index.ts +++ b/packages/workflow-code/src/compile/index.ts @@ -15,6 +15,7 @@ import type { LocatedDiagnostic, SourceSpan } from '../diagnostics/frame' import { jsonFieldText, type RefTypeScope, refValueKind } from '../json-fields' import { utf8Bytes } from '../limits' import { lockIdentRenames, type SourceHints, type WorkflowLockV1 } from '../lock' +import { renamedReferenceRewriter } from '../materialize/values' import type { AnchorKey, BlockKey, @@ -433,19 +434,42 @@ function emptyResult(reporter: Reporter, sdkMajor: number | null): CompileResult const BYTE_ORDER_MARK = '\uFEFF' /** - * Whether the stored workflow holds `value` in `param` of block `key` already: a JSON field the - * source left as it was printed, whose untyped references the edit did not write, so - * `W_JSON_REF_UNTYPED` is not repeated on every apply of a workflow it was saved with. + * The stored workflow as the source names it: `block` looks a block up by its source key (under + * that key in a stored model re-keyed by the renames, else under its stored key) and `holds` says + * whether a param holds `value` already, both through the block renames of the apply (`renamed`, + * stored key → source key), whose reference rewrites are no edit of the source. */ -function storedHolds( +interface StoredView { + block(key: BlockKey): CanonicalBlock | undefined + holds(key: BlockKey, param: string, value: string): boolean +} + +function storedView( stored: CanonicalWorkflow | undefined, - key: BlockKey, - param: string, - value: string -): boolean { - const held = stored && getOwn(stored.blocks, key) - const storedParam = held ? getOwn(held.params, param) : undefined - return storedParam?.kind === 'value' && storedParam.value === value + renamed: ReadonlyMap +): StoredView { + const storedKeyOf = new Map([...renamed].map(([from, to]) => [to, from])) + const rewrite = renamedReferenceRewriter([...renamed].map(([from, to]) => ({ from, to }))) + const block = (key: BlockKey) => { + if (!stored) return undefined + const storedKey = storedKeyOf.get(key) + return ( + getOwn(stored.blocks, key) ?? + (storedKey === undefined ? undefined : getOwn(stored.blocks, storedKey)) + ) + } + return { + block, + holds: (key, param, value) => { + const held = block(key) + const storedParam = held ? getOwn(held.params, param) : undefined + return ( + storedParam?.kind === 'value' && + typeof storedParam.value === 'string' && + rewrite(storedParam.value) === value + ) + }, + } } /** @@ -654,7 +678,9 @@ function compileEntry( const unreadRenames = unread ? lockIdentRenames(hints, unread, (key) => Object.hasOwn(unread.blocks, key)) : new Map() - checkCodeFileRefs(ctx, new Map([...unreadRenames, ...(input.renamedKeys ?? [])])) + const renamed = new Map([...unreadRenames, ...(input.renamedKeys ?? [])]) + checkCodeFileRefs(ctx, renamed) + const stored = storedView(input.stored, renamed) const refTypes: RefTypeScope = { catalog: input.catalog, @@ -672,11 +698,8 @@ function compileEntry( // An unchanged field stays quiet only while what it reads is too: a Function that now // returns text where it returned a number makes the same bare token break the JSON. if ( - storedHolds(input.stored, ref.blockKey, id, value) && - referencedResultKind( - ref.token, - (key) => input.stored && getOwn(input.stored.blocks, key) - ) === returned + stored.holds(ref.blockKey, id, value) && + referencedResultKind(ref.token, stored.block) === returned ) { continue } diff --git a/packages/workflow-code/src/compile/values.ts b/packages/workflow-code/src/compile/values.ts index 8461ab6a913..3f8a01f33c1 100644 --- a/packages/workflow-code/src/compile/values.ts +++ b/packages/workflow-code/src/compile/values.ts @@ -34,6 +34,7 @@ import { responseFormatFields, valueTree, } from '../json-fields' +import { SOURCE_ENTRY_FILE } from '../limits' import { type BlockKey, type CanonicalBlock, @@ -857,7 +858,9 @@ export function checkDynamicOutputRefs( * that names no block (a block's ident, a renamed block's old key, a typo) or a variable that does * not exist is `W_UNRESOLVED_REF`, and a path no output has is `W_UNKNOWN_OUTPUT_PATH`. Warnings, * not errors, because stored code may already hold a dangling reference and must round-trip; at - * run time such a token is spliced into the code as text, which fails as JavaScript. A head that + * run time such a token is spliced into the code as text, which fails as JavaScript, so the apply + * and an offline check refuse `W_UNRESOLVED_REF` as `E_APPLY_UNRESOLVED_REF` on code the edit + * changed. A head that * is the stored key of a block the source renamed (`renamed`, stored key → source key) resolves: * an apply rewrites it to the new key, so its path is checked against the renamed block. */ @@ -903,14 +906,18 @@ export function checkCodeFileRefs( const byIdent = ctx.declsByIdent.get(head.name)?.[0] const byName = ctx.declsByKey.get(normalizeWorkflowBlockName(head.name)) const suggested = byIdent ?? byName + const rewritten = (key: BlockKey) => + formatMemberPath(REF_ROOT, [{ kind: 'name', name: key }, ...rest]) ctx.reporter.report( 'W_UNRESOLVED_REF', `${written} names no block, so it does not resolve at run time.`, at, { hint: suggested - ? `${REF_ROOT} names a block by its key: write ${formatMemberPath(REF_ROOT, [{ kind: 'name', name: suggested.key }, ...rest])}.` - : () => didYouMean(head.name, ctx.declsByKey.keys(), (key) => `${REF_ROOT}.${key}`), + ? `${REF_ROOT} names a block by its key: write ${rewritten(suggested.key)}.` + : ctx.knownKeys.has(head.name) + ? formerBlockHint(ctx, head.name, renamed, rewritten) + : () => didYouMean(head.name, ctx.declsByKey.keys(), (key) => `${REF_ROOT}.${key}`), blockKey, } ) @@ -918,6 +925,30 @@ export function checkCodeFileRefs( } } +/** + * The hint for a code file `$ref` head that is the key of a block the workflow had (the lock or + * the stored workflow binds it) and source no longer declares: most often a block renamed in both + * name and const, whose references an apply rewrites only when it ties the block to the stored + * one. Names the one declared block nothing binds, when there is exactly one, as the probable new + * key (`rewritten` writes the reference with it). + */ +function formerBlockHint( + ctx: CompileContext, + formerKey: BlockKey, + renamed: ReadonlyMap, + rewritten: (key: BlockKey) => string +): string { + const renamedTo = new Set(renamed.values()) + const unbound = [...ctx.declsByKey.values()].filter( + (decl) => !ctx.knownKeys.has(decl.key) && !renamedTo.has(decl.key) + ) + const lead = `${REF_ROOT}.${formerKey} is a block this workflow had that ${SOURCE_ENTRY_FILE} no longer declares under that key` + const [only] = unbound + return unbound.length === 1 && only + ? `${lead}: if ${only.name} is it renamed, write ${rewritten(only.key)}.` + : `${lead}: if you renamed it, write ${REF_ROOT}., the display name lowercased with whitespace and dots removed.` +} + function subflowScopeRef( ctx: CompileContext, node: Node, diff --git a/packages/workflow-code/src/diagnostics/codes.ts b/packages/workflow-code/src/diagnostics/codes.ts index 6dd3ae5eb1d..d768acdb64e 100644 --- a/packages/workflow-code/src/diagnostics/codes.ts +++ b/packages/workflow-code/src/diagnostics/codes.ts @@ -455,6 +455,11 @@ const APPLY_DIAGNOSTIC_CODES = [ 'W_APPLY_PROBABLE_RENAME', /** A blocking workflow lint finding, mapped to a source line. */ 'E_APPLY_LINT', + /** + * A function code file `$ref` naming no block or variable in code the edit changed, which fails + * at run time; on code the stored workflow already holds it stays `W_UNRESOLVED_REF`. + */ + 'E_APPLY_UNRESOLVED_REF', /** A non-blocking workflow lint finding, mapped to a source line. */ 'W_APPLY_LINT', /** A table row write (insert, upsert, update) with a key that is not a column of its table. */ diff --git a/packages/workflow-code/src/index.ts b/packages/workflow-code/src/index.ts index 1cc4ce34d88..7711c3be87e 100644 --- a/packages/workflow-code/src/index.ts +++ b/packages/workflow-code/src/index.ts @@ -56,6 +56,7 @@ export { codeFrame, type Diagnostic, type DiagnosticFields, + type LocatedDiagnostic, type SourceSpan, splitLines, } from './diagnostics/frame' @@ -110,7 +111,7 @@ export type { } from './model' export { assignIdentifiers, printWorkflow } from './print' export { hasSuspiciousCharacter, printKey, quoteString } from './print/strings' -export { invertRecord, parseJsonContainer, setOrDelete, setOwn } from './records' +export { getOwn, invertRecord, parseJsonContainer, setOrDelete, setOwn } from './records' export { JS_RESERVED_WORDS, SDK_CORE_EXPORTS } from './sdk' export { holdsSecret } from './secrets' export { diff --git a/packages/workflow-code/src/json-fields.test.ts b/packages/workflow-code/src/json-fields.test.ts index 80f76840906..f749ea97517 100644 --- a/packages/workflow-code/src/json-fields.test.ts +++ b/packages/workflow-code/src/json-fields.test.ts @@ -231,6 +231,38 @@ describe('W_JSON_REF_UNTYPED on a Function block output', () => { expect(untyped(code, 'w', printed)).toHaveLength(1) }) + it('on an apply, stays quiet on an unchanged JSON field whose reference only a rename rewrote', () => { + const code = 'return { v: $ref.start.v }' + const printed = compile(code, 'v').workflow + if (!printed) throw new Error('the source did not compile') + const renamed = (stored: CanonicalWorkflow, renamedKeys: ReadonlyMap) => { + const text = `// @sim-sdk 1\n${FN_IMPORTS}\n\nexport default workflow('Test', {}, () => { + const start = trigger.start('Start') + const score = fn('Lead Score', { code: ${JSON.stringify(code)} }) + const next = api('Next', { url: 'https://x', body: { v: score.result.v } }) + start.to(score) + score.to(next) +})\n` + return compileWorkflow({ + entry: { path: 'workflow.ts', text }, + files: new Map(), + catalog: TEST_CATALOG, + sdkMajor: 1, + stored, + renamedKeys, + }).diagnostics.filter((d) => d.code === 'W_JSON_REF_UNTYPED') + } + + const { score, ...others } = printed.blocks + if (!score) throw new Error('the source has no score block') + const rekeyed = { ...printed, blocks: { ...others, leadscore: { ...score, key: 'leadscore' } } } + const renames = new Map([['score', 'leadscore']]) + + expect(renamed(printed, renames)).toEqual([]) + expect(renamed(rekeyed, renames)).toEqual([]) + expect(renamed(printed, new Map())).toHaveLength(1) + }) + it('on an apply, warns on an unchanged JSON field when the Function it reads now returns text there', () => { const printed = compile('return { v: 1 }', 'v').workflow if (!printed) throw new Error('the source did not compile') diff --git a/packages/workflow-code/src/materialize/index.test.ts b/packages/workflow-code/src/materialize/index.test.ts index 14ad4a7e684..5b93ac4ca59 100644 --- a/packages/workflow-code/src/materialize/index.test.ts +++ b/packages/workflow-code/src/materialize/index.test.ts @@ -19,7 +19,7 @@ import { workflowState, } from './fixtures.test-helpers' import { QUIRK_STATES } from './quirks.test-helpers' -import { rewriteRenamedReferences } from './values' +import { renamedReferenceRewriter, rewriteRenamedReferences } from './values' function run( workflow: CanonicalWorkflow, @@ -1065,6 +1065,43 @@ describe('materialize: preserved UI state comes from prev', () => { expect(subBlockValue(result.state, 's1', 'text')).toBe('saw ') }) + it.each([ + ['a chain', { fetch: 'report', report: 'summary' }, 'saw and '], + ['a swap', { fetch: 'report', report: 'fetch' }, 'saw and '], + ])( + 'rewrites a restored inactive value through %s of renames at once', + (_label, moves, expected) => { + const state = workflowState([ + blockState('a1', 'function', 'Fetch', { code: 'return 1' }), + blockState('a2', 'function', 'Report', { code: 'return 2' }), + blockState('s1', 'slack', 'Post', { + operation: 'read', + channel: 'C1', + text: 'saw and ', + }), + ]) + const lock = lockFor(state) + const renamed: Record = moves + const target = edit(canonical(state, lock).workflow, (draft) => { + const blocks = { ...draft.blocks } + draft.blocks = omit(draft.blocks, ['fetch', 'report']) + for (const [from, to] of Object.entries(renamed)) { + draft.blocks[to] = { ...blocks[from], key: to, name: to[0].toUpperCase() + to.slice(1) } + } + draft.blocks.post.params = omit(draft.blocks.post.params, ['text']) + }) + const result = run(target, lock, { + prev: state, + compiledIdents: Object.fromEntries( + Object.entries(renamed).map(([from, to]) => [to, lock.blocks[from].ident]) + ), + }) + expect(result.identity.blocks[renamed.fetch].id).toBe('a1') + expect(result.identity.blocks[renamed.report].id).toBe('a2') + expect(subBlockValue(result.state, 's1', 'text')).toBe(expected) + } + ) + it('clears a standalone advanced field that source emptied instead of restoring it', () => { const state = workflowState([ blockState('s1', 'slack', 'Post', { operation: 'send', text: 'hi', threadTs: '123.456' }), @@ -1186,3 +1223,19 @@ describe('rewriteRenamedReferences', () => { expect(tools?.kind === 'tools' && getOwn(tools.tools[0].params, '__proto__')).toBe('') }) }) + +describe('renamedReferenceRewriter', () => { + it('rewrites swapped and chained renames at once, never a token a rename already wrote', () => { + const swap = renamedReferenceRewriter([ + { from: 'a', to: 'b' }, + { from: 'b', to: 'a' }, + ]) + expect(swap(' ')).toBe(' ') + + const chain = renamedReferenceRewriter([ + { from: 'a', to: 'b' }, + { from: 'b', to: 'c' }, + ]) + expect(chain(' ')).toBe(' ') + }) +}) diff --git a/packages/workflow-code/src/materialize/index.ts b/packages/workflow-code/src/materialize/index.ts index a036eac2b1b..9b5a7ba48d9 100644 --- a/packages/workflow-code/src/materialize/index.ts +++ b/packages/workflow-code/src/materialize/index.ts @@ -27,7 +27,7 @@ import { type BranchHandles, writeEdges } from './edges' import { type IdentityEntry, type LockFreshness, resolveIdentity } from './identity' import { restorePreserved } from './preserve' import { loopEntry, parallelEntry } from './subflows' -import { rewriteRenamedReferences } from './values' +import { rewriteRenamedReferences, rewriteStoredBlockReferences } from './values' /** * Variables keep their ids by key: the lock's id, then prev's, else a new one. A variable @@ -195,12 +195,15 @@ export function materialize( }) const prevBlock = (prevKey: BlockKey | null) => prevKey !== null && prevCanonical ? getOwn(prevCanonical.workflow.blocks, prevKey) : undefined - // Rewrite after restoring: a restored inactive value can hold ` prevBlock(resolution.blocks.get(key)?.prevKey ?? null), + (key) => { + const stored = prevBlock(resolution.blocks.get(key)?.prevKey ?? null) + return stored && rewriteStoredBlockReferences(stored, resolution.renames) + }, catalog ), resolution.renames diff --git a/packages/workflow-code/src/materialize/values.ts b/packages/workflow-code/src/materialize/values.ts index 96a2663195c..0a08e62cb25 100644 --- a/packages/workflow-code/src/materialize/values.ts +++ b/packages/workflow-code/src/materialize/values.ts @@ -174,9 +174,35 @@ function rewriteBlock(block: CanonicalBlock, rewrite: (text: string) => string): } /** - * Rewrites ` +): (text: string) => string { + const targets = new Map() + for (const { from, to } of renames) { + const key = from.toLowerCase() + if (!targets.has(key)) targets.set(key, to) + } + if (targets.size === 0) return (text) => text + const pattern = new RegExp(`<(${[...targets.keys()].map(escapeRegExp).join('|')})\\.`, 'gi') + return (text) => + text.includes('<') + ? text.replace(pattern, (token, from: string) => { + const to = targets.get(from.toLowerCase()) + return to === undefined ? token : `<${to}.` + }) + : text +} + +/** + * Rewrites ` !Object.hasOwn(workflow.blocks, from)) if (active.length === 0) return workflow - const patterns = active.map(({ from, to }) => ({ - pattern: new RegExp(`<${escapeRegExp(from)}\\.`, 'gi'), - replacement: `<${to}.`, - })) - const rewrite = (text: string): string => - text.includes('<') - ? patterns.reduce((acc, { pattern, replacement }) => acc.replace(pattern, replacement), text) - : text + const rewrite = renamedReferenceRewriter(active) const blocks: CanonicalWorkflow['blocks'] = {} for (const [key, block] of Object.entries(workflow.blocks)) { setOwn(blocks, key, rewriteBlock(block, rewrite)) } return { ...workflow, blocks } } + +/** + * `block` (a stored block, whose text names blocks by their stored keys) with every ` +): CanonicalBlock { + return renames.length === 0 ? block : rewriteBlock(block, renamedReferenceRewriter(renames)) +}