@aria-cli/tools 1.0.12 → 1.0.14

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 (233) hide show
  1. package/dist/index.js +378 -70
  2. package/dist/network-runtime/index.js +8 -12
  3. package/dist-cjs/index.js +400 -435
  4. package/dist-cjs/network-runtime/index.js +8 -172
  5. package/package.json +8 -6
  6. package/dist/.tsbuildinfo +0 -1
  7. package/dist/ask-user-interaction.js +0 -22
  8. package/dist/cache/web-cache.js +0 -66
  9. package/dist/definitions/arion.js +0 -104
  10. package/dist/definitions/browser/browser.js +0 -418
  11. package/dist/definitions/browser/index.js +0 -4
  12. package/dist/definitions/browser/pw-downloads.js +0 -114
  13. package/dist/definitions/browser/pw-interactions.js +0 -199
  14. package/dist/definitions/browser/pw-responses.js +0 -76
  15. package/dist/definitions/browser/pw-session.js +0 -310
  16. package/dist/definitions/browser/pw-shared.js +0 -66
  17. package/dist/definitions/browser/pw-snapshot.js +0 -301
  18. package/dist/definitions/browser/pw-state.js +0 -62
  19. package/dist/definitions/browser/types.js +0 -4
  20. package/dist/definitions/code-intelligence.js +0 -470
  21. package/dist/definitions/core.js +0 -109
  22. package/dist/definitions/delegation.js +0 -512
  23. package/dist/definitions/deploy.js +0 -65
  24. package/dist/definitions/filesystem.js +0 -196
  25. package/dist/definitions/frg.js +0 -63
  26. package/dist/definitions/index.js +0 -20
  27. package/dist/definitions/memory.js +0 -123
  28. package/dist/definitions/messaging.js +0 -625
  29. package/dist/definitions/meta.js +0 -349
  30. package/dist/definitions/network.js +0 -159
  31. package/dist/definitions/outlook.js +0 -277
  32. package/dist/definitions/patch/apply-patch.js +0 -184
  33. package/dist/definitions/patch/fuzzy-match.js +0 -166
  34. package/dist/definitions/patch/index.js +0 -1
  35. package/dist/definitions/patch/patch-parser.js +0 -207
  36. package/dist/definitions/patch/sandbox-paths.js +0 -105
  37. package/dist/definitions/process/index.js +0 -4
  38. package/dist/definitions/process/process-registry.js +0 -213
  39. package/dist/definitions/process/process.js +0 -386
  40. package/dist/definitions/process/pty-keys.js +0 -254
  41. package/dist/definitions/process/session-slug.js +0 -142
  42. package/dist/definitions/quip.js +0 -195
  43. package/dist/definitions/search.js +0 -60
  44. package/dist/definitions/session-history.js +0 -69
  45. package/dist/definitions/shell.js +0 -181
  46. package/dist/definitions/slack.js +0 -180
  47. package/dist/definitions/web.js +0 -109
  48. package/dist/executors/apply-patch.js +0 -901
  49. package/dist/executors/arion.js +0 -119
  50. package/dist/executors/code-intelligence.js +0 -882
  51. package/dist/executors/deploy.js +0 -848
  52. package/dist/executors/filesystem.js +0 -1122
  53. package/dist/executors/frg-freshness.js +0 -576
  54. package/dist/executors/frg.js +0 -298
  55. package/dist/executors/index.js +0 -46
  56. package/dist/executors/learning-meta.js +0 -1146
  57. package/dist/executors/lsp-client.js +0 -296
  58. package/dist/executors/memory.js +0 -750
  59. package/dist/executors/meta.js +0 -220
  60. package/dist/executors/process-registry.js +0 -465
  61. package/dist/executors/pty-session-store.js +0 -30
  62. package/dist/executors/pty.js +0 -271
  63. package/dist/executors/restart.js +0 -119
  64. package/dist/executors/search-freshness.js +0 -195
  65. package/dist/executors/search-types.js +0 -52
  66. package/dist/executors/search.js +0 -66
  67. package/dist/executors/self-diagnose.js +0 -398
  68. package/dist/executors/session-history.js +0 -283
  69. package/dist/executors/shell-safety.js +0 -473
  70. package/dist/executors/shell.js +0 -954
  71. package/dist/executors/utils.js +0 -33
  72. package/dist/executors/web.js +0 -542
  73. package/dist/extraction/content-extraction.js +0 -235
  74. package/dist/extraction/index.js +0 -4
  75. package/dist/headless-control-contract.js +0 -967
  76. package/dist/local-control-http-auth.js +0 -2
  77. package/dist/mcp/client.js +0 -181
  78. package/dist/mcp/connection.js +0 -480
  79. package/dist/mcp/index.js +0 -10
  80. package/dist/mcp/jsonrpc.js +0 -144
  81. package/dist/mcp/types.js +0 -7
  82. package/dist/network-control-adapter.js +0 -72
  83. package/dist/network-runtime/address-types.js +0 -165
  84. package/dist/network-runtime/db-owner-fencing.js +0 -69
  85. package/dist/network-runtime/delivery-receipts.js +0 -267
  86. package/dist/network-runtime/direct-endpoint-authority.js +0 -25
  87. package/dist/network-runtime/local-control-contract.js +0 -627
  88. package/dist/network-runtime/node-store-contract.js +0 -34
  89. package/dist/network-runtime/pair-route-contract.js +0 -77
  90. package/dist/network-runtime/peer-capabilities.js +0 -28
  91. package/dist/network-runtime/peer-principal-ref.js +0 -12
  92. package/dist/network-runtime/peer-state-machine.js +0 -121
  93. package/dist/network-runtime/protocol-schemas.js +0 -205
  94. package/dist/network-runtime/runtime-bootstrap-contract.js +0 -60
  95. package/dist/outlook/desktop-session.js +0 -279
  96. package/dist/policy.js +0 -149
  97. package/dist/providers/brave.js +0 -62
  98. package/dist/providers/duckduckgo.js +0 -176
  99. package/dist/providers/exa.js +0 -63
  100. package/dist/providers/firecrawl.js +0 -55
  101. package/dist/providers/index.js +0 -7
  102. package/dist/providers/jina.js +0 -49
  103. package/dist/providers/router.js +0 -96
  104. package/dist/providers/search-provider.js +0 -32
  105. package/dist/providers/tavily.js +0 -54
  106. package/dist/quip/desktop-session.js +0 -317
  107. package/dist/registry/index.js +0 -1
  108. package/dist/registry/registry.js +0 -756
  109. package/dist/runtime-socket-local-control-client.js +0 -330
  110. package/dist/security/dns-normalization.js +0 -19
  111. package/dist/security/dns-pinning.js +0 -123
  112. package/dist/security/external-content.js +0 -91
  113. package/dist/security/ssrf.js +0 -181
  114. package/dist/slack/desktop-session.js +0 -324
  115. package/dist/tool-factory.js +0 -47
  116. package/dist/types.js +0 -7
  117. package/dist/utils/retry.js +0 -132
  118. package/dist/utils/safe-parse-json.js +0 -160
  119. package/dist/utils/url.js +0 -19
  120. package/dist-cjs/.tsbuildinfo +0 -1
  121. package/dist-cjs/ask-user-interaction.js +0 -27
  122. package/dist-cjs/cache/web-cache.js +0 -70
  123. package/dist-cjs/definitions/arion.js +0 -107
  124. package/dist-cjs/definitions/browser/browser.js +0 -421
  125. package/dist-cjs/definitions/browser/index.js +0 -8
  126. package/dist-cjs/definitions/browser/pw-downloads.js +0 -117
  127. package/dist-cjs/definitions/browser/pw-interactions.js +0 -213
  128. package/dist-cjs/definitions/browser/pw-responses.js +0 -84
  129. package/dist-cjs/definitions/browser/pw-session.js +0 -326
  130. package/dist-cjs/definitions/browser/pw-shared.js +0 -72
  131. package/dist-cjs/definitions/browser/pw-snapshot.js +0 -307
  132. package/dist-cjs/definitions/browser/pw-state.js +0 -70
  133. package/dist-cjs/definitions/browser/types.js +0 -5
  134. package/dist-cjs/definitions/code-intelligence.js +0 -473
  135. package/dist-cjs/definitions/core.js +0 -133
  136. package/dist-cjs/definitions/delegation.js +0 -515
  137. package/dist-cjs/definitions/deploy.js +0 -68
  138. package/dist-cjs/definitions/filesystem.js +0 -199
  139. package/dist-cjs/definitions/frg.js +0 -66
  140. package/dist-cjs/definitions/index.js +0 -43
  141. package/dist-cjs/definitions/memory.js +0 -126
  142. package/dist-cjs/definitions/messaging.js +0 -631
  143. package/dist-cjs/definitions/meta.js +0 -352
  144. package/dist-cjs/definitions/network.js +0 -162
  145. package/dist-cjs/definitions/outlook.js +0 -280
  146. package/dist-cjs/definitions/patch/apply-patch.js +0 -191
  147. package/dist-cjs/definitions/patch/fuzzy-match.js +0 -172
  148. package/dist-cjs/definitions/patch/index.js +0 -5
  149. package/dist-cjs/definitions/patch/patch-parser.js +0 -215
  150. package/dist-cjs/definitions/patch/sandbox-paths.js +0 -113
  151. package/dist-cjs/definitions/process/index.js +0 -8
  152. package/dist-cjs/definitions/process/process-registry.js +0 -231
  153. package/dist-cjs/definitions/process/process.js +0 -389
  154. package/dist-cjs/definitions/process/pty-keys.js +0 -259
  155. package/dist-cjs/definitions/process/session-slug.js +0 -145
  156. package/dist-cjs/definitions/quip.js +0 -198
  157. package/dist-cjs/definitions/search.js +0 -63
  158. package/dist-cjs/definitions/session-history.js +0 -72
  159. package/dist-cjs/definitions/shell.js +0 -184
  160. package/dist-cjs/definitions/slack.js +0 -183
  161. package/dist-cjs/definitions/web.js +0 -112
  162. package/dist-cjs/executors/apply-patch.js +0 -938
  163. package/dist-cjs/executors/arion.js +0 -125
  164. package/dist-cjs/executors/code-intelligence.js +0 -925
  165. package/dist-cjs/executors/deploy.js +0 -869
  166. package/dist-cjs/executors/filesystem.js +0 -1167
  167. package/dist-cjs/executors/frg-freshness.js +0 -627
  168. package/dist-cjs/executors/frg.js +0 -334
  169. package/dist-cjs/executors/index.js +0 -143
  170. package/dist-cjs/executors/learning-meta.js +0 -1165
  171. package/dist-cjs/executors/lsp-client.js +0 -310
  172. package/dist-cjs/executors/memory.js +0 -796
  173. package/dist-cjs/executors/meta.js +0 -226
  174. package/dist-cjs/executors/process-registry.js +0 -469
  175. package/dist-cjs/executors/pty-session-store.js +0 -34
  176. package/dist-cjs/executors/pty.js +0 -312
  177. package/dist-cjs/executors/restart.js +0 -155
  178. package/dist-cjs/executors/search-freshness.js +0 -234
  179. package/dist-cjs/executors/search-types.js +0 -56
  180. package/dist-cjs/executors/search.js +0 -102
  181. package/dist-cjs/executors/self-diagnose.js +0 -434
  182. package/dist-cjs/executors/session-history.js +0 -320
  183. package/dist-cjs/executors/shell-safety.js +0 -478
  184. package/dist-cjs/executors/shell.js +0 -1001
  185. package/dist-cjs/executors/utils.js +0 -73
  186. package/dist-cjs/executors/web.js +0 -547
  187. package/dist-cjs/extraction/content-extraction.js +0 -243
  188. package/dist-cjs/extraction/index.js +0 -8
  189. package/dist-cjs/headless-control-contract.js +0 -972
  190. package/dist-cjs/local-control-http-auth.js +0 -5
  191. package/dist-cjs/mcp/client.js +0 -185
  192. package/dist-cjs/mcp/connection.js +0 -484
  193. package/dist-cjs/mcp/index.js +0 -30
  194. package/dist-cjs/mcp/jsonrpc.js +0 -148
  195. package/dist-cjs/mcp/types.js +0 -8
  196. package/dist-cjs/network-control-adapter.js +0 -77
  197. package/dist-cjs/network-runtime/address-types.js +0 -168
  198. package/dist-cjs/network-runtime/db-owner-fencing.js +0 -76
  199. package/dist-cjs/network-runtime/delivery-receipts.js +0 -276
  200. package/dist-cjs/network-runtime/direct-endpoint-authority.js +0 -29
  201. package/dist-cjs/network-runtime/local-control-contract.js +0 -633
  202. package/dist-cjs/network-runtime/node-store-contract.js +0 -38
  203. package/dist-cjs/network-runtime/pair-route-contract.js +0 -80
  204. package/dist-cjs/network-runtime/peer-capabilities.js +0 -37
  205. package/dist-cjs/network-runtime/peer-principal-ref.js +0 -15
  206. package/dist-cjs/network-runtime/peer-state-machine.js +0 -129
  207. package/dist-cjs/network-runtime/protocol-schemas.js +0 -212
  208. package/dist-cjs/network-runtime/runtime-bootstrap-contract.js +0 -63
  209. package/dist-cjs/outlook/desktop-session.js +0 -318
  210. package/dist-cjs/policy.js +0 -155
  211. package/dist-cjs/providers/brave.js +0 -66
  212. package/dist-cjs/providers/duckduckgo.js +0 -180
  213. package/dist-cjs/providers/exa.js +0 -67
  214. package/dist-cjs/providers/firecrawl.js +0 -59
  215. package/dist-cjs/providers/index.js +0 -17
  216. package/dist-cjs/providers/jina.js +0 -53
  217. package/dist-cjs/providers/router.js +0 -100
  218. package/dist-cjs/providers/search-provider.js +0 -36
  219. package/dist-cjs/providers/tavily.js +0 -58
  220. package/dist-cjs/quip/desktop-session.js +0 -353
  221. package/dist-cjs/registry/index.js +0 -6
  222. package/dist-cjs/registry/registry.js +0 -761
  223. package/dist-cjs/runtime-socket-local-control-client.js +0 -367
  224. package/dist-cjs/security/dns-normalization.js +0 -22
  225. package/dist-cjs/security/dns-pinning.js +0 -160
  226. package/dist-cjs/security/external-content.js +0 -95
  227. package/dist-cjs/security/ssrf.js +0 -221
  228. package/dist-cjs/slack/desktop-session.js +0 -366
  229. package/dist-cjs/tool-factory.js +0 -50
  230. package/dist-cjs/types.js +0 -8
  231. package/dist-cjs/utils/retry.js +0 -169
  232. package/dist-cjs/utils/safe-parse-json.js +0 -164
  233. package/dist-cjs/utils/url.js +0 -23
@@ -1,1122 +0,0 @@
1
- /**
2
- * @aria/tools - Filesystem tool executors
3
- *
4
- * Implementation of filesystem operations for ARIA tool system.
5
- */
6
- import * as fs from "node:fs/promises";
7
- import * as fsSync from "node:fs";
8
- import * as nodePath from "node:path";
9
- import * as crypto from "node:crypto";
10
- import * as readline from "node:readline";
11
- import { structuredPatch as computePatch } from "diff";
12
- import fastGlob from "fast-glob";
13
- import { success, fail, getErrorMessage, isPathWithinBase } from "./utils.js";
14
- import { recordFrgMutation } from "./frg-freshness.js";
15
- import { recordSearchMutation } from "./search-freshness.js";
16
- /** Media file extensions for binary reading */
17
- const MEDIA_EXTENSIONS = new Set([
18
- ".png",
19
- ".jpg",
20
- ".jpeg",
21
- ".gif",
22
- ".webp",
23
- ".svg",
24
- ".bmp",
25
- ".ico",
26
- ".mp3",
27
- ".wav",
28
- ".ogg",
29
- ".flac",
30
- ".aac",
31
- ".mp4",
32
- ".webm",
33
- ".mov",
34
- ".pdf",
35
- ]);
36
- /** MIME type lookup */
37
- const MIME_TYPES = {
38
- ".png": "image/png",
39
- ".jpg": "image/jpeg",
40
- ".jpeg": "image/jpeg",
41
- ".gif": "image/gif",
42
- ".webp": "image/webp",
43
- ".svg": "image/svg+xml",
44
- ".bmp": "image/bmp",
45
- ".ico": "image/x-icon",
46
- ".mp3": "audio/mpeg",
47
- ".wav": "audio/wav",
48
- ".ogg": "audio/ogg",
49
- ".flac": "audio/flac",
50
- ".aac": "audio/aac",
51
- ".mp4": "video/mp4",
52
- ".webm": "video/webm",
53
- ".mov": "video/quicktime",
54
- ".pdf": "application/pdf",
55
- };
56
- const MAX_MEDIA_SIZE_BYTES = 10 * 1024 * 1024; // 10MB
57
- const DEFAULT_READ_LIMIT = 2000;
58
- const MAX_READ_BYTES = 200 * 1024; // 200KB
59
- /**
60
- * Hard output caps to avoid overwhelming tool responses.
61
- * read_file already has media-size capping and line slicing support.
62
- */
63
- export const FILESYSTEM_OUTPUT_LIMITS = {
64
- MAX_GLOB_RESULTS: 2000,
65
- MAX_GREP_MATCHES: 2000,
66
- MAX_GREP_ERRORS: 200,
67
- MAX_GREP_FILE_BYTES: 2 * 1024 * 1024,
68
- MAX_GREP_LINE_BYTES: 16 * 1024,
69
- };
70
- /**
71
- * Normalize common aliases before passing encodings to Node fs APIs.
72
- */
73
- const SUPPORTED_ENCODINGS = new Set([
74
- "utf8",
75
- "utf16le",
76
- "latin1",
77
- "ascii",
78
- "base64",
79
- "base64url",
80
- "hex",
81
- "ucs2",
82
- "binary",
83
- ]);
84
- function normalizeEncoding(inputEncoding) {
85
- if (!inputEncoding)
86
- return "utf8";
87
- const normalized = inputEncoding.toLowerCase();
88
- const canonical = normalized === "utf-8" ? "utf8" : normalized;
89
- if (!SUPPORTED_ENCODINGS.has(canonical)) {
90
- throw new Error(`Unsupported encoding: ${inputEncoding}`);
91
- }
92
- return canonical;
93
- }
94
- /**
95
- * Sensitive dot-directories under $HOME that must never be accessed by tools.
96
- * These contain credentials, keys, and tokens that have no legitimate use in
97
- * ARIA's filesystem operations.
98
- */
99
- const BLOCKED_HOME_DIRS = new Set([
100
- ".ssh",
101
- ".gnupg",
102
- ".gpg",
103
- ".aws",
104
- ".azure",
105
- ".config/gcloud",
106
- ".docker",
107
- ".kube",
108
- ".npmrc",
109
- ".pypirc",
110
- ".netrc",
111
- ".git-credentials",
112
- ".password-store",
113
- ]);
114
- /**
115
- * Returns true if `resolved` falls inside a blocked sensitive directory under home.
116
- * Paths are compared after normalisation so symlink tricks don't bypass.
117
- */
118
- function isBlockedHomePath(resolved, realHome) {
119
- // Strip home prefix to get the relative tail, e.g. ".ssh/id_rsa"
120
- const homePrefix = realHome.endsWith(nodePath.sep) ? realHome : realHome + nodePath.sep;
121
- if (!resolved.startsWith(homePrefix))
122
- return false;
123
- const tail = resolved.slice(homePrefix.length);
124
- for (const blocked of BLOCKED_HOME_DIRS) {
125
- if (tail === blocked || tail.startsWith(blocked + nodePath.sep)) {
126
- return true;
127
- }
128
- }
129
- return false;
130
- }
131
- /**
132
- * Resolves a path relative to the working directory if not absolute.
133
- * Resolves symlinks to prevent bypassing path restrictions.
134
- * Throws if the resolved path is outside the working directory (path traversal protection).
135
- */
136
- function resolvePath(inputPath, ctx) {
137
- let resolved = nodePath.isAbsolute(inputPath)
138
- ? nodePath.resolve(inputPath)
139
- : nodePath.resolve(ctx.workingDir, inputPath);
140
- // Resolve symlinks on both the path and the base to prevent bypass
141
- // and to handle platforms where /tmp → /private/tmp (macOS)
142
- let realBase = ctx.workingDir;
143
- try {
144
- realBase = fsSync.realpathSync(ctx.workingDir);
145
- }
146
- catch {
147
- // Base dir should exist, but fall back to original if not
148
- }
149
- try {
150
- resolved = fsSync.realpathSync(resolved);
151
- }
152
- catch {
153
- // Path may not exist yet (write to new file / mkdir).
154
- // Walk up to find the nearest existing ancestor and resolve from there,
155
- // to handle symlinked temp dirs (e.g., /tmp → /private/tmp on macOS).
156
- let current = resolved;
157
- let suffix = "";
158
- while (current !== nodePath.dirname(current)) {
159
- const parent = nodePath.dirname(current);
160
- suffix = suffix
161
- ? nodePath.join(nodePath.basename(current), suffix)
162
- : nodePath.basename(current);
163
- try {
164
- const realAncestor = fsSync.realpathSync(parent);
165
- resolved = nodePath.join(realAncestor, suffix);
166
- break;
167
- }
168
- catch {
169
- current = parent;
170
- }
171
- }
172
- }
173
- // 1. Check working directory containment (standard sandbox)
174
- if (isPathWithinBase(resolved, realBase)) {
175
- return resolved;
176
- }
177
- // 2. Check home directory containment (allows access to ~/.aria/, ~/projects, etc.)
178
- // Sensitive dot-directories are blocklisted unless autonomy is "full".
179
- const home = process.env.HOME;
180
- if (home) {
181
- try {
182
- const realHome = fsSync.realpathSync(home);
183
- if (isPathWithinBase(resolved, realHome)) {
184
- if (ctx.autonomy !== "full" && isBlockedHomePath(resolved, realHome)) {
185
- throw new Error(`Path not allowed: access to sensitive home directory is blocked (set autonomy to "full" to override)`);
186
- }
187
- return resolved;
188
- }
189
- }
190
- catch (err) {
191
- if (err instanceof Error && err.message.includes("sensitive home directory")) {
192
- throw err;
193
- }
194
- // Home not accessible/exists — fall through to final guard
195
- }
196
- }
197
- // Always enforce containment. This closes bypasses where
198
- // absolute paths could escape the workspace if outer sandboxing is misconfigured.
199
- throw new Error(`Path traversal not allowed: resolved path is outside working directory and home directory`);
200
- }
201
- function normalizeLineEndings(text) {
202
- return text.replace(/\r\n/g, "\n");
203
- }
204
- function hashSha256(text) {
205
- return crypto.createHash("sha256").update(text, "utf8").digest("hex");
206
- }
207
- function splitLinesWithoutTrailingEmpty(content) {
208
- const lines = content.split("\n");
209
- if (content.endsWith("\n") && lines.length > 0 && lines[lines.length - 1] === "") {
210
- lines.pop();
211
- }
212
- return lines;
213
- }
214
- function getReadWindow(content, offset, limit) {
215
- const lines = splitLinesWithoutTrailingEmpty(content);
216
- const startLine = Math.max(0, offset);
217
- const maxLines = Math.max(1, limit);
218
- const sliced = lines.slice(startLine, startLine + maxLines);
219
- const visible = [];
220
- let bytes = 0;
221
- let truncatedByBytes = false;
222
- for (const line of sliced) {
223
- const lineBytes = Buffer.byteLength(line, "utf8");
224
- const separatorBytes = visible.length > 0 ? 1 : 0; // "\n"
225
- if (bytes + lineBytes + separatorBytes > MAX_READ_BYTES) {
226
- truncatedByBytes = true;
227
- break;
228
- }
229
- visible.push(line);
230
- bytes += lineBytes + separatorBytes;
231
- }
232
- const endExclusive = startLine + visible.length;
233
- const truncatedByLines = endExclusive < lines.length;
234
- const truncated = truncatedByBytes || truncatedByLines;
235
- const nextOffset = truncated ? endExclusive : undefined;
236
- return {
237
- content: visible.join("\n"),
238
- startLine,
239
- endLine: endExclusive,
240
- totalLines: lines.length,
241
- truncated,
242
- nextOffset,
243
- bytes,
244
- };
245
- }
246
- async function getReadWindowFromFile(filePath, encoding, offset, limit) {
247
- const startLine = Math.max(0, offset);
248
- const maxLines = Math.max(1, limit);
249
- const visible = [];
250
- let bytes = 0;
251
- let totalLines = 0;
252
- let truncatedByBytes = false;
253
- let hasExtraLines = false;
254
- const input = fsSync.createReadStream(filePath, { encoding });
255
- const lines = readline.createInterface({ input, crlfDelay: Infinity });
256
- try {
257
- for await (const line of lines) {
258
- totalLines++;
259
- if (totalLines <= startLine) {
260
- continue;
261
- }
262
- if (visible.length >= maxLines) {
263
- hasExtraLines = true;
264
- continue;
265
- }
266
- const lineBytes = Buffer.byteLength(line, encoding);
267
- const separatorBytes = visible.length > 0 ? 1 : 0;
268
- if (bytes + lineBytes + separatorBytes > MAX_READ_BYTES) {
269
- truncatedByBytes = true;
270
- hasExtraLines = true;
271
- continue;
272
- }
273
- visible.push(line);
274
- bytes += lineBytes + separatorBytes;
275
- }
276
- }
277
- finally {
278
- lines.close();
279
- input.destroy();
280
- }
281
- const endExclusive = startLine + visible.length;
282
- const truncatedByLines = hasExtraLines;
283
- const truncated = truncatedByBytes || truncatedByLines;
284
- const nextOffset = truncated ? endExclusive : undefined;
285
- return {
286
- content: visible.join("\n"),
287
- startLine,
288
- endLine: endExclusive,
289
- totalLines,
290
- truncated,
291
- nextOffset,
292
- bytes,
293
- };
294
- }
295
- function dedupeRanges(ranges) {
296
- const seen = new Set();
297
- const result = [];
298
- for (const range of ranges) {
299
- const key = `${range.start}:${range.end}`;
300
- if (seen.has(key))
301
- continue;
302
- seen.add(key);
303
- result.push(range);
304
- }
305
- return result.sort((a, b) => a.start - b.start || a.end - b.end);
306
- }
307
- function countLeadingWhitespace(line) {
308
- const match = line.match(/^[\t ]*/);
309
- return match ? match[0].length : 0;
310
- }
311
- function stripCommonIndent(lines) {
312
- let min = Number.POSITIVE_INFINITY;
313
- for (const line of lines) {
314
- if (line.trim().length === 0)
315
- continue;
316
- min = Math.min(min, countLeadingWhitespace(line));
317
- }
318
- if (!Number.isFinite(min) || min <= 0)
319
- return [...lines];
320
- return lines.map((line) => (line.trim().length === 0 ? line : line.slice(min)));
321
- }
322
- function lineStartOffsets(content) {
323
- const starts = [0];
324
- for (let i = 0; i < content.length; i++) {
325
- if (content[i] === "\n") {
326
- starts.push(i + 1);
327
- }
328
- }
329
- return starts;
330
- }
331
- function normalizeSearchLines(raw) {
332
- const lines = [...raw];
333
- if (lines.length > 0 && lines[lines.length - 1] === "") {
334
- lines.pop();
335
- }
336
- return lines;
337
- }
338
- function getLineWindowRanges(content, oldText, predicate) {
339
- if (!oldText.includes("\n"))
340
- return [];
341
- const contentLines = content.split("\n");
342
- const searchLines = normalizeSearchLines(oldText.split("\n"));
343
- if (searchLines.length === 0 || searchLines.length > contentLines.length) {
344
- return [];
345
- }
346
- const starts = lineStartOffsets(content);
347
- const ranges = [];
348
- for (let i = 0; i <= contentLines.length - searchLines.length; i++) {
349
- const window = contentLines.slice(i, i + searchLines.length);
350
- if (!predicate(window, searchLines))
351
- continue;
352
- const start = starts[i] ?? 0;
353
- const lastLineIdx = i + searchLines.length - 1;
354
- const lineStart = starts[lastLineIdx] ?? 0;
355
- const line = contentLines[lastLineIdx] ?? "";
356
- const end = lineStart + line.length;
357
- ranges.push({ start, end });
358
- }
359
- return dedupeRanges(ranges);
360
- }
361
- function findExactRanges(content, oldText) {
362
- if (oldText.length === 0)
363
- return [];
364
- const ranges = [];
365
- let cursor = 0;
366
- while (cursor <= content.length) {
367
- const idx = content.indexOf(oldText, cursor);
368
- if (idx === -1)
369
- break;
370
- ranges.push({ start: idx, end: idx + oldText.length });
371
- cursor = idx + Math.max(oldText.length, 1);
372
- }
373
- return dedupeRanges(ranges);
374
- }
375
- function findTrimmedLineBlockRanges(content, oldText) {
376
- const trimmedNeedle = oldText.trim();
377
- if (trimmedNeedle.length === 0)
378
- return [];
379
- if (!oldText.includes("\n")) {
380
- const lines = content.split("\n");
381
- const starts = lineStartOffsets(content);
382
- const ranges = [];
383
- for (let i = 0; i < lines.length; i++) {
384
- if ((lines[i] ?? "").trim() !== trimmedNeedle)
385
- continue;
386
- const start = starts[i] ?? 0;
387
- const line = lines[i] ?? "";
388
- ranges.push({ start, end: start + line.length });
389
- }
390
- return dedupeRanges(ranges);
391
- }
392
- return getLineWindowRanges(content, oldText, (window, search) => window.every((line, idx) => line.trim() === (search[idx] ?? "").trim()));
393
- }
394
- function findIndentFlexibleRanges(content, oldText) {
395
- return getLineWindowRanges(content, oldText, (window, search) => {
396
- const normalizedWindow = stripCommonIndent(window);
397
- const normalizedSearch = stripCommonIndent(search);
398
- if (normalizedWindow.length !== normalizedSearch.length)
399
- return false;
400
- for (let i = 0; i < normalizedWindow.length; i++) {
401
- if ((normalizedWindow[i] ?? "") !== (normalizedSearch[i] ?? ""))
402
- return false;
403
- }
404
- return true;
405
- });
406
- }
407
- function normalizeWhitespace(line) {
408
- return line.trim().replace(/\s+/g, " ");
409
- }
410
- function findWhitespaceNormalizedRanges(content, oldText) {
411
- if (!oldText.includes("\n")) {
412
- const needle = normalizeWhitespace(oldText);
413
- if (!needle)
414
- return [];
415
- const lines = content.split("\n");
416
- const starts = lineStartOffsets(content);
417
- const ranges = [];
418
- for (let i = 0; i < lines.length; i++) {
419
- const line = lines[i] ?? "";
420
- if (normalizeWhitespace(line) !== needle)
421
- continue;
422
- const start = starts[i] ?? 0;
423
- ranges.push({ start, end: start + line.length });
424
- }
425
- return dedupeRanges(ranges);
426
- }
427
- return getLineWindowRanges(content, oldText, (window, search) => window.every((line, idx) => normalizeWhitespace(line) === normalizeWhitespace(search[idx] ?? "")));
428
- }
429
- function applyRanges(content, ranges, replacement) {
430
- const sorted = [...ranges].sort((a, b) => b.start - a.start || b.end - a.end);
431
- let next = content;
432
- for (const range of sorted) {
433
- next = next.slice(0, range.start) + replacement + next.slice(range.end);
434
- }
435
- return next;
436
- }
437
- /**
438
- * Reads a file's contents.
439
- */
440
- export async function executeReadFile(input, ctx) {
441
- if (ctx.abortSignal?.aborted)
442
- return fail("Operation cancelled");
443
- try {
444
- if (input.offset !== undefined && (!Number.isInteger(input.offset) || input.offset < 0)) {
445
- return fail("offset must be a non-negative integer");
446
- }
447
- if (input.limit !== undefined && (!Number.isInteger(input.limit) || input.limit <= 0)) {
448
- return fail("limit must be a positive integer");
449
- }
450
- const filePath = resolvePath(input.path, ctx);
451
- const stat = await fs.stat(filePath);
452
- const encoding = normalizeEncoding(input.encoding);
453
- if (stat.isDirectory()) {
454
- return fail(`Path is a directory: ${filePath}`);
455
- }
456
- // Detect media files by extension — return base64 object unless caller explicitly requested
457
- // a non-UTF8 text/binary encoding.
458
- const ext = nodePath.extname(filePath).toLowerCase();
459
- const isMediaFile = MEDIA_EXTENSIONS.has(ext);
460
- if (isMediaFile && stat.size > MAX_MEDIA_SIZE_BYTES) {
461
- return fail(`File too large for media reading: ${(stat.size / 1024 / 1024).toFixed(1)}MB (max 10MB)`);
462
- }
463
- const useMediaEncoding = !input.encoding || encoding === "utf8";
464
- if (isMediaFile && useMediaEncoding) {
465
- const buffer = await fs.readFile(filePath);
466
- const base64 = buffer.toString("base64");
467
- const mimeType = MIME_TYPES[ext] ?? "application/octet-stream";
468
- return success(`Read media file: ${nodePath.basename(filePath)} (${mimeType}, ${(stat.size / 1024).toFixed(1)}KB)`, {
469
- type: "media",
470
- mimeType,
471
- base64,
472
- size: stat.size,
473
- path: filePath,
474
- });
475
- }
476
- const explicitWindow = input.offset !== undefined || input.limit !== undefined;
477
- let window;
478
- let returnedContent;
479
- if (!explicitWindow && stat.size <= MAX_READ_BYTES) {
480
- const content = normalizeLineEndings(await fs.readFile(filePath, { encoding }));
481
- window = getReadWindow(content, 0, DEFAULT_READ_LIMIT);
482
- returnedContent = window.truncated ? window.content : content;
483
- }
484
- else {
485
- window = await getReadWindowFromFile(filePath, encoding, input.offset ?? 0, input.limit ?? DEFAULT_READ_LIMIT);
486
- returnedContent = window.content;
487
- }
488
- const returnedBytes = Buffer.byteLength(returnedContent, encoding);
489
- const lineSummary = `lines ${window.startLine + 1}-${window.endLine} of ${window.totalLines}`;
490
- const truncationSummary = window.truncated
491
- ? ` (truncated; use offset=${window.nextOffset ?? window.endLine} to continue)`
492
- : "";
493
- return success(`Read ${returnedContent.length} characters (${lineSummary}, ${returnedBytes} bytes) from ${filePath}${truncationSummary}`, returnedContent);
494
- }
495
- catch (err) {
496
- return fail(getErrorMessage(err));
497
- }
498
- }
499
- /**
500
- * Writes content to a file.
501
- */
502
- export async function executeWriteFile(input, ctx) {
503
- if (ctx.abortSignal?.aborted)
504
- return fail("Operation cancelled");
505
- try {
506
- const filePath = resolvePath(input.path, ctx);
507
- const encoding = normalizeEncoding(input.encoding);
508
- const expectedHash = input.expectedHash?.trim().toLowerCase();
509
- if (expectedHash && !/^[a-f0-9]{64}$/.test(expectedHash)) {
510
- return fail("expectedHash must be a 64-character lowercase/uppercase SHA-256 hex digest");
511
- }
512
- let existed = false;
513
- let previousContent = "";
514
- try {
515
- previousContent = await fs.readFile(filePath, { encoding });
516
- existed = true;
517
- }
518
- catch (err) {
519
- const code = err?.code;
520
- if (code !== "ENOENT") {
521
- throw err;
522
- }
523
- }
524
- const previousHash = existed ? hashSha256(normalizeLineEndings(previousContent)) : undefined;
525
- if (expectedHash) {
526
- if (!existed) {
527
- return fail("expectedHash was provided but the file does not exist");
528
- }
529
- if (previousHash !== expectedHash) {
530
- return fail(`Hash mismatch for ${filePath}: file changed since last read`, {
531
- expectedHash,
532
- actualHash: previousHash,
533
- path: filePath,
534
- });
535
- }
536
- }
537
- // Ensure parent directory exists
538
- const parentDir = nodePath.dirname(filePath);
539
- await fs.mkdir(parentDir, { recursive: true });
540
- if (input.append) {
541
- await fs.appendFile(filePath, input.content, { encoding });
542
- }
543
- else {
544
- await fs.writeFile(filePath, input.content, { encoding });
545
- }
546
- const currentContent = input.append ? previousContent + input.content : input.content;
547
- const currentHash = hashSha256(normalizeLineEndings(currentContent));
548
- const bytesWritten = Buffer.byteLength(input.content, encoding);
549
- const action = input.append ? "appended" : existed ? "overwritten" : "created";
550
- // Compute structured diff for overwrites and appends (not new files)
551
- let structuredPatchData;
552
- if (existed) {
553
- const patch = computePatch(filePath, filePath, previousContent, currentContent, "", "", {
554
- context: 3,
555
- });
556
- structuredPatchData = patch.hunks;
557
- }
558
- recordFrgMutation(filePath, "write", currentContent);
559
- recordSearchMutation(filePath, "write", currentContent);
560
- return success(`Successfully ${action} ${filePath} (${bytesWritten} bytes written)`, {
561
- filePath,
562
- action,
563
- existed,
564
- bytesWritten,
565
- previousHash,
566
- currentHash,
567
- ...(structuredPatchData ? { structuredPatch: structuredPatchData } : {}),
568
- });
569
- }
570
- catch (err) {
571
- return fail(getErrorMessage(err));
572
- }
573
- }
574
- /**
575
- * Edits a file by replacing text.
576
- */
577
- export async function executeEditFile(input, ctx) {
578
- if (ctx.abortSignal?.aborted)
579
- return fail("Operation cancelled");
580
- try {
581
- // Validate oldText is not empty to prevent matching everywhere
582
- if (input.oldText === "") {
583
- return fail("oldText cannot be empty");
584
- }
585
- if (input.expectedReplacements !== undefined &&
586
- (!Number.isInteger(input.expectedReplacements) || input.expectedReplacements <= 0)) {
587
- return fail("expectedReplacements must be a positive integer");
588
- }
589
- const expectedHash = input.expectedHash?.trim().toLowerCase();
590
- if (expectedHash && !/^[a-f0-9]{64}$/.test(expectedHash)) {
591
- return fail("expectedHash must be a 64-character lowercase/uppercase SHA-256 hex digest");
592
- }
593
- if (!input.replaceAll && (input.expectedReplacements ?? 1) > 1) {
594
- return fail("expectedReplacements > 1 requires replaceAll=true");
595
- }
596
- const filePath = resolvePath(input.path, ctx);
597
- const originalContent = await fs.readFile(filePath, "utf-8");
598
- const originalNormalized = normalizeLineEndings(originalContent);
599
- const currentHash = hashSha256(originalNormalized);
600
- if (expectedHash && currentHash !== expectedHash) {
601
- return fail(`Hash mismatch for ${filePath}: file changed since last read`, {
602
- path: filePath,
603
- expectedHash,
604
- actualHash: currentHash,
605
- });
606
- }
607
- const hadCRLF = originalContent.includes("\r\n");
608
- const content = originalNormalized;
609
- const oldText = normalizeLineEndings(input.oldText);
610
- const newText = normalizeLineEndings(input.newText);
611
- const strategies = [
612
- { name: "exact", ranges: findExactRanges(content, oldText) },
613
- { name: "trimmed_line_block", ranges: findTrimmedLineBlockRanges(content, oldText) },
614
- { name: "indentation_flexible", ranges: findIndentFlexibleRanges(content, oldText) },
615
- { name: "whitespace_normalized", ranges: findWhitespaceNormalizedRanges(content, oldText) },
616
- ];
617
- const selected = strategies.find((entry) => entry.ranges.length > 0);
618
- if (!selected) {
619
- return fail(`Text not found in file after trying all strategies: "${input.oldText}"`);
620
- }
621
- const ranges = selected.ranges;
622
- const selectedRanges = input.replaceAll ? ranges : [ranges[0]];
623
- const replacements = selectedRanges.length;
624
- const expectedReplacements = input.expectedReplacements;
625
- if (expectedReplacements !== undefined && replacements !== expectedReplacements) {
626
- return fail(`Replacement count mismatch: expected ${expectedReplacements}, would apply ${replacements}`, {
627
- path: filePath,
628
- strategy: selected.name,
629
- matchesFound: ranges.length,
630
- replacements,
631
- expectedReplacements,
632
- });
633
- }
634
- let updated = applyRanges(content, selectedRanges, newText);
635
- // Compute structured diff before CRLF restoration (both sides are LF-normalized)
636
- const patch = computePatch(filePath, filePath, content, updated, "", "", { context: 3 });
637
- if (hadCRLF) {
638
- updated = updated.replace(/\n/g, "\r\n");
639
- }
640
- await fs.writeFile(filePath, updated, "utf-8");
641
- recordFrgMutation(filePath, "write", updated);
642
- recordSearchMutation(filePath, "write", updated);
643
- return success(`Edited ${filePath} using strategy=${selected.name} (${replacements} replacement(s))`, {
644
- filePath,
645
- strategy: selected.name,
646
- matchesFound: ranges.length,
647
- replacements,
648
- expectedReplacements,
649
- replaceAll: input.replaceAll === true,
650
- previousHash: currentHash,
651
- currentHash: hashSha256(normalizeLineEndings(updated)),
652
- structuredPatch: patch.hunks,
653
- });
654
- }
655
- catch (err) {
656
- return fail(getErrorMessage(err));
657
- }
658
- }
659
- function getStatType(stat) {
660
- if (stat.isFile())
661
- return "file";
662
- if (stat.isDirectory())
663
- return "directory";
664
- if (stat.isSymbolicLink())
665
- return "symlink";
666
- return "other";
667
- }
668
- function getErrorCode(err) {
669
- if (typeof err !== "object" || err === null)
670
- return undefined;
671
- const withCode = err;
672
- return typeof withCode.code === "string" ? withCode.code : undefined;
673
- }
674
- export const LS_INTERNALS = {
675
- async readType(entryPath) {
676
- const stat = await fs.lstat(entryPath);
677
- return getStatType(stat);
678
- },
679
- async readMetadata(entryPath) {
680
- const stat = await fs.lstat(entryPath);
681
- return {
682
- size: stat.size,
683
- mode: (stat.mode & 0o777).toString(8).padStart(3, "0"),
684
- mtimeMs: Math.trunc(stat.mtimeMs),
685
- modifiedAt: stat.mtime.toISOString(),
686
- };
687
- },
688
- };
689
- function normalizeNonNegativeInteger(value, fallback) {
690
- if (value === undefined)
691
- return fallback;
692
- if (!Number.isFinite(value))
693
- return fallback;
694
- if (value <= 0)
695
- return 0;
696
- return Math.floor(value);
697
- }
698
- function getEntryDepth(relativePath) {
699
- return relativePath.split("/").filter(Boolean).length;
700
- }
701
- function insertRetainedPath(retained, candidate, maxEntries) {
702
- if (maxEntries <= 0)
703
- return;
704
- if (retained.length < maxEntries) {
705
- retained.push(candidate);
706
- return;
707
- }
708
- let maxIndex = 0;
709
- for (let i = 1; i < retained.length; i++) {
710
- if (retained[i].localeCompare(retained[maxIndex]) > 0) {
711
- maxIndex = i;
712
- }
713
- }
714
- if (candidate.localeCompare(retained[maxIndex]) < 0) {
715
- retained[maxIndex] = candidate;
716
- }
717
- }
718
- /**
719
- * List files and directories with structured metadata.
720
- */
721
- export async function executeLs(input, ctx) {
722
- if (ctx.abortSignal?.aborted)
723
- return fail("Operation cancelled");
724
- try {
725
- const dirPath = resolvePath(input.path ?? ".", ctx);
726
- const dirStat = await fs.stat(dirPath);
727
- if (!dirStat.isDirectory()) {
728
- return fail(`Path is not a directory: ${dirPath}`);
729
- }
730
- const includeAll = input.all === true;
731
- const includeLong = input.long === true;
732
- const depth = normalizeNonNegativeInteger(input.depth, 1);
733
- const offset = normalizeNonNegativeInteger(input.offset, 0);
734
- const ignore = (input.ignore ?? []).filter((pattern) => pattern.trim() !== "");
735
- const requestedLimit = input.limit !== undefined ? normalizeNonNegativeInteger(input.limit, 0) : null;
736
- const retainedRelativePaths = [];
737
- let total = 0;
738
- for (const relativePath of await fastGlob.glob("**/*", {
739
- cwd: dirPath,
740
- onlyFiles: false,
741
- absolute: false,
742
- dot: includeAll,
743
- deep: depth,
744
- ignore: ignore.length > 0 ? ignore : undefined,
745
- })) {
746
- total++;
747
- if (requestedLimit === null) {
748
- retainedRelativePaths.push(relativePath);
749
- continue;
750
- }
751
- insertRetainedPath(retainedRelativePaths, relativePath, offset + requestedLimit);
752
- }
753
- retainedRelativePaths.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
754
- const limit = requestedLimit === null ? Math.max(total - offset, 0) : requestedLimit;
755
- const pageRelativePaths = retainedRelativePaths.slice(offset, offset + limit);
756
- const truncated = offset + limit < total;
757
- const pageEntries = [];
758
- for (const relativePath of pageRelativePaths) {
759
- const entryPath = nodePath.join(dirPath, relativePath);
760
- const name = nodePath.basename(relativePath);
761
- const entryDepth = getEntryDepth(relativePath);
762
- try {
763
- const type = await LS_INTERNALS.readType(entryPath);
764
- pageEntries.push({
765
- name,
766
- path: relativePath,
767
- depth: entryDepth,
768
- type,
769
- });
770
- }
771
- catch (err) {
772
- const code = getErrorCode(err);
773
- // Entry may disappear between glob traversal and metadata reads.
774
- if (code === "ENOENT" || code === "ENOTDIR") {
775
- continue;
776
- }
777
- // Keep listing useful even if one entry cannot be typed.
778
- pageEntries.push({
779
- name,
780
- path: relativePath,
781
- depth: entryDepth,
782
- type: "other",
783
- });
784
- }
785
- }
786
- let entries;
787
- if (includeLong) {
788
- // Use sequential metadata reads to avoid EMFILE in large directories.
789
- // Any per-entry metadata failure should not fail the whole listing.
790
- entries = [];
791
- for (const entry of pageEntries) {
792
- const entryPath = nodePath.join(dirPath, entry.path);
793
- try {
794
- const metadata = await LS_INTERNALS.readMetadata(entryPath);
795
- entries.push({
796
- ...entry,
797
- ...metadata,
798
- });
799
- }
800
- catch (err) {
801
- const code = getErrorCode(err);
802
- // File may disappear between readdir and lstat; skip stale entries.
803
- if (code === "ENOENT" || code === "ENOTDIR") {
804
- continue;
805
- }
806
- // Keep listing useful even if one entry's metadata cannot be read.
807
- entries.push(entry);
808
- }
809
- }
810
- }
811
- else {
812
- entries = pageEntries;
813
- }
814
- const output = {
815
- path: dirPath,
816
- depth,
817
- limit,
818
- offset,
819
- ignore,
820
- truncated,
821
- total,
822
- entries,
823
- };
824
- const truncationNote = truncated ? " (truncated)" : "";
825
- return success(`Listed ${entries.length} entries in ${dirPath}${truncationNote}`, output);
826
- }
827
- catch (err) {
828
- return fail(getErrorMessage(err));
829
- }
830
- }
831
- /**
832
- * Finds files matching a glob pattern.
833
- */
834
- export async function executeGlob(input, ctx) {
835
- if (ctx.abortSignal?.aborted)
836
- return fail("Operation cancelled");
837
- try {
838
- const cwd = input.cwd ? resolvePath(input.cwd, ctx) : ctx.workingDir;
839
- const files = await fastGlob.glob(input.pattern, {
840
- cwd,
841
- onlyFiles: true,
842
- absolute: false,
843
- ignore: input.ignore,
844
- });
845
- const truncated = files.length > FILESYSTEM_OUTPUT_LIMITS.MAX_GLOB_RESULTS;
846
- const cappedFiles = truncated
847
- ? files.slice(0, FILESYSTEM_OUTPUT_LIMITS.MAX_GLOB_RESULTS)
848
- : files;
849
- const suffix = truncated ? ` (truncated to ${cappedFiles.length})` : "";
850
- return success(`Found ${files.length} files matching ${input.pattern}${suffix}`, cappedFiles);
851
- }
852
- catch (err) {
853
- return fail(getErrorMessage(err));
854
- }
855
- }
856
- const MAX_GREP_PATTERN_LENGTH = 512;
857
- const MAX_GREP_QUANTIFIERS = 64;
858
- const BACKREFERENCE_RE = /\\[1-9][0-9]*/;
859
- const NESTED_QUANTIFIER_RE = /\((?:\?:)?(?:[^()\\]|\\.)*(?:\+|\*|\{\d+(?:,\d*)?\})(?:[^()\\]|\\.)*\)(?:\+|\*|\{\d+(?:,\d*)?\})/;
860
- const NESTED_WILDCARD_RE = /\((?:\?:)?(?:[^()\\]|\\.)*\.(?:\+|\*|\{\d+(?:,\d*)?\})(?:[^()\\]|\\.)*\)(?:\+|\*|\{\d+(?:,\d*)?\})/;
861
- const LOOKAROUND_RE = /\(\?(?:=|!|<=|<!)/;
862
- const REPEATED_SIMPLE_GROUP_RE = /\((?:\?:)?((?:[^()\\]|\\.)+)\)(?:\+|\*|\{\d+(?:,\d*)?\})/g;
863
- const UNESCAPED_QUANTIFIER_TOKEN_RE = /(^|[^\\])(?:\*|\+|\?|\{\d+(?:,\d*)?\})/;
864
- function splitTopLevelAlternatives(groupSource) {
865
- const parts = [];
866
- let current = "";
867
- let escaped = false;
868
- let inClass = false;
869
- let depth = 0;
870
- for (let i = 0; i < groupSource.length; i++) {
871
- const ch = groupSource[i];
872
- if (escaped) {
873
- current += ch;
874
- escaped = false;
875
- continue;
876
- }
877
- if (ch === "\\") {
878
- current += ch;
879
- escaped = true;
880
- continue;
881
- }
882
- if (inClass) {
883
- current += ch;
884
- if (ch === "]") {
885
- inClass = false;
886
- }
887
- continue;
888
- }
889
- if (ch === "[") {
890
- inClass = true;
891
- current += ch;
892
- continue;
893
- }
894
- if (ch === "(") {
895
- depth++;
896
- current += ch;
897
- continue;
898
- }
899
- if (ch === ")" && depth > 0) {
900
- depth--;
901
- current += ch;
902
- continue;
903
- }
904
- if (ch === "|" && depth === 0) {
905
- parts.push(current);
906
- current = "";
907
- continue;
908
- }
909
- current += ch;
910
- }
911
- parts.push(current);
912
- return parts;
913
- }
914
- function normalizeAlternative(raw) {
915
- return raw
916
- .replace(/\\./g, "x")
917
- .replace(/\[[^\]]*\]/g, "x")
918
- .replace(/[\^\$]/g, "")
919
- .replace(/(^|[^\\])(?:\*|\+|\?|\{\d+(?:,\d*)?\})/g, "$1");
920
- }
921
- function hasAmbiguousRepeatedAlternation(pattern) {
922
- for (const match of pattern.matchAll(new RegExp(REPEATED_SIMPLE_GROUP_RE.source, "g"))) {
923
- const groupSource = match[1];
924
- if (!groupSource || !groupSource.includes("|"))
925
- continue;
926
- const alternatives = splitTopLevelAlternatives(groupSource);
927
- if (alternatives.length < 2)
928
- continue;
929
- if (alternatives.some((alt) => alt.length === 0)) {
930
- return true;
931
- }
932
- // Quantified alternatives inside an already repeated group are common
933
- // catastrophic-backtracking shapes ((a+|aa)+, (a?|aa)+, etc.).
934
- if (alternatives.some((alt) => UNESCAPED_QUANTIFIER_TOKEN_RE.test(alt))) {
935
- return true;
936
- }
937
- const normalized = alternatives.map(normalizeAlternative);
938
- for (let i = 0; i < normalized.length; i++) {
939
- for (let j = i + 1; j < normalized.length; j++) {
940
- const left = normalized[i];
941
- const right = normalized[j];
942
- if (!left || !right) {
943
- return true;
944
- }
945
- if (left.startsWith(right) || right.startsWith(left)) {
946
- return true;
947
- }
948
- }
949
- }
950
- }
951
- return false;
952
- }
953
- function countUnescapedQuantifiers(pattern) {
954
- return [...pattern.matchAll(/(^|[^\\])(?:\*|\+|\?|\{\d+(?:,\d*)?\})/g)].length;
955
- }
956
- function getUnsafeRegexReason(pattern) {
957
- if (pattern.length > MAX_GREP_PATTERN_LENGTH) {
958
- return `pattern length exceeds ${MAX_GREP_PATTERN_LENGTH} characters`;
959
- }
960
- if (LOOKAROUND_RE.test(pattern)) {
961
- return "lookaround assertions are not allowed";
962
- }
963
- if (BACKREFERENCE_RE.test(pattern)) {
964
- return "backreferences are not allowed";
965
- }
966
- if (countUnescapedQuantifiers(pattern) > MAX_GREP_QUANTIFIERS) {
967
- return `pattern uses too many quantifiers (max ${MAX_GREP_QUANTIFIERS})`;
968
- }
969
- if (NESTED_QUANTIFIER_RE.test(pattern) || NESTED_WILDCARD_RE.test(pattern)) {
970
- return "nested quantifiers are not allowed";
971
- }
972
- if (hasAmbiguousRepeatedAlternation(pattern)) {
973
- return "ambiguous alternation inside repeated groups is not allowed";
974
- }
975
- return null;
976
- }
977
- /**
978
- * Searches for a pattern in files.
979
- */
980
- export async function executeGrep(input, ctx) {
981
- if (ctx.abortSignal?.aborted)
982
- return fail("Operation cancelled");
983
- try {
984
- const unsafeReason = getUnsafeRegexReason(input.pattern);
985
- if (unsafeReason) {
986
- return fail(`Unsafe regular expression rejected: ${unsafeReason}`);
987
- }
988
- // Validate regex
989
- let regex;
990
- try {
991
- regex = new RegExp(input.pattern, input.ignoreCase ? "gi" : "g");
992
- }
993
- catch {
994
- return fail(`Invalid regular expression: ${input.pattern}`);
995
- }
996
- const targetPath = resolvePath(input.path ?? ".", ctx);
997
- const stat = await fs.stat(targetPath);
998
- const matches = [];
999
- const errors = [];
1000
- let truncated = false;
1001
- if (stat.isDirectory()) {
1002
- // Search in directory
1003
- const globPattern = input.glob ?? "**/*";
1004
- const files = await fastGlob.glob(globPattern, {
1005
- cwd: targetPath,
1006
- onlyFiles: true,
1007
- absolute: true,
1008
- });
1009
- for (const file of files) {
1010
- if (matches.length >= FILESYSTEM_OUTPUT_LIMITS.MAX_GREP_MATCHES) {
1011
- truncated = true;
1012
- break;
1013
- }
1014
- const remaining = FILESYSTEM_OUTPUT_LIMITS.MAX_GREP_MATCHES - matches.length;
1015
- const result = await searchInFile(file, regex, remaining);
1016
- matches.push(...result.matches);
1017
- if (result.truncated) {
1018
- truncated = true;
1019
- }
1020
- if (result.error) {
1021
- if (errors.length < FILESYSTEM_OUTPUT_LIMITS.MAX_GREP_ERRORS) {
1022
- errors.push(result.error);
1023
- }
1024
- }
1025
- if (truncated) {
1026
- break;
1027
- }
1028
- }
1029
- }
1030
- else {
1031
- // Search in single file
1032
- const result = await searchInFile(targetPath, regex, FILESYSTEM_OUTPUT_LIMITS.MAX_GREP_MATCHES);
1033
- matches.push(...result.matches);
1034
- if (result.truncated) {
1035
- truncated = true;
1036
- }
1037
- if (result.error) {
1038
- if (errors.length < FILESYSTEM_OUTPUT_LIMITS.MAX_GREP_ERRORS) {
1039
- errors.push(result.error);
1040
- }
1041
- }
1042
- }
1043
- // Return matches with optional errors array for visibility
1044
- // Note: errors are logged but don't fail the overall search
1045
- const suffix = truncated ? ` (truncated to ${matches.length})` : "";
1046
- return success(`Found ${matches.length} matches for pattern "${input.pattern}"${suffix}`, {
1047
- matches,
1048
- errors: errors.length > 0 ? errors : undefined,
1049
- truncated,
1050
- });
1051
- }
1052
- catch (err) {
1053
- return fail(getErrorMessage(err));
1054
- }
1055
- }
1056
- /**
1057
- * Searches for a regex pattern in a single file.
1058
- * Uses string.match() instead of regex.test() to avoid global regex lastIndex issues.
1059
- */
1060
- async function searchInFile(filePath, regex, maxMatches) {
1061
- const matches = [];
1062
- let skippedLongLine = false;
1063
- if (maxMatches <= 0) {
1064
- return { matches, truncated: true };
1065
- }
1066
- try {
1067
- const stat = await fs.stat(filePath);
1068
- if (stat.size > FILESYSTEM_OUTPUT_LIMITS.MAX_GREP_FILE_BYTES) {
1069
- return {
1070
- matches: [],
1071
- error: `Skipped file ${filePath}: exceeds ${FILESYSTEM_OUTPUT_LIMITS.MAX_GREP_FILE_BYTES} bytes`,
1072
- };
1073
- }
1074
- // Create a non-global version for testing to avoid lastIndex issues
1075
- const testRegex = new RegExp(regex.source, regex.flags.replace("g", ""));
1076
- const input = fsSync.createReadStream(filePath, { encoding: "utf8" });
1077
- const lines = readline.createInterface({ input, crlfDelay: Infinity });
1078
- let lineNumber = 0;
1079
- try {
1080
- for await (const line of lines) {
1081
- lineNumber++;
1082
- const lineBytes = Buffer.byteLength(line, "utf8");
1083
- if (lineBytes > FILESYSTEM_OUTPUT_LIMITS.MAX_GREP_LINE_BYTES) {
1084
- skippedLongLine = true;
1085
- continue;
1086
- }
1087
- if (!testRegex.test(line)) {
1088
- continue;
1089
- }
1090
- matches.push({
1091
- file: filePath,
1092
- line: lineNumber,
1093
- content: line,
1094
- });
1095
- if (matches.length >= maxMatches) {
1096
- lines.close();
1097
- input.destroy();
1098
- return { matches, truncated: true };
1099
- }
1100
- }
1101
- if (skippedLongLine) {
1102
- return {
1103
- matches,
1104
- error: `Skipped lines longer than ${FILESYSTEM_OUTPUT_LIMITS.MAX_GREP_LINE_BYTES} bytes in ${filePath}`,
1105
- };
1106
- }
1107
- return { matches };
1108
- }
1109
- finally {
1110
- lines.close();
1111
- input.destroy();
1112
- }
1113
- }
1114
- catch (err) {
1115
- // Return error info for files that can't be read (binary, permissions, etc.)
1116
- // This allows callers to decide whether to log or surface these errors
1117
- return {
1118
- matches: [],
1119
- error: `Could not read file ${filePath}: ${getErrorMessage(err)}`,
1120
- };
1121
- }
1122
- }