Skip to content

Commit fc69dd4

Browse files
committed
fix(table): fail a tool call whose v1 builder rules are corrupt
A pinned v1 builder value that does not decode to an array iterated as a string, built nothing, and fell back to the model's filter. On a tool call it now fails the call; an empty rule list keeps its model fallback. Also types the new test helper params.
1 parent 3a75733 commit fc69dd4

2 files changed

Lines changed: 30 additions & 10 deletions

File tree

‎apps/sim/blocks/blocks/table.ts‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,21 @@ interface ParsedParams {
9595
* list) falls back to the model's `filter`/`sort` arg instead of discarding it; the canvas keeps
9696
* the builder's result.
9797
*/
98-
function resolveBuilderOrJson(
98+
function resolveBuilderOrJson<TRule>(
9999
builderActive: boolean,
100-
fromBuilder: () => unknown,
100+
builderValue: unknown,
101+
fromBuilder: (rules: TRule[]) => unknown,
101102
json: unknown,
102103
fieldName: string,
103104
context: BlockToolParamsContext | undefined
104105
): unknown {
105106
if (builderActive) {
106-
const built = fromBuilder() || undefined
107+
// A tool row's rules that did not decode to an array are corrupt, not empty: falling back
108+
// would run the operation on the model's filter instead of the one the user pinned.
109+
if (isToolCall(context) && !Array.isArray(builderValue)) {
110+
throw new Error(`Invalid ${fieldName} builder rules: expected a list of conditions.`)
111+
}
112+
const built = fromBuilder(builderValue as TRule[]) || undefined
107113
if (built !== undefined || !isToolCall(context) || !json) return built
108114
} else if (!json) {
109115
return undefined
@@ -117,8 +123,8 @@ function resolveBulkFilter(
117123
): unknown {
118124
return resolveBuilderOrJson(
119125
params.bulkFilterMode === 'builder' && Boolean(params.bulkFilterBuilder),
120-
() =>
121-
filterRulesToFilter(params.bulkFilterBuilder as Parameters<typeof filterRulesToFilter>[0]),
126+
params.bulkFilterBuilder,
127+
filterRulesToFilter,
122128
params.filter,
123129
'Filter',
124130
context
@@ -196,14 +202,16 @@ const paramTransformers: Record<
196202
const builderMode = params.builderMode === 'builder'
197203
const filter = resolveBuilderOrJson(
198204
builderMode && Boolean(params.filterBuilder),
199-
() => filterRulesToFilter(params.filterBuilder as Parameters<typeof filterRulesToFilter>[0]),
205+
params.filterBuilder,
206+
filterRulesToFilter,
200207
params.filter,
201208
'Filter',
202209
context
203210
)
204211
const sort = resolveBuilderOrJson(
205212
builderMode && Boolean(params.sortBuilder),
206-
() => sortRulesToSort(params.sortBuilder as Parameters<typeof sortRulesToSort>[0]),
213+
params.sortBuilder,
214+
sortRulesToSort,
207215
params.sort,
208216
'Sort',
209217
context

‎apps/sim/providers/utils.test.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
sortRulesToSort,
1919
} from '@/lib/table/query-builder/converters'
2020
import { normalizeTablePredicate } from '@/lib/table/query-builder/predicate'
21-
import type { FilterRule, SortRule } from '@/lib/table/types'
21+
import type { FilterRule, Predicate, SortRule } from '@/lib/table/types'
2222
import { getParamsForToolOperation } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/param-dependents'
2323
import { CbInsightsV2Block } from '@/blocks/blocks/cbinsights'
2424
import { CheckrV2Block } from '@/blocks/blocks/checkr'
@@ -942,12 +942,12 @@ describe('Table builder rules pinned on an agent tool row', () => {
942942
const pinnedRules: FilterRule[] = [
943943
{ id: 'r1', logicalOperator: 'and', column: 'status', operator: 'eq', value: 'active' },
944944
]
945-
const modelFilter = { field: 'status', op: 'eq', value: 'archived' }
945+
const modelFilter: Predicate = { field: 'status', op: 'eq', value: 'archived' }
946946

947947
async function runTableTool(
948948
block: typeof TableBlock,
949949
operation: string,
950-
tool: typeof tableUpdateRowsByFilterTool | typeof tableQueryRowsV2Tool,
950+
tool: { toolEnrichment?: unknown },
951951
stored: Record<string, string>,
952952
modelArgs: Record<string, unknown>
953953
) {
@@ -1031,6 +1031,18 @@ describe('Table builder rules pinned on an agent tool row', () => {
10311031
expect(params.filter).toEqual(filterRulesToFilter(pinnedRules))
10321032
})
10331033

1034+
it('fails the call instead of running a v1 bulk delete on the model filter', async () => {
1035+
await expect(
1036+
runTableTool(
1037+
TableBlock,
1038+
'delete_rows_by_filter',
1039+
tableDeleteRowsByFilterTool,
1040+
{ bulkFilterMode: 'builder', bulkFilterBuilder: '[{"column":"status","value":"a"b"}]' },
1041+
{ filter: { status: 'archived' } }
1042+
)
1043+
).rejects.toThrow('Invalid Filter builder rules')
1044+
})
1045+
10341046
it('applies the pinned v1 query filter and sort builders', async () => {
10351047
const sortRules: SortRule[] = [{ id: 's1', column: 'wins', direction: 'desc' }]
10361048
const params = await runTableTool(

0 commit comments

Comments
 (0)