Skip to content

Commit bb14912

Browse files
committed
fix(agent): record or skip a tool call whose block mapper rejects its params
A pending-call retry now records a ToolCallParamsError as a failed tool call instead of aborting the continuation (other preparation refusals still abort), and a Human-in-the-loop v2 notification whose mapper rejects its params is skipped rather than sent with raw params.
1 parent fc69dd4 commit bb14912

5 files changed

Lines changed: 76 additions & 20 deletions

File tree

‎apps/sim/executor/handlers/human-in-the-loop/handler.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import type { BlockHandler, ExecutionContext, PauseMetadata } from '@/executor/t
2828
import { collectBlockData } from '@/executor/utils/block-data'
2929
import { convertBuilderDataToJson, convertPropertyValue } from '@/executor/utils/builder-data'
3030
import { parseObjectStrings } from '@/executor/utils/json'
31+
import { ToolCallParamsError } from '@/providers/tool-params-error'
3132
import { buildBlockToolParamsTransform } from '@/providers/utils'
3233
import type { SerializedBlock } from '@/serializer/types'
3334
import { executeTool } from '@/tools'
@@ -449,7 +450,9 @@ export class HumanInTheLoopBlockHandler implements BlockHandler {
449450
return paramsTransform(visibleParams)
450451
} catch (error) {
451452
// Mirrors `prepareToolExecution`: a transform that throws must not take the
452-
// notification down with it, so the raw params still go out.
453+
// notification down with it, so the raw params still go out — except a
454+
// `ToolCallParamsError`, whose raw params are unsafe to send.
455+
if (error instanceof ToolCallParamsError) throw error
453456
logger.warn('Notification tool params transform failed, using raw params', {
454457
toolId,
455458
error,

‎apps/sim/executor/handlers/human-in-the-loop/notification-params.test.ts‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,38 @@ const notifierBlock = {
2727
outputs: {},
2828
}
2929

30+
/** A notifier whose mapper rejects its stored params, as Table's does for a corrupt filter. */
31+
const rejectingNotifierBlock = {
32+
...notifierBlock,
33+
tools: {
34+
...notifierBlock.tools,
35+
config: {
36+
params: () => {
37+
throw new ToolCallParamsError('Invalid JSON in Filter')
38+
},
39+
},
40+
},
41+
}
42+
3043
vi.mock('@/tools/utils', () => toolsUtilsMock)
3144

3245
vi.mock('@/tools', () => toolsMock)
3346

3447
import { getBlock } from '@/blocks/registry'
3548
import { PAUSE_RESUME } from '@/executor/constants'
3649
import { HumanInTheLoopBlockHandler } from '@/executor/handlers/human-in-the-loop/handler'
50+
import { ToolCallParamsError } from '@/providers/tool-params-error'
3751

3852
toolsUtilsMockFns.mockGetTool.mockReturnValue({
3953
id: 'notifier_send',
4054
params: { channel: { type: 'string' }, silent: { type: 'boolean' } },
4155
})
4256

43-
vi.mocked(getBlock).mockImplementation((type: string) =>
44-
type === 'notifier' ? (notifierBlock as never) : undefined
45-
)
57+
vi.mocked(getBlock).mockImplementation((type: string) => {
58+
if (type === 'notifier') return notifierBlock as never
59+
if (type === 'rejecting_notifier') return rejectingNotifierBlock as never
60+
return undefined
61+
})
4662

4763
const executed: Array<Record<string, unknown>> = []
4864
toolsMockFns.mockExecuteTool.mockImplementation(
@@ -53,7 +69,10 @@ toolsMockFns.mockExecuteTool.mockImplementation(
5369
)
5470

5571
/** Runs one notification whose channel was configured in ADVANCED mode. */
56-
async function runNotification(blockTypeId: string): Promise<Record<string, unknown>> {
72+
async function runNotification(
73+
blockTypeId: string,
74+
notifierType = 'notifier'
75+
): Promise<Record<string, unknown>> {
5776
executed.length = 0
5877

5978
const block = {
@@ -74,7 +93,7 @@ async function runNotification(blockTypeId: string): Promise<Record<string, unkn
7493
operation: PAUSE_RESUME.OPERATION.HUMAN,
7594
notification: [
7695
{
77-
type: 'notifier',
96+
type: notifierType,
7897
toolId: 'notifier_send',
7998
title: 'Notify',
8099
params: { channel: '', manualChannel: 'C123', silent: 'false' },
@@ -103,6 +122,11 @@ describe('Human block notification params', () => {
103122
})
104123
})
105124

125+
it('v2 skips a notification whose mapper rejects its params instead of sending raw ones', async () => {
126+
await runNotification('human_in_the_loop_v2', 'rejecting_notifier')
127+
expect(executed).toEqual([])
128+
})
129+
106130
it('v1 keeps handing the tool its raw stored values', async () => {
107131
// Deliberately unchanged: altering what an already-configured v1 notification sends
108132
// is exactly the break v2 exists to avoid.

‎apps/sim/providers/conversation-continuation.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
setEncryptedConversationMessage,
2121
} from '@/providers/conversation-metadata'
2222
import { executeProviderTool } from '@/providers/runtime-context'
23+
import { ToolCallParamsError } from '@/providers/tool-params-error'
2324
import type { Message, ProviderRequest } from '@/providers/types'
2425
import { prepareToolExecution } from '@/providers/utils'
2526

@@ -137,6 +138,19 @@ describe('durable conversation restoration and continuation', () => {
137138
).rejects.toBe(refused)
138139
})
139140

141+
it('records a block mapper rejection as a failed tool call without dispatching it', async () => {
142+
vi.mocked(prepareToolExecution).mockImplementationOnce(() => {
143+
throw new ToolCallParamsError('Invalid JSON in Filter')
144+
})
145+
const session = await pendingSession()
146+
await continuePendingConversationCalls(toolRequest, session)
147+
expect(session.getPendingCalls()).toEqual([])
148+
expect(session.getMessages('openai', 'model-a', 'binding-a')[1].content).toContain(
149+
'Invalid JSON in Filter'
150+
)
151+
expect(executeProviderTool).not.toHaveBeenCalled()
152+
})
153+
140154
it('clears a previously restored native message when the next fallback binding changes', async () => {
141155
const messages = toolGroup()
142156
const native = {

‎apps/sim/providers/conversation-continuation.ts‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import { getProviderHistoryProtocol, requiresNativeToolHistory } from '@/providers/history-adapters'
1717
import { executeProviderTool } from '@/providers/runtime-context'
1818
import { isAbortError } from '@/providers/streaming-tool-loop-shared'
19+
import { ToolCallParamsError } from '@/providers/tool-params-error'
1920
import type { Message, ProviderId, ProviderRequest } from '@/providers/types'
2021
import { prepareToolExecution } from '@/providers/utils'
2122

@@ -78,15 +79,29 @@ export async function continuePendingConversationCalls(
7879
})
7980
continue
8081
}
81-
const { executionParams } = prepareToolExecution(
82-
tool,
83-
args,
84-
{
85-
...request,
86-
resolveToolInvocationId: () => call.invocationId,
87-
},
88-
call.providerCallId
89-
)
82+
let executionParams: ReturnType<typeof prepareToolExecution>['executionParams']
83+
try {
84+
executionParams = prepareToolExecution(
85+
tool,
86+
args,
87+
{
88+
...request,
89+
resolveToolInvocationId: () => call.invocationId,
90+
},
91+
call.providerCallId
92+
).executionParams
93+
} catch (error) {
94+
// Any other preparation refusal aborts the continuation; a block mapper rejecting the
95+
// arguments is a failed tool call, recorded as the original attempt would have reported it.
96+
if (!(error instanceof ToolCallParamsError)) throw error
97+
const result = { success: false, output: {}, error: getErrorMessage(error) }
98+
await session.recordToolResult({
99+
invocationId: call.invocationId,
100+
rawResponse: result,
101+
modelResponse: result,
102+
})
103+
continue
104+
}
90105
try {
91106
logger.info('Retrying an Agent tool with an unrecorded outcome')
92107
await executeProviderTool(tool.id, executionParams)

‎apps/sim/providers/tool-params-error.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
* Thrown by a block's `tools.config.params` mapper when a tool call must not run at all.
33
*
44
* A mapper failure on a tool call is normally logged and the tool runs with the raw merged
5-
* params instead (`prepareToolExecution`, Pi's local tools). That fallback drops the canonical
6-
* collapse along with everything else, so a model-supplied argument the user's pinned value had
7-
* hidden comes back: a Table bulk delete would run with the model's `filter` rather than the
8-
* filter the user configured. This error is rethrown instead, and the provider reports it to the
9-
* model as a failed tool call.
5+
* params instead (`prepareToolExecution`, Pi's local tools, Human-in-the-loop v2 notifications).
6+
* That fallback drops the canonical collapse along with everything else, so a model-supplied
7+
* argument the user's pinned value had hidden comes back: a Table bulk delete would run with the
8+
* model's `filter` rather than the filter the user configured. This error is rethrown instead,
9+
* and the call is reported as failed.
1010
*
1111
* Deliberately a leaf module: blocks import it, and `@/blocks/utils` already imports
1212
* `@/providers/utils`.

0 commit comments

Comments
 (0)