diff --git a/apps/sim/lib/workflows/application/read-workflow-lint-tables.test.ts b/apps/sim/lib/workflows/application/read-workflow-lint-tables.test.ts index 6608a6ae858..960ebe70a0e 100644 --- a/apps/sim/lib/workflows/application/read-workflow-lint-tables.test.ts +++ b/apps/sim/lib/workflows/application/read-workflow-lint-tables.test.ts @@ -41,6 +41,7 @@ vi.mock('@/lib/workflows/editing/validation', () => ({ import { OrchestrationError } from '@/lib/core/orchestration/types' import { readWorkflowLint } from '@/lib/workflows/application/read-workflow-lint' import { TableBlock } from '@/blocks/blocks/table' +import { TableV2Block } from '@/blocks/blocks/table_v2' const mocks = { snapshot: workflowsQueriesMockFns.mockLoadWorkflowReadSnapshot, @@ -236,6 +237,148 @@ describe('standalone table diagnostics against the actual Table block', () => { expect(result.tableFieldIssues).toEqual([]) }) + it('checks literal row keys around bare references that resolve to JSON at runtime', async () => { + setGraph( + block('write', { + operation: 'insert_row', + data: '{"name": , "missing": , "note": "<3 {{SUFFIX}}", "env": {{N}}}', + }) + ) + const result = await lint() + expect(result.notes.filter((note) => note.includes('could not complete'))).toEqual([]) + expect( + result.unresolvedReferences.filter((reference) => reference.kind === 'resource') + ).toEqual([ + expect.objectContaining({ + blockId: 'write', + field: 'data', + value: ['missing', 'note', 'env'], + }), + ]) + }) + + describe('with the registered TableV2Block predicate grammar', () => { + beforeEach(() => { + mockGetBlock.mockImplementation((type: string) => + type === 'table_v2' ? TableV2Block : undefined + ) + }) + + it('checks literal predicate fields around a bare reference value', async () => { + setGraph( + block('query', { + filter: + '{"all":[{"field":"name","op":"eq","value":},{"field":"other","op":"gt","value":}]}', + }) + ) + const result = await lint() + expect(result.notes.filter((note) => note.includes('could not complete'))).toEqual([]) + expect(result.tableFieldIssues.map(({ field }) => field)).toEqual(['other']) + }) + + it('checks predicate fields around an in-list reference', async () => { + setGraph( + block('query', { + filter: + '{"all":[{"field":"other","op":"in","value":},{"field":"name","op":"eq","value":}]}', + }) + ) + const result = await lint() + expect(result.notes.filter((note) => note.includes('could not complete'))).toEqual([]) + expect(result.tableFieldIssues.map(({ field }) => field)).toEqual(['other']) + }) + + it('notes a whole-field filter reference as runtime-resolved and still checks row data', async () => { + setGraph( + block('write', { + operation: 'update_rows_by_filter', + bulkFilterMode: 'json', + filter: '', + data: '{"name":"Ada","missing":1}', + }) + ) + const result = await lint() + expect(result.notes.filter((note) => note.includes('could not complete'))).toEqual([]) + expect(result.notes).toContain( + 'Table inputs in block "write" depend on references resolved at runtime and were not checked: filterInput.' + ) + expect( + result.unresolvedReferences.filter((reference) => reference.kind === 'resource') + ).toEqual([expect.objectContaining({ field: 'data', value: ['missing'] })]) + }) + + it('defers a filter with more mixed references than the placeholder search covers', async () => { + const conditions = [ + ...['a', 'b', 'c', 'd'].map((name) => `{"field":"name","op":"eq","value":}`), + ...['e', 'f', 'g'].map((name) => `{"field":"name","op":"in","value":}`), + ] + setGraph( + block('write', { + operation: 'update_rows_by_filter', + bulkFilterMode: 'json', + filter: `{"all":[${conditions.join(',')}]}`, + data: '{"name":"Ada","missing":1}', + }) + ) + const result = await lint() + expect(result.notes.filter((note) => note.includes('could not complete'))).toEqual([]) + expect(result.notes).toContain( + 'Table inputs in block "write" depend on references resolved at runtime and were not checked: filterInput.' + ) + expect( + result.unresolvedReferences.filter((reference) => reference.kind === 'resource') + ).toEqual([expect.objectContaining({ field: 'data', value: ['missing'] })]) + }) + + it('still reports an invalid literal condition beside a reference', async () => { + setGraph( + block('query', { + filter: + '{"all":[{"field":"name","op":"eq","value":},{"field":"name","op":"in","value":"not a list"}]}', + }) + ) + const result = await lint() + expect(result.notes).toEqual( + expect.arrayContaining([ + expect.stringContaining( + 'Table input checks in block "query" could not complete before runtime:' + ), + ]) + ) + }) + }) + + it.each([ + ['data', 'insert_row', '', 'Table row "write".data is not a static object'], + [ + 'rows', + 'batch_insert_rows', + '', + 'Table rows in block "write" are not a static array', + ], + ['rows', 'batch_insert_rows', '[, {"name":"Ada"}]', 'Table row "write".rows[0]'], + ])( + 'notes a %s value that is a bare reference instead of a failed check (%s, %s)', + async (field, operation, value, note) => { + setGraph(block('write', { operation, [field]: value })) + const result = await lint() + expect(result.notes.filter((entry) => entry.includes('could not complete'))).toEqual([]) + expect(result.notes).toEqual(expect.arrayContaining([expect.stringContaining(note)])) + } + ) + + it('still reports malformed literal JSON that contains a reference', async () => { + setGraph(block('write', { operation: 'insert_row', data: '{"name": ,}' })) + const result = await lint() + expect(result.notes).toEqual( + expect.arrayContaining([ + expect.stringContaining( + 'Table input checks in block "write" could not complete before runtime: Invalid JSON in Row Data' + ), + ]) + ) + }) + it('reports a query object whose entire value requires runtime resolution', async () => { setGraph(block('query', { filter: '""' })) const result = await lint() diff --git a/apps/sim/lib/workflows/application/read-workflow-lint.ts b/apps/sim/lib/workflows/application/read-workflow-lint.ts index ec17565104b..7bda45a2750 100644 --- a/apps/sim/lib/workflows/application/read-workflow-lint.ts +++ b/apps/sim/lib/workflows/application/read-workflow-lint.ts @@ -1,6 +1,7 @@ import { requirePrincipalSubjectUserId } from '@sim/auth/principal' import { getErrorMessage } from '@sim/utils/errors' import { isPlainRecord } from '@sim/utils/object' +import { findWorkflowReferenceTokens } from '@sim/utils/workflow-references' import type { CursorKey } from '@/lib/api/list-query' import { bindCopilotWorkspaceOperation } from '@/lib/core/application/copilot-workspace-invocation' import type { PrincipalForOperation } from '@/lib/core/application/workspace-operation' @@ -254,6 +255,97 @@ function hasRuntimeReference(value: string): boolean { return createReferencePattern().test(value) || createEnvVarPattern().test(value) } +/** + * Outside Function code a reference resolves to JSON text (objects, arrays, numbers, booleans), so + * `{"name": }` or a whole-field `` is valid JSON at runtime but not before. + * Each reference written outside a JSON string is replaced by a placeholder so the Table block's + * own transform reads the literal parts: a quoted string, or a one-item list for slots such as a + * filter `in`, trying every combination. A field that is a single whole reference has no literal + * part, so if no combination fits it is left out and returned in `runtimeFields`. When the search + * is exhaustive and still fails, the error comes from the literal parts (malformed JSON, an + * invalid literal condition) and is rethrown. Past the search bound that cannot be told apart, so + * every reference-bearing field is left out instead and the other inputs are still checked. Only + * this lint-local copy is rewritten; references to string outputs left unquoted are reported by + * `collectUnquotedJsonStringReferences`. + */ +function normalizeTableInputs( + transform: (params: Record) => Record, + params: Record +): { normalized: Record; runtimeFields: string[] } { + const referenceFields: string[] = [] + const wholeReferenceFields: string[] = [] + for (const [key, value] of Object.entries(params)) { + if (typeof value !== 'string') continue + const trimmed = value.trim() + const tokens = findWorkflowReferenceTokens(trimmed) + if (tokens.length === 0) continue + referenceFields.push(key) + if (tokens.length === 1 && tokens[0].start === 0 && tokens[0].end === trimmed.length) { + wholeReferenceFields.push(key) + } + } + if (referenceFields.length === 0) return { normalized: transform(params), runtimeFields: [] } + const tiers = wholeReferenceFields.length > 0 ? [[], wholeReferenceFields] : [[]] + let searchBounded = false + let firstError: unknown + for (const runtimeFields of tiers) { + const withPlaceholders = (asList: (ordinal: number) => boolean) => { + let ordinal = 0 + const candidate: Record = {} + for (const [key, value] of Object.entries(params)) { + if (runtimeFields.includes(key)) continue + candidate[key] = + typeof value === 'string' + ? replaceBareReferences(value, (reference) => + asList(ordinal++) ? `[${JSON.stringify(reference)}]` : JSON.stringify(reference) + ) + : value + } + return { candidate, count: ordinal } + } + const { count } = withPlaceholders(() => false) + if (count > MAX_PLACEHOLDER_SEARCH_REFERENCES) { + searchBounded = true + continue + } + for (let mask = 0; mask < 2 ** count; mask++) { + try { + const { candidate } = withPlaceholders((ordinal) => ((mask >> ordinal) & 1) === 1) + return { normalized: transform(candidate), runtimeFields } + } catch (error) { + firstError ??= error + } + } + } + if (!searchBounded) throw firstError + const literal = Object.fromEntries( + Object.entries(params).filter(([key]) => !referenceFields.includes(key)) + ) + return { normalized: transform(literal), runtimeFields: referenceFields } +} + +/** Bounds the placeholder search to 2^6 pure transform calls per Table block. */ +const MAX_PLACEHOLDER_SEARCH_REFERENCES = 6 + +function replaceBareReferences(json: string, placeholder: (reference: string) => string): string { + const tokens = findWorkflowReferenceTokens(json) + if (tokens.length === 0) return json + let result = '' + let cursor = 0 + let inString = false + for (const token of tokens) { + for (let index = cursor; index < token.start; index++) { + const char = json[index] + if (inString && char === '\\') index++ + else if (char === '"') inString = !inString + } + result += json.slice(cursor, token.start) + result += inString ? token.value : placeholder(token.value) + cursor = token.end + } + return result + json.slice(cursor) +} + /** Read each active table once; column checks consume the Table block's own pure input transform. */ async function readTableDiagnostics( graph: ReadWorkflowGraphResult, @@ -320,7 +412,13 @@ async function readTableDiagnostics( if (!transform) throw new Error('Table input normalization is unavailable') let normalized: Record try { - normalized = transform(params) + const inputs = normalizeTableInputs(transform, params) + normalized = inputs.normalized + if (inputs.runtimeFields.length > 0) { + result.notes.push( + `Table inputs in block "${block.name || blockId}" depend on references resolved at runtime and were not checked: ${inputs.runtimeFields.join(', ')}.` + ) + } } catch (error) { result.notes.push( `Table input checks in block "${block.name || blockId}" could not complete before runtime: ${getErrorMessage(error)}`