@google/gemini-cli-core 0.1.13 → 0.1.15-nightly.250731.0c6f7884
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -1
- package/dist/google-gemini-cli-core-0.1.15.tgz +0 -0
- package/dist/src/code_assist/codeAssist.js +2 -2
- package/dist/src/code_assist/codeAssist.js.map +1 -1
- package/dist/src/code_assist/oauth2.js +9 -2
- package/dist/src/code_assist/oauth2.js.map +1 -1
- package/dist/src/code_assist/oauth2.test.js +99 -7
- package/dist/src/code_assist/oauth2.test.js.map +1 -1
- package/dist/src/code_assist/server.d.ts +4 -6
- package/dist/src/code_assist/server.js +4 -69
- package/dist/src/code_assist/server.js.map +1 -1
- package/dist/src/code_assist/setup.d.ts +6 -1
- package/dist/src/code_assist/setup.js +4 -1
- package/dist/src/code_assist/setup.js.map +1 -1
- package/dist/src/code_assist/setup.test.js +4 -1
- package/dist/src/code_assist/setup.test.js.map +1 -1
- package/dist/src/code_assist/types.d.ts +2 -2
- package/dist/src/config/config.d.ts +43 -11
- package/dist/src/config/config.js +79 -27
- package/dist/src/config/config.js.map +1 -1
- package/dist/src/config/config.test.js +34 -23
- package/dist/src/config/config.test.js.map +1 -1
- package/dist/src/config/flashFallback.test.js +24 -48
- package/dist/src/config/flashFallback.test.js.map +1 -1
- package/dist/src/config/models.d.ts +1 -0
- package/dist/src/config/models.js +1 -0
- package/dist/src/config/models.js.map +1 -1
- package/dist/src/core/client.d.ts +5 -2
- package/dist/src/core/client.js +64 -22
- package/dist/src/core/client.js.map +1 -1
- package/dist/src/core/client.test.js +199 -2
- package/dist/src/core/client.test.js.map +1 -1
- package/dist/src/core/contentGenerator.d.ts +1 -1
- package/dist/src/core/contentGenerator.js +1 -1
- package/dist/src/core/contentGenerator.js.map +1 -1
- package/dist/src/core/coreToolScheduler.d.ts +1 -3
- package/dist/src/core/coreToolScheduler.js +1 -3
- package/dist/src/core/coreToolScheduler.js.map +1 -1
- package/dist/src/core/coreToolScheduler.test.js +76 -1
- package/dist/src/core/coreToolScheduler.test.js.map +1 -1
- package/dist/src/core/geminiChat.d.ts +4 -3
- package/dist/src/core/geminiChat.js +9 -11
- package/dist/src/core/geminiChat.js.map +1 -1
- package/dist/src/core/geminiRequest.js +2 -37
- package/dist/src/core/geminiRequest.js.map +1 -1
- package/dist/src/core/logger.d.ts +1 -0
- package/dist/src/core/logger.js +24 -4
- package/dist/src/core/logger.js.map +1 -1
- package/dist/src/core/logger.test.js +61 -10
- package/dist/src/core/logger.test.js.map +1 -1
- package/dist/src/core/nonInteractiveToolExecutor.test.js +5 -5
- package/dist/src/core/prompts.js +43 -19
- package/dist/src/core/prompts.js.map +1 -1
- package/dist/src/core/prompts.test.js +121 -4
- package/dist/src/core/prompts.test.js.map +1 -1
- package/dist/src/core/tokenLimits.js +1 -0
- package/dist/src/core/tokenLimits.js.map +1 -1
- package/dist/src/core/turn.d.ts +7 -2
- package/dist/src/core/turn.js +9 -0
- package/dist/src/core/turn.js.map +1 -1
- package/dist/src/core/turn.test.js +129 -0
- package/dist/src/core/turn.test.js.map +1 -1
- package/dist/src/ide/detect-ide.d.ts +10 -0
- package/dist/src/ide/detect-ide.js +24 -0
- package/dist/src/ide/detect-ide.js.map +1 -0
- package/dist/src/ide/ide-client.d.ts +40 -0
- package/dist/src/ide/ide-client.js +152 -0
- package/dist/src/ide/ide-client.js.map +1 -0
- package/dist/src/ide/ide-installer.d.ts +15 -0
- package/dist/src/ide/ide-installer.js +111 -0
- package/dist/src/ide/ide-installer.js.map +1 -0
- package/dist/src/ide/ide-installer.test.js +78 -0
- package/dist/src/ide/ide-installer.test.js.map +1 -0
- package/dist/src/ide/ideContext.d.ts +279 -0
- package/dist/src/ide/ideContext.js +102 -0
- package/dist/src/ide/ideContext.js.map +1 -0
- package/dist/src/ide/ideContext.test.js +265 -0
- package/dist/src/ide/ideContext.test.js.map +1 -0
- package/dist/src/index.d.ts +11 -1
- package/dist/src/index.js +14 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/mcp/google-auth-provider.d.ts +23 -0
- package/dist/src/mcp/google-auth-provider.js +63 -0
- package/dist/src/mcp/google-auth-provider.js.map +1 -0
- package/dist/src/mcp/google-auth-provider.test.d.ts +6 -0
- package/dist/src/mcp/google-auth-provider.test.js +54 -0
- package/dist/src/mcp/google-auth-provider.test.js.map +1 -0
- package/dist/src/mcp/oauth-provider.d.ts +5 -1
- package/dist/src/mcp/oauth-provider.js +36 -11
- package/dist/src/mcp/oauth-provider.js.map +1 -1
- package/dist/src/mcp/oauth-provider.test.js +2 -2
- package/dist/src/mcp/oauth-provider.test.js.map +1 -1
- package/dist/src/mcp/oauth-token-storage.d.ts +3 -1
- package/dist/src/mcp/oauth-token-storage.js +3 -1
- package/dist/src/mcp/oauth-token-storage.js.map +1 -1
- package/dist/src/mcp/oauth-utils.js +2 -2
- package/dist/src/mcp/oauth-utils.js.map +1 -1
- package/dist/src/mcp/oauth-utils.test.js +1 -1
- package/dist/src/mcp/oauth-utils.test.js.map +1 -1
- package/dist/src/prompts/mcp-prompts.d.ts +8 -0
- package/dist/src/prompts/mcp-prompts.js +13 -0
- package/dist/src/prompts/mcp-prompts.js.map +1 -0
- package/dist/src/prompts/prompt-registry.d.ts +26 -0
- package/dist/src/prompts/prompt-registry.js +47 -0
- package/dist/src/prompts/prompt-registry.js.map +1 -0
- package/dist/src/services/fileDiscoveryService.test.js +101 -60
- package/dist/src/services/fileDiscoveryService.test.js.map +1 -1
- package/dist/src/services/gitService.test.js +67 -86
- package/dist/src/services/gitService.test.js.map +1 -1
- package/dist/src/services/loopDetectionService.d.ts +51 -5
- package/dist/src/services/loopDetectionService.js +140 -36
- package/dist/src/services/loopDetectionService.js.map +1 -1
- package/dist/src/services/loopDetectionService.test.js +105 -99
- package/dist/src/services/loopDetectionService.test.js.map +1 -1
- package/dist/src/services/shellExecutionService.d.ts +70 -0
- package/dist/src/services/shellExecutionService.js +152 -0
- package/dist/src/services/shellExecutionService.js.map +1 -0
- package/dist/src/services/shellExecutionService.test.d.ts +6 -0
- package/dist/src/services/shellExecutionService.test.js +258 -0
- package/dist/src/services/shellExecutionService.test.js.map +1 -0
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.d.ts +3 -1
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.js +75 -33
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.js.map +1 -1
- package/dist/src/telemetry/clearcut-logger/event-metadata-key.d.ts +3 -1
- package/dist/src/telemetry/clearcut-logger/event-metadata-key.js +7 -0
- package/dist/src/telemetry/clearcut-logger/event-metadata-key.js.map +1 -1
- package/dist/src/telemetry/constants.d.ts +2 -0
- package/dist/src/telemetry/constants.js +2 -0
- package/dist/src/telemetry/constants.js.map +1 -1
- package/dist/src/telemetry/file-exporters.d.ts +28 -0
- package/dist/src/telemetry/file-exporters.js +62 -0
- package/dist/src/telemetry/file-exporters.js.map +1 -0
- package/dist/src/telemetry/index.d.ts +2 -2
- package/dist/src/telemetry/index.js +2 -2
- package/dist/src/telemetry/index.js.map +1 -1
- package/dist/src/telemetry/loggers.d.ts +3 -1
- package/dist/src/telemetry/loggers.js +33 -1
- package/dist/src/telemetry/loggers.js.map +1 -1
- package/dist/src/telemetry/sdk.js +17 -6
- package/dist/src/telemetry/sdk.js.map +1 -1
- package/dist/src/telemetry/telemetry.test.js +2 -0
- package/dist/src/telemetry/telemetry.test.js.map +1 -1
- package/dist/src/telemetry/types.d.ts +16 -2
- package/dist/src/telemetry/types.js +25 -1
- package/dist/src/telemetry/types.js.map +1 -1
- package/dist/src/test-utils/mockWorkspaceContext.d.ts +13 -0
- package/dist/src/test-utils/mockWorkspaceContext.js +24 -0
- package/dist/src/test-utils/mockWorkspaceContext.js.map +1 -0
- package/dist/src/tools/edit.js +14 -7
- package/dist/src/tools/edit.js.map +1 -1
- package/dist/src/tools/edit.test.js +37 -1
- package/dist/src/tools/edit.test.js.map +1 -1
- package/dist/src/tools/glob.js +53 -17
- package/dist/src/tools/glob.js.map +1 -1
- package/dist/src/tools/glob.test.js +32 -6
- package/dist/src/tools/glob.test.js.map +1 -1
- package/dist/src/tools/grep.d.ts +1 -1
- package/dist/src/tools/grep.js +81 -29
- package/dist/src/tools/grep.js.map +1 -1
- package/dist/src/tools/grep.test.js +77 -10
- package/dist/src/tools/grep.test.js.map +1 -1
- package/dist/src/tools/ls.d.ts +5 -2
- package/dist/src/tools/ls.js +43 -13
- package/dist/src/tools/ls.js.map +1 -1
- package/dist/src/tools/ls.test.d.ts +6 -0
- package/dist/src/tools/ls.test.js +356 -0
- package/dist/src/tools/ls.test.js.map +1 -0
- package/dist/src/tools/mcp-client.d.ts +31 -3
- package/dist/src/tools/mcp-client.js +467 -38
- package/dist/src/tools/mcp-client.js.map +1 -1
- package/dist/src/tools/mcp-client.test.js +99 -7
- package/dist/src/tools/mcp-client.test.js.map +1 -1
- package/dist/src/tools/mcp-tool.js +1 -1
- package/dist/src/tools/mcp-tool.js.map +1 -1
- package/dist/src/tools/mcp-tool.test.js +34 -0
- package/dist/src/tools/mcp-tool.test.js.map +1 -1
- package/dist/src/tools/memoryTool.d.ts +17 -2
- package/dist/src/tools/memoryTool.js +130 -13
- package/dist/src/tools/memoryTool.js.map +1 -1
- package/dist/src/tools/memoryTool.test.js +88 -3
- package/dist/src/tools/memoryTool.test.js.map +1 -1
- package/dist/src/tools/modifiable-tool.test.js +51 -62
- package/dist/src/tools/modifiable-tool.test.js.map +1 -1
- package/dist/src/tools/read-file.js +8 -6
- package/dist/src/tools/read-file.js.map +1 -1
- package/dist/src/tools/read-file.test.js +125 -68
- package/dist/src/tools/read-file.test.js.map +1 -1
- package/dist/src/tools/read-many-files.d.ts +5 -3
- package/dist/src/tools/read-many-files.js +83 -33
- package/dist/src/tools/read-many-files.js.map +1 -1
- package/dist/src/tools/read-many-files.test.js +40 -4
- package/dist/src/tools/read-many-files.test.js.map +1 -1
- package/dist/src/tools/shell.d.ts +3 -23
- package/dist/src/tools/shell.js +173 -300
- package/dist/src/tools/shell.js.map +1 -1
- package/dist/src/tools/shell.test.js +278 -384
- package/dist/src/tools/shell.test.js.map +1 -1
- package/dist/src/tools/tool-registry.d.ts +13 -1
- package/dist/src/tools/tool-registry.js +46 -2
- package/dist/src/tools/tool-registry.js.map +1 -1
- package/dist/src/tools/tool-registry.test.js +13 -5
- package/dist/src/tools/tool-registry.test.js.map +1 -1
- package/dist/src/tools/write-file.js +5 -3
- package/dist/src/tools/write-file.js.map +1 -1
- package/dist/src/tools/write-file.test.js +36 -2
- package/dist/src/tools/write-file.test.js.map +1 -1
- package/dist/src/utils/bfsFileSearch.d.ts +2 -0
- package/dist/src/utils/bfsFileSearch.js +51 -24
- package/dist/src/utils/bfsFileSearch.js.map +1 -1
- package/dist/src/utils/bfsFileSearch.test.js +163 -103
- package/dist/src/utils/bfsFileSearch.test.js.map +1 -1
- package/dist/src/utils/editCorrector.js +4 -4
- package/dist/src/utils/editCorrector.js.map +1 -1
- package/dist/src/utils/editCorrector.test.js +1 -1
- package/dist/src/utils/editor.js +16 -10
- package/dist/src/utils/editor.js.map +1 -1
- package/dist/src/utils/editor.test.js +128 -28
- package/dist/src/utils/editor.test.js.map +1 -1
- package/dist/src/utils/errorReporting.d.ts +1 -1
- package/dist/src/utils/errorReporting.js +2 -2
- package/dist/src/utils/errorReporting.js.map +1 -1
- package/dist/src/utils/errorReporting.test.js +44 -38
- package/dist/src/utils/errorReporting.test.js.map +1 -1
- package/dist/src/utils/fileUtils.d.ts +4 -4
- package/dist/src/utils/fileUtils.js +31 -15
- package/dist/src/utils/fileUtils.js.map +1 -1
- package/dist/src/utils/fileUtils.test.js +37 -37
- package/dist/src/utils/fileUtils.test.js.map +1 -1
- package/dist/src/utils/flashFallback.integration.test.js +8 -0
- package/dist/src/utils/flashFallback.integration.test.js.map +1 -1
- package/dist/src/utils/formatters.d.ts +6 -0
- package/dist/src/utils/formatters.js +16 -0
- package/dist/src/utils/formatters.js.map +1 -0
- package/dist/src/utils/getFolderStructure.d.ts +3 -2
- package/dist/src/utils/getFolderStructure.js +27 -28
- package/dist/src/utils/getFolderStructure.js.map +1 -1
- package/dist/src/utils/getFolderStructure.test.js +169 -187
- package/dist/src/utils/getFolderStructure.test.js.map +1 -1
- package/dist/src/utils/gitIgnoreParser.js +4 -7
- package/dist/src/utils/gitIgnoreParser.js.map +1 -1
- package/dist/src/utils/gitIgnoreParser.test.js +70 -61
- package/dist/src/utils/gitIgnoreParser.test.js.map +1 -1
- package/dist/src/utils/memoryDiscovery.d.ts +2 -1
- package/dist/src/utils/memoryDiscovery.js +11 -5
- package/dist/src/utils/memoryDiscovery.js.map +1 -1
- package/dist/src/utils/memoryDiscovery.test.js +160 -371
- package/dist/src/utils/memoryDiscovery.test.js.map +1 -1
- package/dist/src/utils/nextSpeakerChecker.js +2 -2
- package/dist/src/utils/nextSpeakerChecker.js.map +1 -1
- package/dist/src/utils/nextSpeakerChecker.test.js +2 -2
- package/dist/src/utils/nextSpeakerChecker.test.js.map +1 -1
- package/dist/src/utils/partUtils.d.ts +14 -0
- package/dist/src/utils/partUtils.js +65 -0
- package/dist/src/utils/partUtils.js.map +1 -0
- package/dist/src/utils/partUtils.test.d.ts +6 -0
- package/dist/src/utils/partUtils.test.js +130 -0
- package/dist/src/utils/partUtils.test.js.map +1 -0
- package/dist/src/utils/paths.d.ts +11 -0
- package/dist/src/utils/paths.js +17 -1
- package/dist/src/utils/paths.js.map +1 -1
- package/dist/src/utils/quotaErrorDetection.js +0 -2
- package/dist/src/utils/quotaErrorDetection.js.map +1 -1
- package/dist/src/utils/retry.d.ts +3 -0
- package/dist/src/utils/retry.js +1 -1
- package/dist/src/utils/retry.js.map +1 -1
- package/dist/src/utils/retry.test.js.map +1 -1
- package/dist/src/utils/schemaValidator.d.ts +1 -1
- package/dist/src/utils/schemaValidator.js +6 -3
- package/dist/src/utils/schemaValidator.js.map +1 -1
- package/dist/src/utils/shell-utils.d.ts +78 -0
- package/dist/src/utils/shell-utils.js +306 -0
- package/dist/src/utils/shell-utils.js.map +1 -0
- package/dist/src/utils/shell-utils.test.d.ts +6 -0
- package/dist/src/utils/shell-utils.test.js +200 -0
- package/dist/src/utils/shell-utils.test.js.map +1 -0
- package/dist/src/utils/summarizer.js +1 -30
- package/dist/src/utils/summarizer.js.map +1 -1
- package/dist/src/utils/systemEncoding.d.ts +40 -0
- package/dist/src/utils/systemEncoding.js +149 -0
- package/dist/src/utils/systemEncoding.js.map +1 -0
- package/dist/src/utils/systemEncoding.test.d.ts +6 -0
- package/dist/src/utils/systemEncoding.test.js +368 -0
- package/dist/src/utils/systemEncoding.test.js.map +1 -0
- package/dist/src/utils/textUtils.d.ts +13 -0
- package/dist/src/utils/textUtils.js +28 -0
- package/dist/src/utils/textUtils.js.map +1 -0
- package/dist/src/utils/workspaceContext.d.ts +47 -0
- package/dist/src/utils/workspaceContext.js +106 -0
- package/dist/src/utils/workspaceContext.js.map +1 -0
- package/dist/src/utils/workspaceContext.test.d.ts +6 -0
- package/dist/src/utils/workspaceContext.test.js +209 -0
- package/dist/src/utils/workspaceContext.test.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -1
- package/dist/google-gemini-cli-core-0.1.12.tgz +0 -0
- package/dist/src/core/geminiRequest.test.js +0 -72
- package/dist/src/core/geminiRequest.test.js.map +0 -1
- package/dist/src/services/ideContext.d.ts +0 -126
- package/dist/src/services/ideContext.js +0 -98
- package/dist/src/services/ideContext.js.map +0 -1
- package/dist/src/services/ideContext.test.js +0 -111
- package/dist/src/services/ideContext.test.js.map +0 -1
- /package/dist/src/{core/geminiRequest.test.d.ts → ide/ide-installer.test.d.ts} +0 -0
- /package/dist/src/{services → ide}/ideContext.test.d.ts +0 -0
|
@@ -3,404 +3,298 @@
|
|
|
3
3
|
* Copyright 2025 Google LLC
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
|
-
import {
|
|
6
|
+
import { vi, describe, it, expect, beforeEach, afterEach, } from 'vitest';
|
|
7
|
+
const mockShellExecutionService = vi.hoisted(() => vi.fn());
|
|
8
|
+
vi.mock('../services/shellExecutionService.js', () => ({
|
|
9
|
+
ShellExecutionService: { execute: mockShellExecutionService },
|
|
10
|
+
}));
|
|
11
|
+
vi.mock('fs');
|
|
12
|
+
vi.mock('os');
|
|
13
|
+
vi.mock('crypto');
|
|
14
|
+
vi.mock('../utils/summarizer.js');
|
|
15
|
+
import { isCommandAllowed } from '../utils/shell-utils.js';
|
|
7
16
|
import { ShellTool } from './shell.js';
|
|
17
|
+
import * as fs from 'fs';
|
|
18
|
+
import * as os from 'os';
|
|
19
|
+
import * as path from 'path';
|
|
20
|
+
import * as crypto from 'crypto';
|
|
8
21
|
import * as summarizer from '../utils/summarizer.js';
|
|
22
|
+
import { ToolConfirmationOutcome } from './tools.js';
|
|
23
|
+
import { OUTPUT_UPDATE_INTERVAL_MS } from './shell.js';
|
|
24
|
+
import { createMockWorkspaceContext } from '../test-utils/mockWorkspaceContext.js';
|
|
9
25
|
describe('ShellTool', () => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
26
|
+
let shellTool;
|
|
27
|
+
let mockConfig;
|
|
28
|
+
let mockShellOutputCallback;
|
|
29
|
+
let resolveExecutionPromise;
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
vi.clearAllMocks();
|
|
32
|
+
mockConfig = {
|
|
33
|
+
getCoreTools: vi.fn().mockReturnValue([]),
|
|
34
|
+
getExcludeTools: vi.fn().mockReturnValue([]),
|
|
35
|
+
getDebugMode: vi.fn().mockReturnValue(false),
|
|
36
|
+
getTargetDir: vi.fn().mockReturnValue('/test/dir'),
|
|
37
|
+
getSummarizeToolOutputConfig: vi.fn().mockReturnValue(undefined),
|
|
38
|
+
getWorkspaceContext: () => createMockWorkspaceContext('.'),
|
|
39
|
+
getGeminiClient: vi.fn(),
|
|
40
|
+
};
|
|
41
|
+
shellTool = new ShellTool(mockConfig);
|
|
42
|
+
vi.mocked(os.platform).mockReturnValue('linux');
|
|
43
|
+
vi.mocked(os.tmpdir).mockReturnValue('/tmp');
|
|
44
|
+
vi.mocked(crypto.randomBytes).mockReturnValue(Buffer.from('abcdef', 'hex'));
|
|
45
|
+
// Capture the output callback to simulate streaming events from the service
|
|
46
|
+
mockShellExecutionService.mockImplementation((_cmd, _cwd, callback) => {
|
|
47
|
+
mockShellOutputCallback = callback;
|
|
48
|
+
return {
|
|
49
|
+
pid: 12345,
|
|
50
|
+
result: new Promise((resolve) => {
|
|
51
|
+
resolveExecutionPromise = resolve;
|
|
52
|
+
}),
|
|
53
|
+
};
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
describe('isCommandAllowed', () => {
|
|
57
|
+
it('should allow a command if no restrictions are provided', () => {
|
|
58
|
+
mockConfig.getCoreTools.mockReturnValue(undefined);
|
|
59
|
+
mockConfig.getExcludeTools.mockReturnValue(undefined);
|
|
60
|
+
expect(isCommandAllowed('ls -l', mockConfig).allowed).toBe(true);
|
|
61
|
+
});
|
|
62
|
+
it('should block a command with command substitution using $()', () => {
|
|
63
|
+
expect(isCommandAllowed('echo $(rm -rf /)', mockConfig).allowed).toBe(false);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
describe('validateToolParams', () => {
|
|
67
|
+
it('should return null for a valid command', () => {
|
|
68
|
+
expect(shellTool.validateToolParams({ command: 'ls -l' })).toBeNull();
|
|
69
|
+
});
|
|
70
|
+
it('should return an error for an empty command', () => {
|
|
71
|
+
expect(shellTool.validateToolParams({ command: ' ' })).toBe('Command cannot be empty.');
|
|
72
|
+
});
|
|
73
|
+
it('should return an error for a non-existent directory', () => {
|
|
74
|
+
vi.mocked(fs.existsSync).mockReturnValue(false);
|
|
75
|
+
expect(shellTool.validateToolParams({ command: 'ls', directory: 'rel/path' })).toBe("Directory 'rel/path' is not a registered workspace directory.");
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
describe('execute', () => {
|
|
79
|
+
const mockAbortSignal = new AbortController().signal;
|
|
80
|
+
const resolveShellExecution = (result = {}) => {
|
|
81
|
+
const fullResult = {
|
|
82
|
+
rawOutput: Buffer.from(result.output || ''),
|
|
83
|
+
output: 'Success',
|
|
84
|
+
stdout: 'Success',
|
|
85
|
+
stderr: '',
|
|
86
|
+
exitCode: 0,
|
|
87
|
+
signal: null,
|
|
88
|
+
error: null,
|
|
89
|
+
aborted: false,
|
|
90
|
+
pid: 12345,
|
|
91
|
+
...result,
|
|
92
|
+
};
|
|
93
|
+
resolveExecutionPromise(fullResult);
|
|
94
|
+
};
|
|
95
|
+
it('should wrap command on linux and parse pgrep output', async () => {
|
|
96
|
+
const promise = shellTool.execute({ command: 'my-command &' }, mockAbortSignal);
|
|
97
|
+
resolveShellExecution({ pid: 54321 });
|
|
98
|
+
vi.mocked(fs.existsSync).mockReturnValue(true);
|
|
99
|
+
vi.mocked(fs.readFileSync).mockReturnValue('54321\n54322\n'); // Service PID and background PID
|
|
100
|
+
const result = await promise;
|
|
101
|
+
const tmpFile = path.join(os.tmpdir(), 'shell_pgrep_abcdef.tmp');
|
|
102
|
+
const wrappedCommand = `{ my-command & }; __code=$?; pgrep -g 0 >${tmpFile} 2>&1; exit $__code;`;
|
|
103
|
+
expect(mockShellExecutionService).toHaveBeenCalledWith(wrappedCommand, expect.any(String), expect.any(Function), mockAbortSignal);
|
|
104
|
+
expect(result.llmContent).toContain('Background PIDs: 54322');
|
|
105
|
+
expect(vi.mocked(fs.unlinkSync)).toHaveBeenCalledWith(tmpFile);
|
|
106
|
+
});
|
|
107
|
+
it('should not wrap command on windows', async () => {
|
|
108
|
+
vi.mocked(os.platform).mockReturnValue('win32');
|
|
109
|
+
const promise = shellTool.execute({ command: 'dir' }, mockAbortSignal);
|
|
110
|
+
resolveExecutionPromise({
|
|
111
|
+
rawOutput: Buffer.from(''),
|
|
112
|
+
output: '',
|
|
113
|
+
stdout: '',
|
|
114
|
+
stderr: '',
|
|
115
|
+
exitCode: 0,
|
|
116
|
+
signal: null,
|
|
117
|
+
error: null,
|
|
118
|
+
aborted: false,
|
|
119
|
+
pid: 12345,
|
|
120
|
+
});
|
|
121
|
+
await promise;
|
|
122
|
+
expect(mockShellExecutionService).toHaveBeenCalledWith('dir', expect.any(String), expect.any(Function), mockAbortSignal);
|
|
123
|
+
});
|
|
124
|
+
it('should format error messages correctly', async () => {
|
|
125
|
+
const error = new Error('wrapped command failed');
|
|
126
|
+
const promise = shellTool.execute({ command: 'user-command' }, mockAbortSignal);
|
|
127
|
+
resolveShellExecution({
|
|
128
|
+
error,
|
|
129
|
+
exitCode: 1,
|
|
130
|
+
output: 'err',
|
|
131
|
+
stderr: 'err',
|
|
132
|
+
rawOutput: Buffer.from('err'),
|
|
133
|
+
stdout: '',
|
|
134
|
+
signal: null,
|
|
135
|
+
aborted: false,
|
|
136
|
+
pid: 12345,
|
|
137
|
+
});
|
|
138
|
+
const result = await promise;
|
|
139
|
+
// The final llmContent should contain the user's command, not the wrapper
|
|
140
|
+
expect(result.llmContent).toContain('Error: wrapped command failed');
|
|
141
|
+
expect(result.llmContent).not.toContain('pgrep');
|
|
142
|
+
});
|
|
143
|
+
it('should summarize output when configured', async () => {
|
|
144
|
+
mockConfig.getSummarizeToolOutputConfig.mockReturnValue({
|
|
145
|
+
[shellTool.name]: { tokenBudget: 1000 },
|
|
146
|
+
});
|
|
147
|
+
vi.mocked(summarizer.summarizeToolOutput).mockResolvedValue('summarized output');
|
|
148
|
+
const promise = shellTool.execute({ command: 'ls' }, mockAbortSignal);
|
|
149
|
+
resolveExecutionPromise({
|
|
150
|
+
output: 'long output',
|
|
151
|
+
rawOutput: Buffer.from('long output'),
|
|
152
|
+
stdout: 'long output',
|
|
153
|
+
stderr: '',
|
|
154
|
+
exitCode: 0,
|
|
155
|
+
signal: null,
|
|
156
|
+
error: null,
|
|
157
|
+
aborted: false,
|
|
158
|
+
pid: 12345,
|
|
159
|
+
});
|
|
160
|
+
const result = await promise;
|
|
161
|
+
expect(summarizer.summarizeToolOutput).toHaveBeenCalledWith(expect.any(String), mockConfig.getGeminiClient(), mockAbortSignal, 1000);
|
|
162
|
+
expect(result.llmContent).toBe('summarized output');
|
|
163
|
+
expect(result.returnDisplay).toBe('long output');
|
|
164
|
+
});
|
|
165
|
+
it('should clean up the temp file on synchronous execution error', async () => {
|
|
166
|
+
const error = new Error('sync spawn error');
|
|
167
|
+
mockShellExecutionService.mockImplementation(() => {
|
|
168
|
+
throw error;
|
|
169
|
+
});
|
|
170
|
+
vi.mocked(fs.existsSync).mockReturnValue(true); // Pretend the file exists
|
|
171
|
+
await expect(shellTool.execute({ command: 'a-command' }, mockAbortSignal)).rejects.toThrow(error);
|
|
172
|
+
const tmpFile = path.join(os.tmpdir(), 'shell_pgrep_abcdef.tmp');
|
|
173
|
+
expect(vi.mocked(fs.unlinkSync)).toHaveBeenCalledWith(tmpFile);
|
|
174
|
+
});
|
|
175
|
+
describe('Streaming to `updateOutput`', () => {
|
|
176
|
+
let updateOutputMock;
|
|
177
|
+
beforeEach(() => {
|
|
178
|
+
vi.useFakeTimers({ toFake: ['Date'] });
|
|
179
|
+
updateOutputMock = vi.fn();
|
|
180
|
+
});
|
|
181
|
+
afterEach(() => {
|
|
182
|
+
vi.useRealTimers();
|
|
183
|
+
});
|
|
184
|
+
it('should throttle text output updates', async () => {
|
|
185
|
+
const promise = shellTool.execute({ command: 'stream' }, mockAbortSignal, updateOutputMock);
|
|
186
|
+
// First chunk, should be throttled.
|
|
187
|
+
mockShellOutputCallback({
|
|
188
|
+
type: 'data',
|
|
189
|
+
stream: 'stdout',
|
|
190
|
+
chunk: 'hello ',
|
|
191
|
+
});
|
|
192
|
+
expect(updateOutputMock).not.toHaveBeenCalled();
|
|
193
|
+
// Advance time past the throttle interval.
|
|
194
|
+
await vi.advanceTimersByTimeAsync(OUTPUT_UPDATE_INTERVAL_MS + 1);
|
|
195
|
+
// Send a second chunk. THIS event triggers the update with the CUMULATIVE content.
|
|
196
|
+
mockShellOutputCallback({
|
|
197
|
+
type: 'data',
|
|
198
|
+
stream: 'stderr',
|
|
199
|
+
chunk: 'world',
|
|
200
|
+
});
|
|
201
|
+
// It should have been called once now with the combined output.
|
|
202
|
+
expect(updateOutputMock).toHaveBeenCalledOnce();
|
|
203
|
+
expect(updateOutputMock).toHaveBeenCalledWith('hello \nworld');
|
|
204
|
+
resolveExecutionPromise({
|
|
205
|
+
rawOutput: Buffer.from(''),
|
|
206
|
+
output: '',
|
|
207
|
+
stdout: '',
|
|
208
|
+
stderr: '',
|
|
209
|
+
exitCode: 0,
|
|
210
|
+
signal: null,
|
|
211
|
+
error: null,
|
|
212
|
+
aborted: false,
|
|
213
|
+
pid: 12345,
|
|
214
|
+
});
|
|
215
|
+
await promise;
|
|
216
|
+
});
|
|
217
|
+
it('should immediately show binary detection message and throttle progress', async () => {
|
|
218
|
+
const promise = shellTool.execute({ command: 'cat img' }, mockAbortSignal, updateOutputMock);
|
|
219
|
+
mockShellOutputCallback({ type: 'binary_detected' });
|
|
220
|
+
expect(updateOutputMock).toHaveBeenCalledOnce();
|
|
221
|
+
expect(updateOutputMock).toHaveBeenCalledWith('[Binary output detected. Halting stream...]');
|
|
222
|
+
mockShellOutputCallback({
|
|
223
|
+
type: 'binary_progress',
|
|
224
|
+
bytesReceived: 1024,
|
|
225
|
+
});
|
|
226
|
+
expect(updateOutputMock).toHaveBeenCalledOnce();
|
|
227
|
+
// Advance time past the throttle interval.
|
|
228
|
+
await vi.advanceTimersByTimeAsync(OUTPUT_UPDATE_INTERVAL_MS + 1);
|
|
229
|
+
// Send a SECOND progress event. This one will trigger the flush.
|
|
230
|
+
mockShellOutputCallback({
|
|
231
|
+
type: 'binary_progress',
|
|
232
|
+
bytesReceived: 2048,
|
|
233
|
+
});
|
|
234
|
+
// Now it should be called a second time with the latest progress.
|
|
235
|
+
expect(updateOutputMock).toHaveBeenCalledTimes(2);
|
|
236
|
+
expect(updateOutputMock).toHaveBeenLastCalledWith('[Receiving binary output... 2.0 KB received]');
|
|
237
|
+
resolveExecutionPromise({
|
|
238
|
+
rawOutput: Buffer.from(''),
|
|
239
|
+
output: '',
|
|
240
|
+
stdout: '',
|
|
241
|
+
stderr: '',
|
|
242
|
+
exitCode: 0,
|
|
243
|
+
signal: null,
|
|
244
|
+
error: null,
|
|
245
|
+
aborted: false,
|
|
246
|
+
pid: 12345,
|
|
247
|
+
});
|
|
248
|
+
await promise;
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
describe('shouldConfirmExecute', () => {
|
|
253
|
+
it('should request confirmation for a new command and whitelist it on "Always"', async () => {
|
|
254
|
+
const params = { command: 'npm install' };
|
|
255
|
+
const confirmation = await shellTool.shouldConfirmExecute(params, new AbortController().signal);
|
|
256
|
+
expect(confirmation).not.toBe(false);
|
|
257
|
+
expect(confirmation && confirmation.type).toBe('exec');
|
|
258
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
259
|
+
await confirmation.onConfirm(ToolConfirmationOutcome.ProceedAlways);
|
|
260
|
+
// Should now be whitelisted
|
|
261
|
+
const secondConfirmation = await shellTool.shouldConfirmExecute({ command: 'npm test' }, new AbortController().signal);
|
|
262
|
+
expect(secondConfirmation).toBe(false);
|
|
263
|
+
});
|
|
264
|
+
it('should skip confirmation if validation fails', async () => {
|
|
265
|
+
const confirmation = await shellTool.shouldConfirmExecute({ command: '' }, new AbortController().signal);
|
|
266
|
+
expect(confirmation).toBe(false);
|
|
267
|
+
});
|
|
47
268
|
});
|
|
48
|
-
|
|
269
|
+
});
|
|
270
|
+
describe('validateToolParams', () => {
|
|
271
|
+
it('should return null for valid directory', () => {
|
|
49
272
|
const config = {
|
|
50
273
|
getCoreTools: () => undefined,
|
|
51
|
-
getExcludeTools: () => ['ShellTool(rm -rf /)'],
|
|
52
|
-
};
|
|
53
|
-
const shellTool = new ShellTool(config);
|
|
54
|
-
const result = shellTool.isCommandAllowed('ls -l');
|
|
55
|
-
expect(result.allowed).toBe(true);
|
|
56
|
-
});
|
|
57
|
-
it('should block a command if it is in both the allowed and blocked lists', async () => {
|
|
58
|
-
const config = {
|
|
59
|
-
getCoreTools: () => ['ShellTool(rm -rf /)'],
|
|
60
|
-
getExcludeTools: () => ['ShellTool(rm -rf /)'],
|
|
61
|
-
};
|
|
62
|
-
const shellTool = new ShellTool(config);
|
|
63
|
-
const result = shellTool.isCommandAllowed('rm -rf /');
|
|
64
|
-
expect(result.allowed).toBe(false);
|
|
65
|
-
expect(result.reason).toBe("Command 'rm -rf /' is blocked by configuration");
|
|
66
|
-
});
|
|
67
|
-
it('should allow any command when ShellTool is in coreTools without specific commands', async () => {
|
|
68
|
-
const config = {
|
|
69
|
-
getCoreTools: () => ['ShellTool'],
|
|
70
|
-
getExcludeTools: () => [],
|
|
71
|
-
};
|
|
72
|
-
const shellTool = new ShellTool(config);
|
|
73
|
-
const result = shellTool.isCommandAllowed('any command');
|
|
74
|
-
expect(result.allowed).toBe(true);
|
|
75
|
-
});
|
|
76
|
-
it('should block any command when ShellTool is in excludeTools without specific commands', async () => {
|
|
77
|
-
const config = {
|
|
78
|
-
getCoreTools: () => [],
|
|
79
|
-
getExcludeTools: () => ['ShellTool'],
|
|
80
|
-
};
|
|
81
|
-
const shellTool = new ShellTool(config);
|
|
82
|
-
const result = shellTool.isCommandAllowed('any command');
|
|
83
|
-
expect(result.allowed).toBe(false);
|
|
84
|
-
expect(result.reason).toBe('Shell tool is globally disabled in configuration');
|
|
85
|
-
});
|
|
86
|
-
it('should allow a command if it is in the allowed list using the public-facing name', async () => {
|
|
87
|
-
const config = {
|
|
88
|
-
getCoreTools: () => ['run_shell_command(ls -l)'],
|
|
89
274
|
getExcludeTools: () => undefined,
|
|
275
|
+
getTargetDir: () => '/root',
|
|
276
|
+
getWorkspaceContext: () => createMockWorkspaceContext('/root', ['/users/test']),
|
|
90
277
|
};
|
|
91
278
|
const shellTool = new ShellTool(config);
|
|
92
|
-
const result = shellTool.
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
getCoreTools: () => undefined,
|
|
98
|
-
getExcludeTools: () => ['run_shell_command(rm -rf /)'],
|
|
99
|
-
};
|
|
100
|
-
const shellTool = new ShellTool(config);
|
|
101
|
-
const result = shellTool.isCommandAllowed('rm -rf /');
|
|
102
|
-
expect(result.allowed).toBe(false);
|
|
103
|
-
expect(result.reason).toBe("Command 'rm -rf /' is blocked by configuration");
|
|
104
|
-
});
|
|
105
|
-
it('should block any command when ShellTool is in excludeTools using the public-facing name', async () => {
|
|
106
|
-
const config = {
|
|
107
|
-
getCoreTools: () => [],
|
|
108
|
-
getExcludeTools: () => ['run_shell_command'],
|
|
109
|
-
};
|
|
110
|
-
const shellTool = new ShellTool(config);
|
|
111
|
-
const result = shellTool.isCommandAllowed('any command');
|
|
112
|
-
expect(result.allowed).toBe(false);
|
|
113
|
-
expect(result.reason).toBe('Shell tool is globally disabled in configuration');
|
|
114
|
-
});
|
|
115
|
-
it('should block any command if coreTools contains an empty ShellTool command list using the public-facing name', async () => {
|
|
116
|
-
const config = {
|
|
117
|
-
getCoreTools: () => ['run_shell_command()'],
|
|
118
|
-
getExcludeTools: () => [],
|
|
119
|
-
};
|
|
120
|
-
const shellTool = new ShellTool(config);
|
|
121
|
-
const result = shellTool.isCommandAllowed('any command');
|
|
122
|
-
expect(result.allowed).toBe(false);
|
|
123
|
-
expect(result.reason).toBe("Command 'any command' is not in the allowed commands list");
|
|
124
|
-
});
|
|
125
|
-
it('should block any command if coreTools contains an empty ShellTool command list', async () => {
|
|
126
|
-
const config = {
|
|
127
|
-
getCoreTools: () => ['ShellTool()'],
|
|
128
|
-
getExcludeTools: () => [],
|
|
129
|
-
};
|
|
130
|
-
const shellTool = new ShellTool(config);
|
|
131
|
-
const result = shellTool.isCommandAllowed('any command');
|
|
132
|
-
expect(result.allowed).toBe(false);
|
|
133
|
-
expect(result.reason).toBe("Command 'any command' is not in the allowed commands list");
|
|
279
|
+
const result = shellTool.validateToolParams({
|
|
280
|
+
command: 'ls',
|
|
281
|
+
directory: 'test',
|
|
282
|
+
});
|
|
283
|
+
expect(result).toBeNull();
|
|
134
284
|
});
|
|
135
|
-
it('should
|
|
285
|
+
it('should return error for directory outside workspace', () => {
|
|
136
286
|
const config = {
|
|
137
|
-
getCoreTools: () => undefined,
|
|
138
|
-
getExcludeTools: () => ['ShellTool(rm -rf /)'],
|
|
139
|
-
};
|
|
140
|
-
const shellTool = new ShellTool(config);
|
|
141
|
-
const result = shellTool.isCommandAllowed(' rm -rf / ');
|
|
142
|
-
expect(result.allowed).toBe(false);
|
|
143
|
-
expect(result.reason).toBe("Command 'rm -rf /' is blocked by configuration");
|
|
144
|
-
});
|
|
145
|
-
it('should allow any command when ShellTool is present with specific commands', async () => {
|
|
146
|
-
const config = {
|
|
147
|
-
getCoreTools: () => ['ShellTool', 'ShellTool(ls)'],
|
|
148
|
-
getExcludeTools: () => [],
|
|
149
|
-
};
|
|
150
|
-
const shellTool = new ShellTool(config);
|
|
151
|
-
const result = shellTool.isCommandAllowed('any command');
|
|
152
|
-
expect(result.allowed).toBe(true);
|
|
153
|
-
});
|
|
154
|
-
it('should block a command on the blocklist even with a wildcard allow', async () => {
|
|
155
|
-
const config = {
|
|
156
|
-
getCoreTools: () => ['ShellTool'],
|
|
157
|
-
getExcludeTools: () => ['ShellTool(rm -rf /)'],
|
|
158
|
-
};
|
|
159
|
-
const shellTool = new ShellTool(config);
|
|
160
|
-
const result = shellTool.isCommandAllowed('rm -rf /');
|
|
161
|
-
expect(result.allowed).toBe(false);
|
|
162
|
-
expect(result.reason).toBe("Command 'rm -rf /' is blocked by configuration");
|
|
163
|
-
});
|
|
164
|
-
it('should allow a command that starts with an allowed command prefix', async () => {
|
|
165
|
-
const config = {
|
|
166
|
-
getCoreTools: () => ['ShellTool(gh issue edit)'],
|
|
167
|
-
getExcludeTools: () => [],
|
|
168
|
-
};
|
|
169
|
-
const shellTool = new ShellTool(config);
|
|
170
|
-
const result = shellTool.isCommandAllowed('gh issue edit 1 --add-label "kind/feature"');
|
|
171
|
-
expect(result.allowed).toBe(true);
|
|
172
|
-
});
|
|
173
|
-
it('should allow a command that starts with an allowed command prefix using the public-facing name', async () => {
|
|
174
|
-
const config = {
|
|
175
|
-
getCoreTools: () => ['run_shell_command(gh issue edit)'],
|
|
176
|
-
getExcludeTools: () => [],
|
|
177
|
-
};
|
|
178
|
-
const shellTool = new ShellTool(config);
|
|
179
|
-
const result = shellTool.isCommandAllowed('gh issue edit 1 --add-label "kind/feature"');
|
|
180
|
-
expect(result.allowed).toBe(true);
|
|
181
|
-
});
|
|
182
|
-
it('should not allow a command that starts with an allowed command prefix but is chained with another command', async () => {
|
|
183
|
-
const config = {
|
|
184
|
-
getCoreTools: () => ['run_shell_command(gh issue edit)'],
|
|
185
|
-
getExcludeTools: () => [],
|
|
186
|
-
};
|
|
187
|
-
const shellTool = new ShellTool(config);
|
|
188
|
-
const result = shellTool.isCommandAllowed('gh issue edit&&rm -rf /');
|
|
189
|
-
expect(result.allowed).toBe(false);
|
|
190
|
-
expect(result.reason).toBe("Command 'rm -rf /' is not in the allowed commands list");
|
|
191
|
-
});
|
|
192
|
-
it('should not allow a command that is a prefix of an allowed command', async () => {
|
|
193
|
-
const config = {
|
|
194
|
-
getCoreTools: () => ['run_shell_command(gh issue edit)'],
|
|
195
|
-
getExcludeTools: () => [],
|
|
196
|
-
};
|
|
197
|
-
const shellTool = new ShellTool(config);
|
|
198
|
-
const result = shellTool.isCommandAllowed('gh issue');
|
|
199
|
-
expect(result.allowed).toBe(false);
|
|
200
|
-
expect(result.reason).toBe("Command 'gh issue' is not in the allowed commands list");
|
|
201
|
-
});
|
|
202
|
-
it('should not allow a command that is a prefix of a blocked command', async () => {
|
|
203
|
-
const config = {
|
|
204
|
-
getCoreTools: () => [],
|
|
205
|
-
getExcludeTools: () => ['run_shell_command(gh issue edit)'],
|
|
206
|
-
};
|
|
207
|
-
const shellTool = new ShellTool(config);
|
|
208
|
-
const result = shellTool.isCommandAllowed('gh issue');
|
|
209
|
-
expect(result.allowed).toBe(true);
|
|
210
|
-
});
|
|
211
|
-
it('should not allow a command that is chained with a pipe', async () => {
|
|
212
|
-
const config = {
|
|
213
|
-
getCoreTools: () => ['run_shell_command(gh issue list)'],
|
|
214
|
-
getExcludeTools: () => [],
|
|
215
|
-
};
|
|
216
|
-
const shellTool = new ShellTool(config);
|
|
217
|
-
const result = shellTool.isCommandAllowed('gh issue list | rm -rf /');
|
|
218
|
-
expect(result.allowed).toBe(false);
|
|
219
|
-
expect(result.reason).toBe("Command 'rm -rf /' is not in the allowed commands list");
|
|
220
|
-
});
|
|
221
|
-
it('should not allow a command that is chained with a semicolon', async () => {
|
|
222
|
-
const config = {
|
|
223
|
-
getCoreTools: () => ['run_shell_command(gh issue list)'],
|
|
224
|
-
getExcludeTools: () => [],
|
|
225
|
-
};
|
|
226
|
-
const shellTool = new ShellTool(config);
|
|
227
|
-
const result = shellTool.isCommandAllowed('gh issue list; rm -rf /');
|
|
228
|
-
expect(result.allowed).toBe(false);
|
|
229
|
-
expect(result.reason).toBe("Command 'rm -rf /' is not in the allowed commands list");
|
|
230
|
-
});
|
|
231
|
-
it('should block a chained command if any part is blocked', async () => {
|
|
232
|
-
const config = {
|
|
233
|
-
getCoreTools: () => ['run_shell_command(echo "hello")'],
|
|
234
|
-
getExcludeTools: () => ['run_shell_command(rm)'],
|
|
235
|
-
};
|
|
236
|
-
const shellTool = new ShellTool(config);
|
|
237
|
-
const result = shellTool.isCommandAllowed('echo "hello" && rm -rf /');
|
|
238
|
-
expect(result.allowed).toBe(false);
|
|
239
|
-
expect(result.reason).toBe("Command 'rm -rf /' is blocked by configuration");
|
|
240
|
-
});
|
|
241
|
-
it('should block a command if its prefix is on the blocklist, even if the command itself is on the allowlist', async () => {
|
|
242
|
-
const config = {
|
|
243
|
-
getCoreTools: () => ['run_shell_command(git push)'],
|
|
244
|
-
getExcludeTools: () => ['run_shell_command(git)'],
|
|
245
|
-
};
|
|
246
|
-
const shellTool = new ShellTool(config);
|
|
247
|
-
const result = shellTool.isCommandAllowed('git push');
|
|
248
|
-
expect(result.allowed).toBe(false);
|
|
249
|
-
expect(result.reason).toBe("Command 'git push' is blocked by configuration");
|
|
250
|
-
});
|
|
251
|
-
it('should be case-sensitive in its matching', async () => {
|
|
252
|
-
const config = {
|
|
253
|
-
getCoreTools: () => ['run_shell_command(echo)'],
|
|
254
|
-
getExcludeTools: () => [],
|
|
255
|
-
};
|
|
256
|
-
const shellTool = new ShellTool(config);
|
|
257
|
-
const result = shellTool.isCommandAllowed('ECHO "hello"');
|
|
258
|
-
expect(result.allowed).toBe(false);
|
|
259
|
-
expect(result.reason).toBe('Command \'ECHO "hello"\' is not in the allowed commands list');
|
|
260
|
-
});
|
|
261
|
-
it('should correctly handle commands with extra whitespace around chaining operators', async () => {
|
|
262
|
-
const config = {
|
|
263
|
-
getCoreTools: () => ['run_shell_command(ls -l)'],
|
|
264
|
-
getExcludeTools: () => ['run_shell_command(rm)'],
|
|
265
|
-
};
|
|
266
|
-
const shellTool = new ShellTool(config);
|
|
267
|
-
const result = shellTool.isCommandAllowed('ls -l ; rm -rf /');
|
|
268
|
-
expect(result.allowed).toBe(false);
|
|
269
|
-
expect(result.reason).toBe("Command 'rm -rf /' is blocked by configuration");
|
|
270
|
-
});
|
|
271
|
-
it('should allow a chained command if all parts are allowed', async () => {
|
|
272
|
-
const config = {
|
|
273
|
-
getCoreTools: () => [
|
|
274
|
-
'run_shell_command(echo)',
|
|
275
|
-
'run_shell_command(ls -l)',
|
|
276
|
-
],
|
|
277
|
-
getExcludeTools: () => [],
|
|
278
|
-
};
|
|
279
|
-
const shellTool = new ShellTool(config);
|
|
280
|
-
const result = shellTool.isCommandAllowed('echo "hello" && ls -l');
|
|
281
|
-
expect(result.allowed).toBe(true);
|
|
282
|
-
});
|
|
283
|
-
it('should allow a command with command substitution using backticks', async () => {
|
|
284
|
-
const config = {
|
|
285
|
-
getCoreTools: () => ['run_shell_command(echo)'],
|
|
286
|
-
getExcludeTools: () => [],
|
|
287
|
-
};
|
|
288
|
-
const shellTool = new ShellTool(config);
|
|
289
|
-
const result = shellTool.isCommandAllowed('echo `rm -rf /`');
|
|
290
|
-
expect(result.allowed).toBe(true);
|
|
291
|
-
});
|
|
292
|
-
it('should block a command with command substitution using $()', async () => {
|
|
293
|
-
const config = {
|
|
294
|
-
getCoreTools: () => ['run_shell_command(echo)'],
|
|
295
|
-
getExcludeTools: () => [],
|
|
296
|
-
};
|
|
297
|
-
const shellTool = new ShellTool(config);
|
|
298
|
-
const result = shellTool.isCommandAllowed('echo $(rm -rf /)');
|
|
299
|
-
expect(result.allowed).toBe(false);
|
|
300
|
-
expect(result.reason).toBe('Command substitution using $() is not allowed for security reasons');
|
|
301
|
-
});
|
|
302
|
-
it('should allow a command with I/O redirection', async () => {
|
|
303
|
-
const config = {
|
|
304
|
-
getCoreTools: () => ['run_shell_command(echo)'],
|
|
305
|
-
getExcludeTools: () => [],
|
|
306
|
-
};
|
|
307
|
-
const shellTool = new ShellTool(config);
|
|
308
|
-
const result = shellTool.isCommandAllowed('echo "hello" > file.txt');
|
|
309
|
-
expect(result.allowed).toBe(true);
|
|
310
|
-
});
|
|
311
|
-
it('should not allow a command that is chained with a double pipe', async () => {
|
|
312
|
-
const config = {
|
|
313
|
-
getCoreTools: () => ['run_shell_command(gh issue list)'],
|
|
314
|
-
getExcludeTools: () => [],
|
|
315
|
-
};
|
|
316
|
-
const shellTool = new ShellTool(config);
|
|
317
|
-
const result = shellTool.isCommandAllowed('gh issue list || rm -rf /');
|
|
318
|
-
expect(result.allowed).toBe(false);
|
|
319
|
-
expect(result.reason).toBe("Command 'rm -rf /' is not in the allowed commands list");
|
|
320
|
-
});
|
|
321
|
-
});
|
|
322
|
-
describe('ShellTool Bug Reproduction', () => {
|
|
323
|
-
let shellTool;
|
|
324
|
-
let config;
|
|
325
|
-
beforeEach(() => {
|
|
326
|
-
config = {
|
|
327
|
-
getCoreTools: () => undefined,
|
|
328
|
-
getExcludeTools: () => undefined,
|
|
329
|
-
getDebugMode: () => false,
|
|
330
|
-
getGeminiClient: () => ({}),
|
|
331
|
-
getTargetDir: () => '.',
|
|
332
|
-
getSummarizeToolOutputConfig: () => ({
|
|
333
|
-
[shellTool.name]: {},
|
|
334
|
-
}),
|
|
335
|
-
};
|
|
336
|
-
shellTool = new ShellTool(config);
|
|
337
|
-
});
|
|
338
|
-
it('should not let the summarizer override the return display', async () => {
|
|
339
|
-
const summarizeSpy = vi
|
|
340
|
-
.spyOn(summarizer, 'summarizeToolOutput')
|
|
341
|
-
.mockResolvedValue('summarized output');
|
|
342
|
-
const abortSignal = new AbortController().signal;
|
|
343
|
-
const result = await shellTool.execute({ command: 'echo "hello"' }, abortSignal);
|
|
344
|
-
expect(result.returnDisplay).toBe('hello\n');
|
|
345
|
-
expect(result.llmContent).toBe('summarized output');
|
|
346
|
-
expect(summarizeSpy).toHaveBeenCalled();
|
|
347
|
-
});
|
|
348
|
-
it('should not call summarizer if disabled in config', async () => {
|
|
349
|
-
config = {
|
|
350
|
-
getCoreTools: () => undefined,
|
|
351
|
-
getExcludeTools: () => undefined,
|
|
352
|
-
getDebugMode: () => false,
|
|
353
|
-
getGeminiClient: () => ({}),
|
|
354
|
-
getTargetDir: () => '.',
|
|
355
|
-
getSummarizeToolOutputConfig: () => ({}),
|
|
356
|
-
};
|
|
357
|
-
shellTool = new ShellTool(config);
|
|
358
|
-
const summarizeSpy = vi
|
|
359
|
-
.spyOn(summarizer, 'summarizeToolOutput')
|
|
360
|
-
.mockResolvedValue('summarized output');
|
|
361
|
-
const abortSignal = new AbortController().signal;
|
|
362
|
-
const result = await shellTool.execute({ command: 'echo "hello"' }, abortSignal);
|
|
363
|
-
expect(result.returnDisplay).toBe('hello\n');
|
|
364
|
-
expect(result.llmContent).not.toBe('summarized output');
|
|
365
|
-
expect(summarizeSpy).not.toHaveBeenCalled();
|
|
366
|
-
});
|
|
367
|
-
it('should pass token budget to summarizer', async () => {
|
|
368
|
-
config = {
|
|
369
287
|
getCoreTools: () => undefined,
|
|
370
288
|
getExcludeTools: () => undefined,
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
getTargetDir: () => '.',
|
|
374
|
-
getSummarizeToolOutputConfig: () => ({
|
|
375
|
-
[shellTool.name]: { tokenBudget: 1000 },
|
|
376
|
-
}),
|
|
289
|
+
getTargetDir: () => '/root',
|
|
290
|
+
getWorkspaceContext: () => createMockWorkspaceContext('/root', ['/users/test']),
|
|
377
291
|
};
|
|
378
|
-
shellTool = new ShellTool(config);
|
|
379
|
-
const
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
expect(summarizeSpy).toHaveBeenCalledWith(expect.any(String), expect.any(Object), expect.any(Object), 1000);
|
|
385
|
-
});
|
|
386
|
-
it('should use default token budget if not specified', async () => {
|
|
387
|
-
config = {
|
|
388
|
-
getCoreTools: () => undefined,
|
|
389
|
-
getExcludeTools: () => undefined,
|
|
390
|
-
getDebugMode: () => false,
|
|
391
|
-
getGeminiClient: () => ({}),
|
|
392
|
-
getTargetDir: () => '.',
|
|
393
|
-
getSummarizeToolOutputConfig: () => ({
|
|
394
|
-
[shellTool.name]: {},
|
|
395
|
-
}),
|
|
396
|
-
};
|
|
397
|
-
shellTool = new ShellTool(config);
|
|
398
|
-
const summarizeSpy = vi
|
|
399
|
-
.spyOn(summarizer, 'summarizeToolOutput')
|
|
400
|
-
.mockResolvedValue('summarized output');
|
|
401
|
-
const abortSignal = new AbortController().signal;
|
|
402
|
-
await shellTool.execute({ command: 'echo "hello"' }, abortSignal);
|
|
403
|
-
expect(summarizeSpy).toHaveBeenCalledWith(expect.any(String), expect.any(Object), expect.any(Object), undefined);
|
|
292
|
+
const shellTool = new ShellTool(config);
|
|
293
|
+
const result = shellTool.validateToolParams({
|
|
294
|
+
command: 'ls',
|
|
295
|
+
directory: 'test2',
|
|
296
|
+
});
|
|
297
|
+
expect(result).toContain('is not a registered workspace directory');
|
|
404
298
|
});
|
|
405
299
|
});
|
|
406
300
|
//# sourceMappingURL=shell.test.js.map
|