@google/gemini-cli-core 0.1.13 → 0.1.15

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.
Files changed (234) hide show
  1. package/README.md +22 -1
  2. package/dist/google-gemini-cli-core-0.1.13.tgz +0 -0
  3. package/dist/src/code_assist/codeAssist.js +2 -2
  4. package/dist/src/code_assist/codeAssist.js.map +1 -1
  5. package/dist/src/code_assist/oauth2.js +9 -2
  6. package/dist/src/code_assist/oauth2.js.map +1 -1
  7. package/dist/src/code_assist/oauth2.test.js +99 -7
  8. package/dist/src/code_assist/oauth2.test.js.map +1 -1
  9. package/dist/src/code_assist/server.d.ts +4 -6
  10. package/dist/src/code_assist/server.js +4 -69
  11. package/dist/src/code_assist/server.js.map +1 -1
  12. package/dist/src/code_assist/setup.d.ts +6 -1
  13. package/dist/src/code_assist/setup.js +4 -1
  14. package/dist/src/code_assist/setup.js.map +1 -1
  15. package/dist/src/code_assist/setup.test.js +4 -1
  16. package/dist/src/code_assist/setup.test.js.map +1 -1
  17. package/dist/src/code_assist/types.d.ts +2 -2
  18. package/dist/src/config/config.d.ts +28 -7
  19. package/dist/src/config/config.js +52 -16
  20. package/dist/src/config/config.js.map +1 -1
  21. package/dist/src/config/config.test.js +1 -23
  22. package/dist/src/config/config.test.js.map +1 -1
  23. package/dist/src/config/flashFallback.test.js +1 -1
  24. package/dist/src/config/flashFallback.test.js.map +1 -1
  25. package/dist/src/core/client.d.ts +5 -2
  26. package/dist/src/core/client.js +39 -17
  27. package/dist/src/core/client.js.map +1 -1
  28. package/dist/src/core/client.test.js +51 -0
  29. package/dist/src/core/client.test.js.map +1 -1
  30. package/dist/src/core/contentGenerator.d.ts +1 -1
  31. package/dist/src/core/contentGenerator.js +1 -1
  32. package/dist/src/core/contentGenerator.js.map +1 -1
  33. package/dist/src/core/geminiChat.d.ts +4 -3
  34. package/dist/src/core/geminiChat.js +8 -11
  35. package/dist/src/core/geminiChat.js.map +1 -1
  36. package/dist/src/core/geminiRequest.js +2 -37
  37. package/dist/src/core/geminiRequest.js.map +1 -1
  38. package/dist/src/core/logger.js +6 -0
  39. package/dist/src/core/logger.js.map +1 -1
  40. package/dist/src/core/logger.test.js +1 -1
  41. package/dist/src/core/logger.test.js.map +1 -1
  42. package/dist/src/core/nonInteractiveToolExecutor.test.js +5 -5
  43. package/dist/src/core/prompts.js +42 -18
  44. package/dist/src/core/prompts.js.map +1 -1
  45. package/dist/src/core/prompts.test.js +121 -4
  46. package/dist/src/core/prompts.test.js.map +1 -1
  47. package/dist/src/core/turn.d.ts +7 -2
  48. package/dist/src/core/turn.js +9 -0
  49. package/dist/src/core/turn.js.map +1 -1
  50. package/dist/src/core/turn.test.js +129 -0
  51. package/dist/src/core/turn.test.js.map +1 -1
  52. package/dist/src/ide/ide-client.d.ts +28 -0
  53. package/dist/src/ide/ide-client.js +88 -0
  54. package/dist/src/ide/ide-client.js.map +1 -0
  55. package/dist/src/ide/ideContext.d.ts +174 -0
  56. package/dist/src/{services → ide}/ideContext.js +28 -25
  57. package/dist/src/ide/ideContext.js.map +1 -0
  58. package/dist/src/{services → ide}/ideContext.test.js +39 -39
  59. package/dist/src/ide/ideContext.test.js.map +1 -0
  60. package/dist/src/index.d.ts +8 -1
  61. package/dist/src/index.js +11 -1
  62. package/dist/src/index.js.map +1 -1
  63. package/dist/src/mcp/google-auth-provider.d.ts +23 -0
  64. package/dist/src/mcp/google-auth-provider.js +63 -0
  65. package/dist/src/mcp/google-auth-provider.js.map +1 -0
  66. package/dist/src/mcp/google-auth-provider.test.js +54 -0
  67. package/dist/src/mcp/google-auth-provider.test.js.map +1 -0
  68. package/dist/src/mcp/oauth-provider.d.ts +5 -1
  69. package/dist/src/mcp/oauth-provider.js +36 -11
  70. package/dist/src/mcp/oauth-provider.js.map +1 -1
  71. package/dist/src/mcp/oauth-provider.test.js +2 -2
  72. package/dist/src/mcp/oauth-provider.test.js.map +1 -1
  73. package/dist/src/mcp/oauth-token-storage.d.ts +3 -1
  74. package/dist/src/mcp/oauth-token-storage.js +3 -1
  75. package/dist/src/mcp/oauth-token-storage.js.map +1 -1
  76. package/dist/src/prompts/mcp-prompts.d.ts +8 -0
  77. package/dist/src/prompts/mcp-prompts.js +13 -0
  78. package/dist/src/prompts/mcp-prompts.js.map +1 -0
  79. package/dist/src/prompts/prompt-registry.d.ts +26 -0
  80. package/dist/src/prompts/prompt-registry.js +47 -0
  81. package/dist/src/prompts/prompt-registry.js.map +1 -0
  82. package/dist/src/services/fileDiscoveryService.test.js +101 -60
  83. package/dist/src/services/fileDiscoveryService.test.js.map +1 -1
  84. package/dist/src/services/gitService.test.js +67 -86
  85. package/dist/src/services/gitService.test.js.map +1 -1
  86. package/dist/src/services/loopDetectionService.d.ts +48 -5
  87. package/dist/src/services/loopDetectionService.js +124 -38
  88. package/dist/src/services/loopDetectionService.js.map +1 -1
  89. package/dist/src/services/loopDetectionService.test.js +39 -112
  90. package/dist/src/services/loopDetectionService.test.js.map +1 -1
  91. package/dist/src/services/shellExecutionService.d.ts +70 -0
  92. package/dist/src/services/shellExecutionService.js +152 -0
  93. package/dist/src/services/shellExecutionService.js.map +1 -0
  94. package/dist/src/services/shellExecutionService.test.d.ts +6 -0
  95. package/dist/src/services/shellExecutionService.test.js +258 -0
  96. package/dist/src/services/shellExecutionService.test.js.map +1 -0
  97. package/dist/src/telemetry/clearcut-logger/clearcut-logger.d.ts +2 -1
  98. package/dist/src/telemetry/clearcut-logger/clearcut-logger.js +17 -2
  99. package/dist/src/telemetry/clearcut-logger/clearcut-logger.js.map +1 -1
  100. package/dist/src/telemetry/constants.d.ts +1 -0
  101. package/dist/src/telemetry/constants.js +1 -0
  102. package/dist/src/telemetry/constants.js.map +1 -1
  103. package/dist/src/telemetry/file-exporters.d.ts +28 -0
  104. package/dist/src/telemetry/file-exporters.js +62 -0
  105. package/dist/src/telemetry/file-exporters.js.map +1 -0
  106. package/dist/src/telemetry/loggers.d.ts +2 -1
  107. package/dist/src/telemetry/loggers.js +17 -1
  108. package/dist/src/telemetry/loggers.js.map +1 -1
  109. package/dist/src/telemetry/sdk.js +17 -6
  110. package/dist/src/telemetry/sdk.js.map +1 -1
  111. package/dist/src/telemetry/types.d.ts +9 -2
  112. package/dist/src/telemetry/types.js +13 -1
  113. package/dist/src/telemetry/types.js.map +1 -1
  114. package/dist/src/tools/edit.js +10 -4
  115. package/dist/src/tools/edit.js.map +1 -1
  116. package/dist/src/tools/edit.test.js +12 -0
  117. package/dist/src/tools/edit.test.js.map +1 -1
  118. package/dist/src/tools/glob.test.js +7 -4
  119. package/dist/src/tools/glob.test.js.map +1 -1
  120. package/dist/src/tools/grep.test.js +5 -5
  121. package/dist/src/tools/grep.test.js.map +1 -1
  122. package/dist/src/tools/ls.d.ts +5 -2
  123. package/dist/src/tools/ls.js +39 -10
  124. package/dist/src/tools/ls.js.map +1 -1
  125. package/dist/src/tools/mcp-client.d.ts +31 -3
  126. package/dist/src/tools/mcp-client.js +478 -38
  127. package/dist/src/tools/mcp-client.js.map +1 -1
  128. package/dist/src/tools/mcp-client.test.js +99 -7
  129. package/dist/src/tools/mcp-client.test.js.map +1 -1
  130. package/dist/src/tools/mcp-tool.js +1 -1
  131. package/dist/src/tools/mcp-tool.js.map +1 -1
  132. package/dist/src/tools/mcp-tool.test.js +34 -0
  133. package/dist/src/tools/mcp-tool.test.js.map +1 -1
  134. package/dist/src/tools/modifiable-tool.test.js +51 -62
  135. package/dist/src/tools/modifiable-tool.test.js.map +1 -1
  136. package/dist/src/tools/read-file.test.js +98 -69
  137. package/dist/src/tools/read-file.test.js.map +1 -1
  138. package/dist/src/tools/read-many-files.d.ts +5 -3
  139. package/dist/src/tools/read-many-files.js +62 -22
  140. package/dist/src/tools/read-many-files.js.map +1 -1
  141. package/dist/src/tools/read-many-files.test.js +5 -2
  142. package/dist/src/tools/read-many-files.test.js.map +1 -1
  143. package/dist/src/tools/shell.d.ts +3 -23
  144. package/dist/src/tools/shell.js +165 -296
  145. package/dist/src/tools/shell.js.map +1 -1
  146. package/dist/src/tools/shell.test.js +254 -392
  147. package/dist/src/tools/shell.test.js.map +1 -1
  148. package/dist/src/tools/tool-registry.d.ts +13 -1
  149. package/dist/src/tools/tool-registry.js +46 -2
  150. package/dist/src/tools/tool-registry.js.map +1 -1
  151. package/dist/src/tools/tool-registry.test.js +5 -5
  152. package/dist/src/tools/tool-registry.test.js.map +1 -1
  153. package/dist/src/utils/bfsFileSearch.d.ts +2 -0
  154. package/dist/src/utils/bfsFileSearch.js +4 -1
  155. package/dist/src/utils/bfsFileSearch.js.map +1 -1
  156. package/dist/src/utils/bfsFileSearch.test.js +108 -105
  157. package/dist/src/utils/bfsFileSearch.test.js.map +1 -1
  158. package/dist/src/utils/editCorrector.js +4 -4
  159. package/dist/src/utils/editCorrector.js.map +1 -1
  160. package/dist/src/utils/editCorrector.test.js +1 -1
  161. package/dist/src/utils/editor.js +16 -10
  162. package/dist/src/utils/editor.js.map +1 -1
  163. package/dist/src/utils/editor.test.js +128 -28
  164. package/dist/src/utils/editor.test.js.map +1 -1
  165. package/dist/src/utils/errorReporting.d.ts +1 -1
  166. package/dist/src/utils/errorReporting.js +2 -2
  167. package/dist/src/utils/errorReporting.js.map +1 -1
  168. package/dist/src/utils/errorReporting.test.js +44 -38
  169. package/dist/src/utils/errorReporting.test.js.map +1 -1
  170. package/dist/src/utils/fileUtils.d.ts +4 -4
  171. package/dist/src/utils/fileUtils.js +31 -15
  172. package/dist/src/utils/fileUtils.js.map +1 -1
  173. package/dist/src/utils/fileUtils.test.js +37 -37
  174. package/dist/src/utils/fileUtils.test.js.map +1 -1
  175. package/dist/src/utils/formatters.d.ts +6 -0
  176. package/dist/src/utils/formatters.js +16 -0
  177. package/dist/src/utils/formatters.js.map +1 -0
  178. package/dist/src/utils/getFolderStructure.d.ts +3 -2
  179. package/dist/src/utils/getFolderStructure.js +27 -28
  180. package/dist/src/utils/getFolderStructure.js.map +1 -1
  181. package/dist/src/utils/getFolderStructure.test.js +169 -187
  182. package/dist/src/utils/getFolderStructure.test.js.map +1 -1
  183. package/dist/src/utils/gitIgnoreParser.js +4 -7
  184. package/dist/src/utils/gitIgnoreParser.js.map +1 -1
  185. package/dist/src/utils/gitIgnoreParser.test.js +70 -61
  186. package/dist/src/utils/gitIgnoreParser.test.js.map +1 -1
  187. package/dist/src/utils/memoryDiscovery.d.ts +2 -1
  188. package/dist/src/utils/memoryDiscovery.js +11 -5
  189. package/dist/src/utils/memoryDiscovery.js.map +1 -1
  190. package/dist/src/utils/memoryDiscovery.test.js +160 -371
  191. package/dist/src/utils/memoryDiscovery.test.js.map +1 -1
  192. package/dist/src/utils/partUtils.d.ts +14 -0
  193. package/dist/src/utils/partUtils.js +65 -0
  194. package/dist/src/utils/partUtils.js.map +1 -0
  195. package/dist/src/utils/partUtils.test.d.ts +6 -0
  196. package/dist/src/utils/partUtils.test.js +130 -0
  197. package/dist/src/utils/partUtils.test.js.map +1 -0
  198. package/dist/src/utils/paths.d.ts +11 -0
  199. package/dist/src/utils/paths.js +17 -1
  200. package/dist/src/utils/paths.js.map +1 -1
  201. package/dist/src/utils/quotaErrorDetection.js +0 -2
  202. package/dist/src/utils/quotaErrorDetection.js.map +1 -1
  203. package/dist/src/utils/retry.js +1 -1
  204. package/dist/src/utils/retry.js.map +1 -1
  205. package/dist/src/utils/schemaValidator.d.ts +1 -1
  206. package/dist/src/utils/schemaValidator.js +6 -3
  207. package/dist/src/utils/schemaValidator.js.map +1 -1
  208. package/dist/src/utils/shell-utils.d.ts +78 -0
  209. package/dist/src/utils/shell-utils.js +306 -0
  210. package/dist/src/utils/shell-utils.js.map +1 -0
  211. package/dist/src/utils/shell-utils.test.d.ts +6 -0
  212. package/dist/src/utils/shell-utils.test.js +200 -0
  213. package/dist/src/utils/shell-utils.test.js.map +1 -0
  214. package/dist/src/utils/summarizer.js +1 -30
  215. package/dist/src/utils/summarizer.js.map +1 -1
  216. package/dist/src/utils/systemEncoding.d.ts +40 -0
  217. package/dist/src/utils/systemEncoding.js +149 -0
  218. package/dist/src/utils/systemEncoding.js.map +1 -0
  219. package/dist/src/utils/systemEncoding.test.d.ts +6 -0
  220. package/dist/src/utils/systemEncoding.test.js +368 -0
  221. package/dist/src/utils/systemEncoding.test.js.map +1 -0
  222. package/dist/src/utils/textUtils.d.ts +13 -0
  223. package/dist/src/utils/textUtils.js +28 -0
  224. package/dist/src/utils/textUtils.js.map +1 -0
  225. package/dist/tsconfig.tsbuildinfo +1 -1
  226. package/package.json +2 -1
  227. package/dist/google-gemini-cli-core-0.1.12.tgz +0 -0
  228. package/dist/src/core/geminiRequest.test.js +0 -72
  229. package/dist/src/core/geminiRequest.test.js.map +0 -1
  230. package/dist/src/services/ideContext.d.ts +0 -126
  231. package/dist/src/services/ideContext.js.map +0 -1
  232. package/dist/src/services/ideContext.test.js.map +0 -1
  233. /package/dist/src/{services → ide}/ideContext.test.d.ts +0 -0
  234. /package/dist/src/{core/geminiRequest.test.d.ts → mcp/google-auth-provider.test.d.ts} +0 -0
@@ -3,404 +3,266 @@
3
3
  * Copyright 2025 Google LLC
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
- import { expect, describe, it, vi, beforeEach } from 'vitest';
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';
9
24
  describe('ShellTool', () => {
10
- it('should allow a command if no restrictions are provided', async () => {
11
- const config = {
12
- getCoreTools: () => undefined,
13
- getExcludeTools: () => undefined,
14
- };
15
- const shellTool = new ShellTool(config);
16
- const result = shellTool.isCommandAllowed('ls -l');
17
- expect(result.allowed).toBe(true);
18
- });
19
- it('should allow a command if it is in the allowed list', async () => {
20
- const config = {
21
- getCoreTools: () => ['ShellTool(ls -l)'],
22
- getExcludeTools: () => undefined,
23
- };
24
- const shellTool = new ShellTool(config);
25
- const result = shellTool.isCommandAllowed('ls -l');
26
- expect(result.allowed).toBe(true);
27
- });
28
- it('should block a command if it is not in the allowed list', async () => {
29
- const config = {
30
- getCoreTools: () => ['ShellTool(ls -l)'],
31
- getExcludeTools: () => undefined,
32
- };
33
- const shellTool = new ShellTool(config);
34
- const result = shellTool.isCommandAllowed('rm -rf /');
35
- expect(result.allowed).toBe(false);
36
- expect(result.reason).toBe("Command 'rm -rf /' is not in the allowed commands list");
37
- });
38
- it('should block a command if it is in the blocked list', async () => {
39
- const config = {
40
- getCoreTools: () => undefined,
41
- getExcludeTools: () => ['ShellTool(rm -rf /)'],
42
- };
43
- const shellTool = new ShellTool(config);
44
- const result = shellTool.isCommandAllowed('rm -rf /');
45
- expect(result.allowed).toBe(false);
46
- expect(result.reason).toBe("Command 'rm -rf /' is blocked by configuration");
47
- });
48
- it('should allow a command if it is not in the blocked list', async () => {
49
- const config = {
50
- 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
- getExcludeTools: () => undefined,
90
- };
91
- const shellTool = new ShellTool(config);
92
- const result = shellTool.isCommandAllowed('ls -l');
93
- expect(result.allowed).toBe(true);
94
- });
95
- it('should block a command if it is in the blocked list using the public-facing name', async () => {
96
- const config = {
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");
134
- });
135
- it('should block a command with extra whitespace if it is in the blocked list', async () => {
136
- 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
25
  let shellTool;
324
- let config;
26
+ let mockConfig;
27
+ let mockShellOutputCallback;
28
+ let resolveExecutionPromise;
325
29
  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
- getCoreTools: () => undefined,
370
- getExcludeTools: () => undefined,
371
- getDebugMode: () => false,
372
- getGeminiClient: () => ({}),
373
- getTargetDir: () => '.',
374
- getSummarizeToolOutputConfig: () => ({
30
+ vi.clearAllMocks();
31
+ mockConfig = {
32
+ getCoreTools: vi.fn().mockReturnValue([]),
33
+ getExcludeTools: vi.fn().mockReturnValue([]),
34
+ getDebugMode: vi.fn().mockReturnValue(false),
35
+ getTargetDir: vi.fn().mockReturnValue('/test/dir'),
36
+ getSummarizeToolOutputConfig: vi.fn().mockReturnValue(undefined),
37
+ getGeminiClient: vi.fn(),
38
+ };
39
+ shellTool = new ShellTool(mockConfig);
40
+ vi.mocked(os.platform).mockReturnValue('linux');
41
+ vi.mocked(os.tmpdir).mockReturnValue('/tmp');
42
+ vi.mocked(crypto.randomBytes).mockReturnValue(Buffer.from('abcdef', 'hex'));
43
+ // Capture the output callback to simulate streaming events from the service
44
+ mockShellExecutionService.mockImplementation((_cmd, _cwd, callback) => {
45
+ mockShellOutputCallback = callback;
46
+ return {
47
+ pid: 12345,
48
+ result: new Promise((resolve) => {
49
+ resolveExecutionPromise = resolve;
50
+ }),
51
+ };
52
+ });
53
+ });
54
+ describe('isCommandAllowed', () => {
55
+ it('should allow a command if no restrictions are provided', () => {
56
+ mockConfig.getCoreTools.mockReturnValue(undefined);
57
+ mockConfig.getExcludeTools.mockReturnValue(undefined);
58
+ expect(isCommandAllowed('ls -l', mockConfig).allowed).toBe(true);
59
+ });
60
+ it('should block a command with command substitution using $()', () => {
61
+ expect(isCommandAllowed('echo $(rm -rf /)', mockConfig).allowed).toBe(false);
62
+ });
63
+ });
64
+ describe('validateToolParams', () => {
65
+ it('should return null for a valid command', () => {
66
+ expect(shellTool.validateToolParams({ command: 'ls -l' })).toBeNull();
67
+ });
68
+ it('should return an error for an empty command', () => {
69
+ expect(shellTool.validateToolParams({ command: ' ' })).toBe('Command cannot be empty.');
70
+ });
71
+ it('should return an error for a non-existent directory', () => {
72
+ vi.mocked(fs.existsSync).mockReturnValue(false);
73
+ expect(shellTool.validateToolParams({ command: 'ls', directory: 'rel/path' })).toBe('Directory must exist.');
74
+ });
75
+ });
76
+ describe('execute', () => {
77
+ const mockAbortSignal = new AbortController().signal;
78
+ const resolveShellExecution = (result = {}) => {
79
+ const fullResult = {
80
+ rawOutput: Buffer.from(result.output || ''),
81
+ output: 'Success',
82
+ stdout: 'Success',
83
+ stderr: '',
84
+ exitCode: 0,
85
+ signal: null,
86
+ error: null,
87
+ aborted: false,
88
+ pid: 12345,
89
+ ...result,
90
+ };
91
+ resolveExecutionPromise(fullResult);
92
+ };
93
+ it('should wrap command on linux and parse pgrep output', async () => {
94
+ const promise = shellTool.execute({ command: 'my-command &' }, mockAbortSignal);
95
+ resolveShellExecution({ pid: 54321 });
96
+ vi.mocked(fs.existsSync).mockReturnValue(true);
97
+ vi.mocked(fs.readFileSync).mockReturnValue('54321\n54322\n'); // Service PID and background PID
98
+ const result = await promise;
99
+ const tmpFile = path.join(os.tmpdir(), 'shell_pgrep_abcdef.tmp');
100
+ const wrappedCommand = `{ my-command & }; __code=$?; pgrep -g 0 >${tmpFile} 2>&1; exit $__code;`;
101
+ expect(mockShellExecutionService).toHaveBeenCalledWith(wrappedCommand, expect.any(String), expect.any(Function), mockAbortSignal);
102
+ expect(result.llmContent).toContain('Background PIDs: 54322');
103
+ expect(vi.mocked(fs.unlinkSync)).toHaveBeenCalledWith(tmpFile);
104
+ });
105
+ it('should not wrap command on windows', async () => {
106
+ vi.mocked(os.platform).mockReturnValue('win32');
107
+ const promise = shellTool.execute({ command: 'dir' }, mockAbortSignal);
108
+ resolveExecutionPromise({
109
+ rawOutput: Buffer.from(''),
110
+ output: '',
111
+ stdout: '',
112
+ stderr: '',
113
+ exitCode: 0,
114
+ signal: null,
115
+ error: null,
116
+ aborted: false,
117
+ pid: 12345,
118
+ });
119
+ await promise;
120
+ expect(mockShellExecutionService).toHaveBeenCalledWith('dir', expect.any(String), expect.any(Function), mockAbortSignal);
121
+ });
122
+ it('should format error messages correctly', async () => {
123
+ const error = new Error('wrapped command failed');
124
+ const promise = shellTool.execute({ command: 'user-command' }, mockAbortSignal);
125
+ resolveShellExecution({
126
+ error,
127
+ exitCode: 1,
128
+ output: 'err',
129
+ stderr: 'err',
130
+ rawOutput: Buffer.from('err'),
131
+ stdout: '',
132
+ signal: null,
133
+ aborted: false,
134
+ pid: 12345,
135
+ });
136
+ const result = await promise;
137
+ // The final llmContent should contain the user's command, not the wrapper
138
+ expect(result.llmContent).toContain('Error: wrapped command failed');
139
+ expect(result.llmContent).not.toContain('pgrep');
140
+ });
141
+ it('should summarize output when configured', async () => {
142
+ mockConfig.getSummarizeToolOutputConfig.mockReturnValue({
375
143
  [shellTool.name]: { tokenBudget: 1000 },
376
- }),
377
- };
378
- shellTool = new ShellTool(config);
379
- const summarizeSpy = vi
380
- .spyOn(summarizer, 'summarizeToolOutput')
381
- .mockResolvedValue('summarized output');
382
- const abortSignal = new AbortController().signal;
383
- await shellTool.execute({ command: 'echo "hello"' }, abortSignal);
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);
144
+ });
145
+ vi.mocked(summarizer.summarizeToolOutput).mockResolvedValue('summarized output');
146
+ const promise = shellTool.execute({ command: 'ls' }, mockAbortSignal);
147
+ resolveExecutionPromise({
148
+ output: 'long output',
149
+ rawOutput: Buffer.from('long output'),
150
+ stdout: 'long output',
151
+ stderr: '',
152
+ exitCode: 0,
153
+ signal: null,
154
+ error: null,
155
+ aborted: false,
156
+ pid: 12345,
157
+ });
158
+ const result = await promise;
159
+ expect(summarizer.summarizeToolOutput).toHaveBeenCalledWith(expect.any(String), mockConfig.getGeminiClient(), mockAbortSignal, 1000);
160
+ expect(result.llmContent).toBe('summarized output');
161
+ expect(result.returnDisplay).toBe('long output');
162
+ });
163
+ it('should clean up the temp file on synchronous execution error', async () => {
164
+ const error = new Error('sync spawn error');
165
+ mockShellExecutionService.mockImplementation(() => {
166
+ throw error;
167
+ });
168
+ vi.mocked(fs.existsSync).mockReturnValue(true); // Pretend the file exists
169
+ await expect(shellTool.execute({ command: 'a-command' }, mockAbortSignal)).rejects.toThrow(error);
170
+ const tmpFile = path.join(os.tmpdir(), 'shell_pgrep_abcdef.tmp');
171
+ expect(vi.mocked(fs.unlinkSync)).toHaveBeenCalledWith(tmpFile);
172
+ });
173
+ describe('Streaming to `updateOutput`', () => {
174
+ let updateOutputMock;
175
+ beforeEach(() => {
176
+ vi.useFakeTimers({ toFake: ['Date'] });
177
+ updateOutputMock = vi.fn();
178
+ });
179
+ afterEach(() => {
180
+ vi.useRealTimers();
181
+ });
182
+ it('should throttle text output updates', async () => {
183
+ const promise = shellTool.execute({ command: 'stream' }, mockAbortSignal, updateOutputMock);
184
+ // First chunk, should be throttled.
185
+ mockShellOutputCallback({
186
+ type: 'data',
187
+ stream: 'stdout',
188
+ chunk: 'hello ',
189
+ });
190
+ expect(updateOutputMock).not.toHaveBeenCalled();
191
+ // Advance time past the throttle interval.
192
+ await vi.advanceTimersByTimeAsync(OUTPUT_UPDATE_INTERVAL_MS + 1);
193
+ // Send a second chunk. THIS event triggers the update with the CUMULATIVE content.
194
+ mockShellOutputCallback({
195
+ type: 'data',
196
+ stream: 'stderr',
197
+ chunk: 'world',
198
+ });
199
+ // It should have been called once now with the combined output.
200
+ expect(updateOutputMock).toHaveBeenCalledOnce();
201
+ expect(updateOutputMock).toHaveBeenCalledWith('hello \nworld');
202
+ resolveExecutionPromise({
203
+ rawOutput: Buffer.from(''),
204
+ output: '',
205
+ stdout: '',
206
+ stderr: '',
207
+ exitCode: 0,
208
+ signal: null,
209
+ error: null,
210
+ aborted: false,
211
+ pid: 12345,
212
+ });
213
+ await promise;
214
+ });
215
+ it('should immediately show binary detection message and throttle progress', async () => {
216
+ const promise = shellTool.execute({ command: 'cat img' }, mockAbortSignal, updateOutputMock);
217
+ mockShellOutputCallback({ type: 'binary_detected' });
218
+ expect(updateOutputMock).toHaveBeenCalledOnce();
219
+ expect(updateOutputMock).toHaveBeenCalledWith('[Binary output detected. Halting stream...]');
220
+ mockShellOutputCallback({
221
+ type: 'binary_progress',
222
+ bytesReceived: 1024,
223
+ });
224
+ expect(updateOutputMock).toHaveBeenCalledOnce();
225
+ // Advance time past the throttle interval.
226
+ await vi.advanceTimersByTimeAsync(OUTPUT_UPDATE_INTERVAL_MS + 1);
227
+ // Send a SECOND progress event. This one will trigger the flush.
228
+ mockShellOutputCallback({
229
+ type: 'binary_progress',
230
+ bytesReceived: 2048,
231
+ });
232
+ // Now it should be called a second time with the latest progress.
233
+ expect(updateOutputMock).toHaveBeenCalledTimes(2);
234
+ expect(updateOutputMock).toHaveBeenLastCalledWith('[Receiving binary output... 2.0 KB received]');
235
+ resolveExecutionPromise({
236
+ rawOutput: Buffer.from(''),
237
+ output: '',
238
+ stdout: '',
239
+ stderr: '',
240
+ exitCode: 0,
241
+ signal: null,
242
+ error: null,
243
+ aborted: false,
244
+ pid: 12345,
245
+ });
246
+ await promise;
247
+ });
248
+ });
249
+ });
250
+ describe('shouldConfirmExecute', () => {
251
+ it('should request confirmation for a new command and whitelist it on "Always"', async () => {
252
+ const params = { command: 'npm install' };
253
+ const confirmation = await shellTool.shouldConfirmExecute(params, new AbortController().signal);
254
+ expect(confirmation).not.toBe(false);
255
+ expect(confirmation && confirmation.type).toBe('exec');
256
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
257
+ await confirmation.onConfirm(ToolConfirmationOutcome.ProceedAlways);
258
+ // Should now be whitelisted
259
+ const secondConfirmation = await shellTool.shouldConfirmExecute({ command: 'npm test' }, new AbortController().signal);
260
+ expect(secondConfirmation).toBe(false);
261
+ });
262
+ it('should skip confirmation if validation fails', async () => {
263
+ const confirmation = await shellTool.shouldConfirmExecute({ command: '' }, new AbortController().signal);
264
+ expect(confirmation).toBe(false);
265
+ });
404
266
  });
405
267
  });
406
268
  //# sourceMappingURL=shell.test.js.map