From d735c78af7fa1692c3c18b3318a484d3c8e95739 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Tue, 17 Nov 2026 21:16:52 -0800 Subject: [PATCH 1/3] fix(lint): replace table placeholder search with structural placeholders Quote every bare reference, turn in/nin condition values into one-item lists and drop whole-condition references from all/any groups, then run the Table transform once. Only if that fails are whole-reference fields left out for one final attempt, whose error is reported. This removes the reference-count bound that skipped row data with many references, blames the literal error rather than a deferred field, and stops deferred row data from being called non-static. --- .../read-workflow-lint-tables.test.ts | 70 ++++++++-- .../application/read-workflow-lint.ts | 123 +++++++++--------- 2 files changed, 123 insertions(+), 70 deletions(-) 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 960ebe70a0e..0feb5785eaa 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 @@ -307,27 +307,73 @@ describe('standalone table diagnostics against the actual Table block', () => { ).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":}`), - ] + const manyReferenceData = `{${['a', 'b', 'c', 'd', 'e', 'f', 'g'] + .map((name) => `"name": `) + .join(',')},"missing":1}` + + it.each([ + ['insert_row', manyReferenceData, undefined], + [ + 'update_rows_by_filter', + manyReferenceData, + '{"all":[{"field":"name","op":"eq","value":},{"field":"name","op":"in","value":}]}', + ], + ])( + 'checks literal row keys beside many bare references (%s)', + async (operation, data, filter) => { + setGraph(block('write', { operation, bulkFilterMode: 'json', data, filter })) + 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({ field: 'data', value: ['missing'] })]) + } + ) + + it('checks the literal conditions beside a whole-condition reference', async () => { + setGraph( + block('query', { + filter: '{"all":[,{"field":"other","op":"eq","value":1}]}', + }) + ) + const result = await lint() + expect(result.notes.filter((note) => note.includes('could not complete'))).toEqual([]) + expect(result.tableFieldIssues.map(({ field }) => field)).toEqual(['other']) + }) + + it('blames malformed row data, not a whole-field filter reference', async () => { setGraph( block('write', { operation: 'update_rows_by_filter', bulkFilterMode: 'json', - filter: `{"all":[${conditions.join(',')}]}`, - data: '{"name":"Ada","missing":1}', + filter: '', + data: '{"name":"Ada",}', + }) + ) + 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('does not call deferred whole-field row data a non-static object', async () => { + setGraph( + block('write', { + operation: 'update_rows_by_filter', + bulkFilterMode: 'json', + filter: '', + data: '', }) ) 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.' + 'Table inputs in block "write" depend on references resolved at runtime and were not checked: data, filterInput.' ) - expect( - result.unresolvedReferences.filter((reference) => reference.kind === 'resource') - ).toEqual([expect.objectContaining({ field: 'data', value: ['missing'] })]) + expect(result.notes.filter((note) => note.includes('not a static object'))).toEqual([]) }) it('still reports an invalid literal condition beside a reference', async () => { diff --git a/apps/sim/lib/workflows/application/read-workflow-lint.ts b/apps/sim/lib/workflows/application/read-workflow-lint.ts index 7bda45a2750..40c133a0a0f 100644 --- a/apps/sim/lib/workflows/application/read-workflow-lint.ts +++ b/apps/sim/lib/workflows/application/read-workflow-lint.ts @@ -258,74 +258,77 @@ function hasRuntimeReference(value: string): boolean { /** * 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`. + * Each reference written outside a JSON string becomes a quoted placeholder so the Table block's + * own transform reads the literal parts; filter slots that need a list or a condition are adapted + * by {@link adaptReferencePlaceholders}. If the transform still fails and a field is one whole + * reference, that field has no literal part, so it is left out, returned in `runtimeFields`, and + * the transform runs once more; an error from that final attempt comes from the literal parts and + * is rethrown. 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[] = [] + const prepared: Record = {} + const runtimeFields: string[] = [] for (const [key, value] of Object.entries(params)) { - if (typeof value !== 'string') continue + if (typeof value !== 'string') { + prepared[key] = value + continue + } + prepared[key] = withReferencePlaceholders(value) 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) + runtimeFields.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 - } - } + try { + return { normalized: transform(prepared), runtimeFields: [] } + } catch (error) { + if (runtimeFields.length === 0) throw error } - if (!searchBounded) throw firstError - const literal = Object.fromEntries( - Object.entries(params).filter(([key]) => !referenceFields.includes(key)) - ) - return { normalized: transform(literal), runtimeFields: referenceFields } + for (const key of runtimeFields) delete prepared[key] + return { normalized: transform(prepared), runtimeFields } } -/** Bounds the placeholder search to 2^6 pure transform calls per Table block. */ -const MAX_PLACEHOLDER_SEARCH_REFERENCES = 6 +function withReferencePlaceholders(json: string): string { + const references = new Set() + const quoted = replaceBareReferences(json, (reference) => { + references.add(reference) + return JSON.stringify(reference) + }) + if (references.size === 0) return quoted + try { + return JSON.stringify(adaptReferencePlaceholders(JSON.parse(quoted), references)) + } catch { + return quoted + } +} + +/** + * A quoted placeholder fits row values and most filter values. The value of an `in`/`nin` + * condition must be a list, so it becomes a one-item list, and a whole condition inside an + * `all`/`any` group has no literal part, so it is left out while the group keeps a literal member. + */ +function adaptReferencePlaceholders(node: unknown, references: Set): unknown { + const isPlaceholder = (value: unknown) => typeof value === 'string' && references.has(value) + if (Array.isArray(node)) return node.map((item) => adaptReferencePlaceholders(item, references)) + if (!isPlainRecord(node)) return node + const adapted: Record = {} + for (const [key, value] of Object.entries(node)) { + if ((key === 'all' || key === 'any') && Array.isArray(value)) { + const literal = value.filter((item) => !isPlaceholder(item)) + adapted[key] = adaptReferencePlaceholders(literal.length > 0 ? literal : value, references) + } else if (key === 'value' && (node.op === 'in' || node.op === 'nin') && isPlaceholder(value)) { + adapted[key] = [value] + } else { + adapted[key] = adaptReferencePlaceholders(value, references) + } + } + return adapted +} function replaceBareReferences(json: string, placeholder: (reference: string) => string): string { const tokens = findWorkflowReferenceTokens(json) @@ -411,12 +414,14 @@ async function readTableDiagnostics( const transform = getBlock(block.type)?.tools.config?.params if (!transform) throw new Error('Table input normalization is unavailable') let normalized: Record + let runtimeFields: string[] try { const inputs = normalizeTableInputs(transform, params) normalized = inputs.normalized - if (inputs.runtimeFields.length > 0) { + runtimeFields = inputs.runtimeFields + if (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(', ')}.` + `Table inputs in block "${block.name || blockId}" depend on references resolved at runtime and were not checked: ${runtimeFields.join(', ')}.` ) } } catch (error) { @@ -464,8 +469,10 @@ async function readTableDiagnostics( }) } } - if (Object.hasOwn(normalized, 'data')) inspectRow(normalized.data, 'data') - if (Object.hasOwn(normalized, 'rows')) { + if (Object.hasOwn(normalized, 'data') && !runtimeFields.includes('data')) { + inspectRow(normalized.data, 'data') + } + if (Object.hasOwn(normalized, 'rows') && !runtimeFields.includes('rows')) { if (Array.isArray(normalized.rows)) { normalized.rows.forEach((row, index) => inspectRow(row, `rows[${index}]`)) } else { From 62d11dd1801fcea5fd641d48d60d7060898347e5 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Tue, 17 Nov 2026 21:29:37 -0800 Subject: [PATCH 2/3] fix(lint): track bare reference occurrences and defer reference-only filter groups Give each bare reference occurrence its own placeholder so a quoted value with the same text is left unchanged, rebuild objects with Object.fromEntries so a __proto__ row key survives, and treat a filter whose root group holds only references as runtime-only so the remaining inputs are still checked. --- .../read-workflow-lint-tables.test.ts | 50 ++++++++++ .../application/read-workflow-lint.ts | 95 +++++++++++-------- 2 files changed, 104 insertions(+), 41 deletions(-) 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 0feb5785eaa..ec60193bb0b 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 @@ -341,6 +341,56 @@ describe('standalone table diagnostics against the actual Table block', () => { expect(result.tableFieldIssues.map(({ field }) => field)).toEqual(['other']) }) + it('defers a filter group made only of references and still checks row data', async () => { + const conditions = ['a', 'b', 'c', 'd', 'e', 'f', 'g'].map((name) => ``) + 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('reports a quoted in-list value that matches a bare reference elsewhere', async () => { + setGraph( + block('query', { + filter: + '{"all":[{"field":"name","op":"eq","value":},{"field":"name","op":"in","value":""}]}', + }) + ) + const result = await lint() + expect(result.notes).toEqual( + expect.arrayContaining([ + expect.stringContaining( + 'Table input checks in block "query" could not complete before runtime:' + ), + ]) + ) + }) + + it('keeps a __proto__ row key beside a bare reference', async () => { + setGraph( + block('write', { + operation: 'insert_row', + data: '{"name":,"__proto__":"extra"}', + }) + ) + const result = await lint() + expect( + result.unresolvedReferences.filter((reference) => reference.kind === 'resource') + ).toEqual([expect.objectContaining({ field: 'data', value: ['__proto__'] })]) + }) + it('blames malformed row data, not a whole-field filter reference', async () => { setGraph( block('write', { diff --git a/apps/sim/lib/workflows/application/read-workflow-lint.ts b/apps/sim/lib/workflows/application/read-workflow-lint.ts index 40c133a0a0f..210ed2f245d 100644 --- a/apps/sim/lib/workflows/application/read-workflow-lint.ts +++ b/apps/sim/lib/workflows/application/read-workflow-lint.ts @@ -1,5 +1,6 @@ import { requirePrincipalSubjectUserId } from '@sim/auth/principal' import { getErrorMessage } from '@sim/utils/errors' +import { generateShortId } from '@sim/utils/id' import { isPlainRecord } from '@sim/utils/object' import { findWorkflowReferenceTokens } from '@sim/utils/workflow-references' import type { CursorKey } from '@/lib/api/list-query' @@ -259,12 +260,11 @@ function hasRuntimeReference(value: string): boolean { * 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 becomes a quoted placeholder so the Table block's - * own transform reads the literal parts; filter slots that need a list or a condition are adapted - * by {@link adaptReferencePlaceholders}. If the transform still fails and a field is one whole - * reference, that field has no literal part, so it is left out, returned in `runtimeFields`, and - * the transform runs once more; an error from that final attempt comes from the literal parts and - * is rethrown. Only this lint-local copy is rewritten; references to string outputs left unquoted - * are reported by `collectUnquotedJsonStringReferences`. + * own transform reads the literal parts (see {@link withReferencePlaceholders}). If the transform + * still fails and a field has no literal part, that field is left out, returned in + * `runtimeFields`, and the transform runs once more; an error from that final attempt comes from + * the literal parts and is rethrown. Only this lint-local copy is rewritten; references to string + * outputs left unquoted are reported by `collectUnquotedJsonStringReferences`. */ function normalizeTableInputs( transform: (params: Record) => Record, @@ -277,12 +277,9 @@ function normalizeTableInputs( prepared[key] = value continue } - prepared[key] = withReferencePlaceholders(value) - const trimmed = value.trim() - const tokens = findWorkflowReferenceTokens(trimmed) - if (tokens.length === 1 && tokens[0].start === 0 && tokens[0].end === trimmed.length) { - runtimeFields.push(key) - } + const input = withReferencePlaceholders(value) + prepared[key] = input.value + if (input.runtimeOnly) runtimeFields.push(key) } try { return { normalized: transform(prepared), runtimeFields: [] } @@ -293,41 +290,57 @@ function normalizeTableInputs( return { normalized: transform(prepared), runtimeFields } } -function withReferencePlaceholders(json: string): string { - const references = new Set() +/** + * Quotes each bare reference occurrence under its own placeholder, so an already-quoted string + * with the same text is never mistaken for one. A quoted reference fits row values and most + * filter values; the value of an `in`/`nin` condition must be a list, so it becomes a one-item + * list, and a condition or group made only of references has no literal part, so it is left out + * of an `all`/`any` group that keeps a literal member. `runtimeOnly` marks a field whose whole + * value is such a reference or group. + */ +function withReferencePlaceholders(json: string): { value: string; runtimeOnly: boolean } { + const nonce = generateShortId() + const references = new Map() const quoted = replaceBareReferences(json, (reference) => { - references.add(reference) - return JSON.stringify(reference) + const placeholder = `${nonce}:${references.size}` + references.set(placeholder, reference) + return JSON.stringify(placeholder) }) - if (references.size === 0) return quoted + if (references.size === 0) return { value: json, runtimeOnly: false } + let parsed: unknown try { - return JSON.stringify(adaptReferencePlaceholders(JSON.parse(quoted), references)) + parsed = JSON.parse(quoted) } catch { - return quoted + const readable = replaceBareReferences(json, (reference) => JSON.stringify(reference)) + return { value: readable, runtimeOnly: false } } -} - -/** - * A quoted placeholder fits row values and most filter values. The value of an `in`/`nin` - * condition must be a list, so it becomes a one-item list, and a whole condition inside an - * `all`/`any` group has no literal part, so it is left out while the group keeps a literal member. - */ -function adaptReferencePlaceholders(node: unknown, references: Set): unknown { - const isPlaceholder = (value: unknown) => typeof value === 'string' && references.has(value) - if (Array.isArray(node)) return node.map((item) => adaptReferencePlaceholders(item, references)) - if (!isPlainRecord(node)) return node - const adapted: Record = {} - for (const [key, value] of Object.entries(node)) { - if ((key === 'all' || key === 'any') && Array.isArray(value)) { - const literal = value.filter((item) => !isPlaceholder(item)) - adapted[key] = adaptReferencePlaceholders(literal.length > 0 ? literal : value, references) - } else if (key === 'value' && (node.op === 'in' || node.op === 'nin') && isPlaceholder(value)) { - adapted[key] = [value] - } else { - adapted[key] = adaptReferencePlaceholders(value, references) - } + const isPlaceholder = (node: unknown): node is string => + typeof node === 'string' && references.has(node) + const isDynamic = (node: unknown): boolean => { + if (isPlaceholder(node)) return true + if (!isPlainRecord(node)) return false + const members = node.all ?? node.any + return Array.isArray(members) && members.length > 0 && members.every(isDynamic) + } + const adapt = (node: unknown): unknown => { + if (isPlaceholder(node)) return references.get(node) + if (Array.isArray(node)) return node.map(adapt) + if (!isPlainRecord(node)) return node + return Object.fromEntries( + Object.entries(node).map(([key, value]) => { + const name = references.get(key) ?? key + if ((key === 'all' || key === 'any') && Array.isArray(value)) { + const literal = value.filter((member) => !isDynamic(member)) + return [name, adapt(literal.length > 0 ? literal : value)] + } + if (key === 'value' && (node.op === 'in' || node.op === 'nin') && isPlaceholder(value)) { + return [name, [adapt(value)]] + } + return [name, adapt(value)] + }) + ) } - return adapted + return { value: JSON.stringify(adapt(parsed)), runtimeOnly: isDynamic(parsed) } } function replaceBareReferences(json: string, placeholder: (reference: string) => string): string { From d3129d1961f571476b24b216d1816b3c88949b12 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Tue, 17 Nov 2026 21:39:03 -0800 Subject: [PATCH 3/3] fix(lint): treat only single-key all/any objects as reference-only filter groups --- .../application/read-workflow-lint-tables.test.ts | 15 +++++++++++++++ .../workflows/application/read-workflow-lint.ts | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) 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 ec60193bb0b..bc6225c7d3d 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 @@ -361,6 +361,21 @@ describe('standalone table diagnostics against the actual Table block', () => { ).toEqual([expect.objectContaining({ field: 'data', value: ['missing'] })]) }) + it('checks row data whose keys resemble a reference-only filter group', async () => { + setGraph( + block('write', { + operation: 'update_rows_by_filter', + bulkFilterMode: 'json', + filter: '', + data: '{"all":[],"missing":1}', + }) + ) + const result = await lint() + expect( + result.unresolvedReferences.filter((reference) => reference.kind === 'resource') + ).toEqual([expect.objectContaining({ field: 'data', value: ['all', 'missing'] })]) + }) + it('reports a quoted in-list value that matches a bare reference elsewhere', async () => { setGraph( block('query', { diff --git a/apps/sim/lib/workflows/application/read-workflow-lint.ts b/apps/sim/lib/workflows/application/read-workflow-lint.ts index 210ed2f245d..ee9f9175207 100644 --- a/apps/sim/lib/workflows/application/read-workflow-lint.ts +++ b/apps/sim/lib/workflows/application/read-workflow-lint.ts @@ -318,7 +318,7 @@ function withReferencePlaceholders(json: string): { value: string; runtimeOnly: typeof node === 'string' && references.has(node) const isDynamic = (node: unknown): boolean => { if (isPlaceholder(node)) return true - if (!isPlainRecord(node)) return false + if (!isPlainRecord(node) || Object.keys(node).length !== 1) return false const members = node.all ?? node.any return Array.isArray(members) && members.length > 0 && members.every(isDynamic) }