Skip to content

Commit 4a95fac

Browse files
committed
Revert "Add deterministic tool call ids (#594)"
This reverts commit f43b59e.
1 parent 1dfed80 commit 4a95fac

8 files changed

Lines changed: 17 additions & 160 deletions

File tree

packages/agent-runtime/src/__tests__/run-programmatic-step.test.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -212,28 +212,6 @@ describe('runProgrammaticStep', () => {
212212
})
213213

214214
describe('tool execution', () => {
215-
it('assigns deterministic per-tool ids to handleSteps tool calls', async () => {
216-
const mockGenerator = (function* () {
217-
yield { toolName: 'read_files', input: { paths: ['first.txt'] } }
218-
yield { toolName: 'read_files', input: { paths: ['second.txt'] } }
219-
yield { toolName: 'end_turn', input: {} }
220-
})() as StepGenerator
221-
222-
mockTemplate.handleSteps = () => mockGenerator
223-
224-
await runProgrammaticStep(mockParams)
225-
226-
expect(executeToolCallSpy.mock.calls[0][0].toolCallId).toBe(
227-
'functions.read_files:0',
228-
)
229-
expect(executeToolCallSpy.mock.calls[1][0].toolCallId).toBe(
230-
'functions.read_files:1',
231-
)
232-
expect(executeToolCallSpy.mock.calls[2][0].toolCallId).toBe(
233-
'functions.end_turn:0',
234-
)
235-
})
236-
237215
it('should not add tool call message for add_message tool', async () => {
238216
const mockGenerator = (function* () {
239217
yield {

packages/agent-runtime/src/__tests__/tool-validation-error.test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,16 +464,13 @@ describe('tool validation error handling', () => {
464464
)
465465
expect(toolCallEvents.length).toBe(1)
466466
expect(toolCallEvents[0].toolName).toBe('read_files')
467-
expect(toolCallEvents[0].toolCallId).toBe('functions.read_files:0')
468467

469468
// Verify tool_result event was emitted
470469
const toolResultEvents = responseChunks.filter(
471470
(chunk): chunk is Extract<PrintModeEvent, { type: 'tool_result' }> =>
472471
typeof chunk !== 'string' && chunk.type === 'tool_result',
473472
)
474473
expect(toolResultEvents.length).toBe(1)
475-
expect(toolResultEvents[0].toolName).toBe('read_files')
476-
expect(toolResultEvents[0].toolCallId).toBe('functions.read_files:0')
477474

478475
// Verify NO error events
479476
const errorEvents = responseChunks.filter(

packages/agent-runtime/src/run-programmatic-step.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { cloneDeep } from 'lodash'
66
import { clearProposedContentForRun } from './tools/handlers/tool/proposed-content-store'
77
import { executeToolCall } from './tools/tool-executor'
88
import { parseTextWithToolCalls } from './util/parse-tool-calls-from-text'
9-
import { createToolCallIdGenerator } from './util/tool-call-id'
9+
1010

1111
import type { FileProcessingState } from './tools/handlers/tool/write-file'
1212
import type { ExecuteToolCallParams } from './tools/tool-executor'
@@ -213,7 +213,6 @@ export async function runProgrammaticStep(
213213
let toolResult: ToolResultOutput[] | undefined = undefined
214214
let endTurn = false
215215
let generateN: number | undefined = undefined
216-
const getToolCallId = createToolCallIdGenerator(agentState.messageHistory)
217216

218217
let startTime = new Date()
219218
let creditsBefore = agentState.directCreditsUsed
@@ -274,7 +273,6 @@ export async function runProgrammaticStep(
274273
previousToolCallFinished: Promise.resolve(),
275274
toolCalls,
276275
toolResults,
277-
getToolCallId,
278276
onResponseChunk,
279277
})
280278
}
@@ -303,7 +301,6 @@ export async function runProgrammaticStep(
303301
previousToolCallFinished: Promise.resolve(),
304302
toolCalls,
305303
toolResults,
306-
getToolCallId,
307304
onResponseChunk,
308305
})
309306

@@ -435,7 +432,6 @@ type ExecuteToolCallsArrayParams = Omit<
435432
| 'toolResultsToAddToMessageHistory'
436433
> & {
437434
agentState: AgentState
438-
getToolCallId: (toolName: string) => string
439435
onResponseChunk: (chunk: string | PrintModeEvent) => void
440436
}
441437

@@ -449,7 +445,7 @@ async function executeSingleToolCall(
449445
toolCallToExecute: ToolCallToExecute,
450446
params: ExecuteToolCallsArrayParams,
451447
): Promise<ToolResultOutput[] | undefined> {
452-
const { agentState, getToolCallId, onResponseChunk, toolResults } = params
448+
const { agentState, onResponseChunk, toolResults } = params
453449

454450
// Note: We don't check if the tool is available for the agent template anymore.
455451
// You can run any tool from handleSteps now!
@@ -459,7 +455,7 @@ async function executeSingleToolCall(
459455
// )
460456
// }
461457

462-
const toolCallId = getToolCallId(toolCallToExecute.toolName)
458+
const toolCallId = crypto.randomUUID()
463459
const excludeToolFromMessageHistory =
464460
toolCallToExecute.includeToolCall === false
465461

packages/agent-runtime/src/tool-stream-parser.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export async function* processStreamWithTools(params: {
5050
}
5151
trackEvent: TrackEventFn
5252
executeXmlToolCall: (params: {
53+
toolCallId: string
5354
toolName: string
5455
input: Record<string, unknown>
5556
}) => Promise<void>
@@ -149,9 +150,12 @@ export async function* processStreamWithTools(params: {
149150

150151
// Then process and yield any XML tool calls found
151152
for (const toolCall of toolCalls) {
153+
const toolCallId = `xml-${crypto.randomUUID().slice(0, 8)}`
154+
152155
// Execute the tool immediately if callback provided, pausing the stream
153156
// The callback handles emitting tool_call and tool_result events
154157
await executeXmlToolCall({
158+
toolCallId,
155159
toolName: toolCall.toolName,
156160
input: toolCall.input,
157161
})

packages/agent-runtime/src/tools/stream-parser.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
assistantMessage,
66
userMessage,
77
} from '@codebuff/common/util/messages'
8+
import { generateCompactId } from '@codebuff/common/util/string'
89

910
import { processStreamWithTools } from '../tool-stream-parser'
1011
import { INCLUDE_REASONING_IN_MESSAGE_HISTORY } from '../constants'
@@ -13,7 +14,6 @@ import {
1314
executeToolCall,
1415
tryTransformAgentToolCall,
1516
} from './tool-executor'
16-
import { createToolCallIdGenerator } from '../util/tool-call-id'
1717
import { withSystemTags } from '../util/messages'
1818

1919
import type { CustomToolCall, ExecuteToolCallParams } from './tool-executor'
@@ -91,7 +91,6 @@ export async function processStream(
9191
const toolCalls: (CodebuffToolCall | CustomToolCall)[] = []
9292
const toolCallsToAddToMessageHistory: (CodebuffToolCall | CustomToolCall)[] = []
9393
const assistantMessages: Message[] = []
94-
const getToolCallId = createToolCallIdGenerator(params.messages)
9594
let hadToolCallError = false
9695
const errorMessages: Message[] = []
9796
const { promise: streamDonePromise, resolve: resolveStreamDonePromise } =
@@ -138,6 +137,7 @@ export async function processStream(
138137
if (signal.aborted) {
139138
return
140139
}
140+
const toolCallId = generateCompactId()
141141
const isNativeTool = toolNames.includes(toolName as ToolName)
142142

143143
// Check if this is an agent tool call that should be transformed to spawn_agents
@@ -160,20 +160,19 @@ export async function processStream(
160160
// Determine which executor to use and with what parameters
161161
let toolPromise: Promise<void>
162162
if (isNativeTool || transformed) {
163-
const effectiveToolName = transformed
164-
? transformed.toolName
165-
: (toolName as ToolName)
166163
// Use executeToolCall for native tools or transformed agent calls
167164
toolPromise = executeToolCall({
168165
...params,
169-
toolName: effectiveToolName,
166+
toolName: transformed
167+
? transformed.toolName
168+
: (toolName as ToolName),
170169
input: transformed ? transformed.input : input,
171170
fromHandleSteps: false,
172171

173172
fileProcessingState,
174173
fullResponse: fullResponseChunks.join(''),
175174
previousToolCallFinished: previousPromise,
176-
toolCallId: getToolCallId(effectiveToolName),
175+
toolCallId,
177176
toolCalls,
178177
toolCallsToAddToMessageHistory,
179178
toolResults,
@@ -192,7 +191,7 @@ export async function processStream(
192191
fileProcessingState,
193192
fullResponse: fullResponseChunks.join(''),
194193
previousToolCallFinished: previousPromise,
195-
toolCallId: getToolCallId(toolName),
194+
toolCallId,
196195
toolCalls,
197196
toolCallsToAddToMessageHistory,
198197
toolResults,

packages/agent-runtime/src/tools/tool-executor.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { endsAgentStepParam, toolNames } from '@codebuff/common/tools/constants'
22
import { toolParams } from '@codebuff/common/tools/list'
33
import { normalizeAgentIdForLookup } from '@codebuff/common/util/agent-id-parsing'
4+
import { generateCompactId } from '@codebuff/common/util/string'
45
import { cloneDeep } from 'lodash'
56

67
import { getMCPToolData } from '../mcp'
78
import { MCP_TOOL_SEPARATOR } from '../mcp-constants'
89
import { getAgentShortName, getAgentToolName } from '../templates/prompts'
910
import { formatValueForError } from '../util/format-value'
10-
import { createToolCallIdGenerator } from '../util/tool-call-id'
1111
import { codebuffToolHandlers } from './handlers/list'
1212
import { getMatchingSpawn } from './handlers/tool/spawn-agent-utils'
1313
import { getAgentTemplate } from '../templates/agent-registry'
@@ -309,9 +309,7 @@ export async function executeToolCall<T extends ToolName>(
309309
onResponseChunk,
310310
requestToolCall,
311311
} = params
312-
const toolCallId =
313-
params.toolCallId ??
314-
createToolCallIdGenerator(agentState.messageHistory, toolCalls)(toolName)
312+
const toolCallId = params.toolCallId ?? generateCompactId()
315313

316314
const toolCall: CodebuffToolCall<T> | ToolCallError = parseRawToolCall<T>({
317315
rawToolCall: {
@@ -651,11 +649,7 @@ export async function executeCustomToolCall(
651649
}),
652650
rawToolCall: {
653651
toolName,
654-
toolCallId:
655-
toolCallId ??
656-
createToolCallIdGenerator(agentState.messageHistory, toolCalls)(
657-
toolName,
658-
),
652+
toolCallId: toolCallId ?? generateCompactId(),
659653
input,
660654
},
661655
autoInsertEndStepParam,

packages/agent-runtime/src/util/__tests__/tool-call-id.test.ts

Lines changed: 0 additions & 63 deletions
This file was deleted.

packages/agent-runtime/src/util/tool-call-id.ts

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)