-
Notifications
You must be signed in to change notification settings - Fork 2.5k
fix(sdk): honor canUseTool timeout in CLI control requests #4491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5db56f2
fix(sdk): honor canUseTool timeout in CLI control requests
DragonnZhang a6b8cff
fix(sdk): add type assertion for CLIControlInitializeRequest.timeout …
DragonnZhang abb8d8a
fix(cli): add upper bound validation for canUseTool timeout
DragonnZhang 708e90e
test(cli): add coverage for permission controller timeout paths
DragonnZhang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
186 changes: 186 additions & 0 deletions
186
packages/cli/src/nonInteractive/control/controllers/permissionController.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Qwen Team | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { describe, expect, it, vi } from 'vitest'; | ||
| import { | ||
| InputFormat, | ||
| ToolConfirmationOutcome, | ||
| } from '@qwen-code/qwen-code-core'; | ||
| import { createMinimalSettings } from '../../../config/settings.js'; | ||
| import type { StreamJsonOutputAdapter } from '../../io/StreamJsonOutputAdapter.js'; | ||
| import type { IControlContext } from '../ControlContext.js'; | ||
| import type { IPendingRequestRegistry } from './baseController.js'; | ||
| import { PermissionController } from './permissionController.js'; | ||
|
|
||
| function createContext(canUseToolTimeoutMs?: number): IControlContext { | ||
| const abortController = new AbortController(); | ||
|
|
||
| return { | ||
| config: { | ||
| getDebugMode: vi.fn().mockReturnValue(false), | ||
| getInputFormat: vi.fn().mockReturnValue(InputFormat.STREAM_JSON), | ||
| } as unknown as IControlContext['config'], | ||
| streamJson: { | ||
| send: vi.fn(), | ||
| } as unknown as StreamJsonOutputAdapter, | ||
| sessionId: 'test-session-id', | ||
| abortSignal: abortController.signal, | ||
| debugMode: false, | ||
| settings: createMinimalSettings(), | ||
| permissionMode: 'default', | ||
| sdkCanUseToolTimeoutMs: canUseToolTimeoutMs, | ||
| sdkMcpServers: new Set<string>(), | ||
| mcpClients: new Map(), | ||
| inputClosed: false, | ||
| }; | ||
| } | ||
|
|
||
| function createRegistry(): IPendingRequestRegistry { | ||
| return { | ||
| registerIncomingRequest: vi.fn(), | ||
| deregisterIncomingRequest: vi.fn(), | ||
| registerOutgoingRequest: vi.fn(), | ||
| deregisterOutgoingRequest: vi.fn(), | ||
| }; | ||
| } | ||
|
|
||
| describe('PermissionController', () => { | ||
| it('uses SDK canUseTool timeout for outgoing permission requests', async () => { | ||
| const context = createContext(120_000); | ||
| const controller = new PermissionController( | ||
| context, | ||
| createRegistry(), | ||
| 'PermissionController', | ||
| ); | ||
| const sendControlRequest = vi | ||
| .spyOn(controller, 'sendControlRequest') | ||
| .mockResolvedValue({ | ||
| subtype: 'success', | ||
| request_id: 'request-1', | ||
| response: { behavior: 'allow' }, | ||
| }); | ||
| const onConfirm = vi.fn(); | ||
|
|
||
| controller.getToolCallUpdateCallback()([ | ||
| { | ||
| status: 'awaiting_approval', | ||
| request: { | ||
| callId: 'tool-call-1', | ||
| name: 'ask_user_question', | ||
| args: { questions: [] }, | ||
| }, | ||
| confirmationDetails: { | ||
| type: 'ask_user_question', | ||
| title: 'Please answer', | ||
| onConfirm, | ||
| }, | ||
| }, | ||
| ]); | ||
|
|
||
| await vi.waitFor(() => { | ||
| expect(sendControlRequest).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| subtype: 'can_use_tool', | ||
| tool_name: 'ask_user_question', | ||
| }), | ||
| 120_000, | ||
| context.abortSignal, | ||
| ); | ||
| }); | ||
| await vi.waitFor(() => { | ||
| expect(onConfirm).toHaveBeenCalledWith( | ||
| ToolConfirmationOutcome.ProceedOnce, | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| it('uses default timeout when SDK canUseTool timeout is undefined', async () => { | ||
| const context = createContext(); // undefined timeout | ||
| const controller = new PermissionController( | ||
| context, | ||
| createRegistry(), | ||
| 'PermissionController', | ||
| ); | ||
| const sendControlRequest = vi | ||
| .spyOn(controller, 'sendControlRequest') | ||
| .mockResolvedValue({ | ||
| subtype: 'success', | ||
| request_id: 'request-2', | ||
| response: { behavior: 'allow' }, | ||
| }); | ||
| const onConfirm = vi.fn(); | ||
|
|
||
| controller.getToolCallUpdateCallback()([ | ||
| { | ||
| status: 'awaiting_approval', | ||
| request: { | ||
| callId: 'tool-call-2', | ||
| name: 'ask_user_question', | ||
| args: { questions: [] }, | ||
| }, | ||
| confirmationDetails: { | ||
| type: 'ask_user_question', | ||
| title: 'Please answer', | ||
| onConfirm, | ||
| }, | ||
| }, | ||
| ]); | ||
|
|
||
| await vi.waitFor(() => { | ||
| expect(sendControlRequest).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| subtype: 'can_use_tool', | ||
| tool_name: 'ask_user_question', | ||
| }), | ||
| 60_000, // DEFAULT_CAN_USE_TOOL_TIMEOUT_MS | ||
| context.abortSignal, | ||
| ); | ||
| }); | ||
| await vi.waitFor(() => { | ||
| expect(onConfirm).toHaveBeenCalledWith( | ||
| ToolConfirmationOutcome.ProceedOnce, | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| it('calls onConfirm with Cancel when sendControlRequest rejects', async () => { | ||
| const context = createContext(120_000); | ||
| const controller = new PermissionController( | ||
| context, | ||
| createRegistry(), | ||
| 'PermissionController', | ||
| ); | ||
| vi.spyOn(controller, 'sendControlRequest').mockRejectedValue( | ||
| new Error('Request timeout'), | ||
| ); | ||
| const onConfirm = vi.fn(); | ||
|
|
||
| controller.getToolCallUpdateCallback()([ | ||
| { | ||
| status: 'awaiting_approval', | ||
| request: { | ||
| callId: 'tool-call-3', | ||
| name: 'ask_user_question', | ||
| args: { questions: [] }, | ||
| }, | ||
| confirmationDetails: { | ||
| type: 'ask_user_question', | ||
| title: 'Please answer', | ||
| onConfirm, | ||
| }, | ||
| }, | ||
| ]); | ||
|
|
||
| await vi.waitFor(() => { | ||
| expect(onConfirm).toHaveBeenCalledWith( | ||
| ToolConfirmationOutcome.Cancel, | ||
| expect.objectContaining({ | ||
| cancelMessage: expect.stringContaining('Request timeout'), | ||
| }), | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.