Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions apps/sim/lib/workflows/application/read-workflow-lint-tables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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": <start.name>, "missing": <start.count>, "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":<start.name>},{"field":"other","op":"gt","value":<start.n>}]}',
})
)
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":<start.names>},{"field":"name","op":"eq","value":<start.name>}]}',
})
)
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: '<start.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":<start.${name}>}`),
...['e', 'f', 'g'].map((name) => `{"field":"name","op":"in","value":<start.${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('still reports an invalid literal condition beside a reference', async () => {
setGraph(
block('query', {
filter:
'{"all":[{"field":"name","op":"eq","value":<start.n>},{"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', '<start.row>', 'Table row "write".data is not a static object'],
[
'rows',
'batch_insert_rows',
'<start.rows>',
'Table rows in block "write" are not a static array',
],
['rows', 'batch_insert_rows', '[<start.first>, {"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": <start.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: '"<start.filter>"' }))
const result = await lint()
Expand Down
100 changes: 99 additions & 1 deletion apps/sim/lib/workflows/application/read-workflow-lint.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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": <start.name>}` or a whole-field `<start.row>` 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<string, unknown>) => Record<string, unknown>,
params: Record<string, unknown>
): { normalized: Record<string, unknown>; 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<string, unknown> = {}
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,
Expand Down Expand Up @@ -320,7 +412,13 @@ async function readTableDiagnostics(
if (!transform) throw new Error('Table input normalization is unavailable')
let normalized: Record<string, unknown>
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)}`
Expand Down
Loading