@aria-cli/tools 1.0.9 → 1.0.11

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