Skip to content

Commit ad057dc

Browse files
committed
test(delegation): cover custom-tool execute context and state fallback
1 parent 2801d66 commit ad057dc

2 files changed

Lines changed: 80 additions & 4 deletions

File tree

‎src/core/assistant-message/__tests__/presentAssistantMessage-custom-tool.spec.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,51 @@ describe("presentAssistantMessage - Custom Tool Recording", () => {
123123
})
124124
})
125125

126+
describe("Custom tool mode delegation regression", () => {
127+
// Regression for issue #1623.
128+
// Before the fix, customTool.execute received the shared provider mode
129+
// instead of the task-local mode. A child delegated to "architect" would
130+
// have its custom tool called with "orchestrator".
131+
it("passes the task-local mode to customTool.execute, not the provider mode", async () => {
132+
// Provider says "orchestrator"; task was delegated to "architect".
133+
mockTask.providerRef = {
134+
deref: () => ({
135+
getState: vi.fn().mockResolvedValue({
136+
mode: "orchestrator",
137+
customModes: [],
138+
experiments: { customTools: true },
139+
}),
140+
}),
141+
}
142+
mockTask.getTaskMode = vi.fn().mockResolvedValue("architect")
143+
144+
const executeMock = vi.fn().mockResolvedValue("result")
145+
vi.mocked(customToolRegistry.has).mockReturnValue(true)
146+
vi.mocked(customToolRegistry.get).mockReturnValue({
147+
name: "my_custom_tool",
148+
description: "A custom tool",
149+
execute: executeMock,
150+
})
151+
152+
mockTask.assistantMessageContent = [
153+
{
154+
type: "tool_use",
155+
id: "call_delegation",
156+
name: "my_custom_tool",
157+
params: { value: "test" },
158+
partial: false,
159+
},
160+
]
161+
162+
await presentAssistantMessage(mockTask)
163+
164+
expect(executeMock).toHaveBeenCalledOnce()
165+
const context = executeMock.mock.calls[0][1]
166+
expect(context.mode).toBe("architect")
167+
expect(context.task).toBe(mockTask)
168+
})
169+
})
170+
126171
describe("Custom tool error recording", () => {
127172
it("should record custom tool error as 'custom_tool'", async () => {
128173
const toolCallId = "tool_call_custom_error_123"

‎src/core/assistant-message/__tests__/presentAssistantMessage-tool-usage-attribution.spec.ts‎

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,12 @@ interface MockTask {
6565
recordToolError: ReturnType<typeof vi.fn>
6666
toolRepetitionDetector: { check: ReturnType<typeof vi.fn> }
6767
providerRef: {
68-
deref: () => {
69-
getState: ReturnType<typeof vi.fn>
70-
getMcpHub?: () => { findServerNameBySanitizedName: (name: string) => string | undefined }
71-
}
68+
deref: () =>
69+
| {
70+
getState: ReturnType<typeof vi.fn>
71+
getMcpHub?: () => { findServerNameBySanitizedName: (name: string) => string | undefined }
72+
}
73+
| undefined
7274
}
7375
say: ReturnType<typeof vi.fn>
7476
ask: ReturnType<typeof vi.fn>
@@ -319,6 +321,35 @@ describe("presentAssistantMessage - tool usage attribution", () => {
319321
})
320322
})
321323

324+
describe("undefined provider state", () => {
325+
// Covers the `state ?? {}` fallback branch (line 347 of presentAssistantMessage.ts).
326+
// When providerRef.deref() returns undefined, state is undefined and the
327+
// destructure falls back to {}, so customModes / experiments / disabledTools
328+
// are all undefined. Tool validation must still use the task-local mode.
329+
it("falls back to empty state when provider is unavailable", async () => {
330+
mockTask.providerRef = { deref: () => undefined }
331+
mockTask.assistantMessageContent = [
332+
{
333+
type: "tool_use",
334+
id: "call_no_state",
335+
name: "read_file",
336+
params: { path: "test.ts" },
337+
nativeArgs: { path: "test.ts" },
338+
partial: false,
339+
},
340+
]
341+
342+
await presentAssistantMessage(mockTask as unknown as Task)
343+
344+
// validateToolUse must still be called with the task-local mode.
345+
const calls = vi.mocked(validateToolUse).mock.calls
346+
expect(calls.length).toBeGreaterThan(0)
347+
expect(calls[0][1]).toBe("code")
348+
// customModes falls back to [] (from the ?? {} path).
349+
expect(calls[0][2]).toEqual([])
350+
})
351+
})
352+
322353
describe("mode delegation regression", () => {
323354
// Regression for issue #1623.
324355
// Before the fix, validateToolUse received the shared provider mode instead

0 commit comments

Comments
 (0)