@j0hanz/filesystem-mcp 2.8.0 → 2.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -26
- package/dist/cli-help.d.ts.map +1 -1
- package/dist/cli-help.js +10 -1
- package/dist/cli-help.js.map +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +10 -3
- package/dist/cli.js.map +1 -1
- package/dist/core/concurrency.d.ts +4 -4
- package/dist/core/concurrency.d.ts.map +1 -1
- package/dist/core/concurrency.js +1 -1
- package/dist/core/concurrency.js.map +1 -1
- package/dist/core/config.d.ts +2 -0
- package/dist/core/config.d.ts.map +1 -1
- package/dist/core/config.js.map +1 -1
- package/dist/core/cursor.d.ts +8 -0
- package/dist/core/cursor.d.ts.map +1 -1
- package/dist/core/cursor.js +14 -0
- package/dist/core/cursor.js.map +1 -1
- package/dist/core/file-uri.d.ts +12 -3
- package/dist/core/file-uri.d.ts.map +1 -1
- package/dist/core/file-uri.js +35 -7
- package/dist/core/file-uri.js.map +1 -1
- package/dist/core/fmt.d.ts +2 -0
- package/dist/core/fmt.d.ts.map +1 -1
- package/dist/core/fmt.js +16 -0
- package/dist/core/fmt.js.map +1 -1
- package/dist/core/fs.d.ts +16 -1
- package/dist/core/fs.d.ts.map +1 -1
- package/dist/core/fs.js +191 -72
- package/dist/core/fs.js.map +1 -1
- package/dist/core/glob.d.ts +8 -0
- package/dist/core/glob.d.ts.map +1 -1
- package/dist/core/glob.js +84 -11
- package/dist/core/glob.js.map +1 -1
- package/dist/core/input-required.d.ts +7 -0
- package/dist/core/input-required.d.ts.map +1 -1
- package/dist/core/input-required.js +13 -0
- package/dist/core/input-required.js.map +1 -1
- package/dist/core/path-completer.d.ts.map +1 -1
- package/dist/core/path-completer.js +5 -3
- package/dist/core/path-completer.js.map +1 -1
- package/dist/core/path-utils.d.ts +51 -12
- package/dist/core/path-utils.d.ts.map +1 -1
- package/dist/core/path-utils.js +69 -29
- package/dist/core/path-utils.js.map +1 -1
- package/dist/core/path.d.ts +108 -21
- package/dist/core/path.d.ts.map +1 -1
- package/dist/core/path.js +436 -83
- package/dist/core/path.js.map +1 -1
- package/dist/core/read.d.ts +13 -3
- package/dist/core/read.d.ts.map +1 -1
- package/dist/core/read.js +46 -40
- package/dist/core/read.js.map +1 -1
- package/dist/core/schema.d.ts +9 -6
- package/dist/core/schema.d.ts.map +1 -1
- package/dist/core/schema.js +22 -19
- package/dist/core/schema.js.map +1 -1
- package/dist/core/search.d.ts +6 -0
- package/dist/core/search.d.ts.map +1 -1
- package/dist/core/search.js +159 -33
- package/dist/core/search.js.map +1 -1
- package/dist/core/sensitive.d.ts.map +1 -1
- package/dist/core/sensitive.js +71 -29
- package/dist/core/sensitive.js.map +1 -1
- package/dist/core/store.d.ts +1 -0
- package/dist/core/store.d.ts.map +1 -1
- package/dist/core/store.js +3 -0
- package/dist/core/store.js.map +1 -1
- package/dist/core/util.d.ts +12 -1
- package/dist/core/util.d.ts.map +1 -1
- package/dist/core/util.js +24 -1
- package/dist/core/util.js.map +1 -1
- package/dist/prompts.d.ts +1 -1
- package/dist/prompts.d.ts.map +1 -1
- package/dist/prompts.js +1 -1
- package/dist/prompts.js.map +1 -1
- package/dist/resources.d.ts +1 -2
- package/dist/resources.d.ts.map +1 -1
- package/dist/resources.js +20 -13
- package/dist/resources.js.map +1 -1
- package/dist/server.js +2 -2
- package/dist/server.js.map +1 -1
- package/dist/tools/create.d.ts.map +1 -1
- package/dist/tools/create.js +122 -45
- package/dist/tools/create.js.map +1 -1
- package/dist/tools/define.d.ts +8 -4
- package/dist/tools/define.d.ts.map +1 -1
- package/dist/tools/define.js +36 -9
- package/dist/tools/define.js.map +1 -1
- package/dist/tools/delete.d.ts.map +1 -1
- package/dist/tools/delete.js +57 -17
- package/dist/tools/delete.js.map +1 -1
- package/dist/tools/diff.js +1 -1
- package/dist/tools/diff.js.map +1 -1
- package/dist/tools/edit.d.ts.map +1 -1
- package/dist/tools/edit.js +14 -12
- package/dist/tools/edit.js.map +1 -1
- package/dist/tools/find-files.d.ts.map +1 -1
- package/dist/tools/find-files.js +20 -20
- package/dist/tools/find-files.js.map +1 -1
- package/dist/tools/index.d.ts +1 -1
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +1 -1
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/list.d.ts.map +1 -1
- package/dist/tools/list.js +142 -73
- package/dist/tools/list.js.map +1 -1
- package/dist/tools/move.d.ts.map +1 -1
- package/dist/tools/move.js +81 -36
- package/dist/tools/move.js.map +1 -1
- package/dist/tools/patch.js +13 -6
- package/dist/tools/patch.js.map +1 -1
- package/dist/tools/read.d.ts.map +1 -1
- package/dist/tools/read.js +114 -45
- package/dist/tools/read.js.map +1 -1
- package/dist/tools/replace-text.d.ts.map +1 -1
- package/dist/tools/replace-text.js +49 -35
- package/dist/tools/replace-text.js.map +1 -1
- package/dist/tools/search-text.d.ts.map +1 -1
- package/dist/tools/search-text.js +20 -49
- package/dist/tools/search-text.js.map +1 -1
- package/dist/tools/stat.d.ts.map +1 -1
- package/dist/tools/stat.js +26 -8
- package/dist/tools/stat.js.map +1 -1
- package/dist/transport/http-policy.d.ts +3 -2
- package/dist/transport/http-policy.d.ts.map +1 -1
- package/dist/transport/http-policy.js +3 -2
- package/dist/transport/http-policy.js.map +1 -1
- package/dist/transport/http.d.ts.map +1 -1
- package/dist/transport/http.js +2 -2
- package/dist/transport/http.js.map +1 -1
- package/dist/transport/stdio.js +1 -1
- package/dist/transport/stdio.js.map +1 -1
- package/package.json +5 -5
|
@@ -51,19 +51,17 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
|
|
|
51
51
|
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
52
52
|
});
|
|
53
53
|
import { Buffer, isUtf8 } from 'node:buffer';
|
|
54
|
-
import { dirname } from 'node:path';
|
|
55
54
|
import * as z from 'zod/v4';
|
|
56
55
|
import { processEntriesConcurrently } from "../core/concurrency.js";
|
|
57
56
|
import { unifiedPatch } from "../core/diff.js";
|
|
58
|
-
import { ErrorCode, FsError, Problem } from "../core/errors.js";
|
|
57
|
+
import { ErrorCode, FsError, isFsError, isSkippableErrno, Problem } from "../core/errors.js";
|
|
59
58
|
import { buildWrittenFileMeta } from "../core/file-uri.js";
|
|
60
59
|
import { stoppedEarlyLine, truncateProgressPattern, zeroMatchHint } from "../core/fmt.js";
|
|
61
60
|
import { globEntries } from "../core/glob.js";
|
|
62
|
-
import { toPosixRelative } from "../core/path.js";
|
|
63
61
|
import { createTooLargeError, readFileBufferWithLimit } from "../core/read.js";
|
|
64
|
-
import { defaultFalseBoolean, IncludeHidden, IncludeIgnored, MaxDepth, nonBlankString, OptionalPath, SafeGlobPattern, } from "../core/schema.js";
|
|
65
|
-
import { compileRegex, createYielder, execMatches, freeRegex, RE2_MAX_INPUT_BYTES, } from "../core/search.js";
|
|
66
|
-
import { DEFAULT_SEARCH_RESULTS,
|
|
62
|
+
import { defaultFalseBoolean, IncludeHidden, IncludeIgnored, mapOptionalPath, MaxDepth, nonBlankString, OptionalPath, SafeGlobPattern, } from "../core/schema.js";
|
|
63
|
+
import { compileRegex, createYielder, execMatches, freeRegex, RE2_MAX_INPUT_BYTES, resolveSearchScope, } from "../core/search.js";
|
|
64
|
+
import { DEFAULT_SEARCH_RESULTS, escapeRegExp, getMaxTextFileSize, MAX_SEARCH_RESULTS, PARALLEL_CONCURRENCY, } from "../core/util.js";
|
|
67
65
|
import { isTotalFailure } from "./batch.js";
|
|
68
66
|
import { defineTool } from "./define.js";
|
|
69
67
|
const SearchAndReplaceInputSchema = z.strictObject({
|
|
@@ -111,12 +109,12 @@ function recordFailure(summary, entryPath, error) {
|
|
|
111
109
|
if (summary.failures.length >= MAX_FAILURES)
|
|
112
110
|
return;
|
|
113
111
|
summary.failures.push({
|
|
114
|
-
path:
|
|
112
|
+
path: summary.paths.show(entryPath),
|
|
115
113
|
error: Problem.fromUnknown(error, ErrorCode.UNKNOWN, entryPath),
|
|
116
114
|
});
|
|
117
115
|
}
|
|
118
116
|
function recordChangedFile(summary, plan, filePath) {
|
|
119
|
-
const relativePath =
|
|
117
|
+
const relativePath = summary.paths.show(filePath);
|
|
120
118
|
// The first changed file is the one the resource_link points at; keep what
|
|
121
119
|
// was written so the link needs no read-back.
|
|
122
120
|
summary.primary ??= { path: filePath, content: plan.updatedContent };
|
|
@@ -227,6 +225,9 @@ function createCaseSensitiveLiteralMatcher(searchPattern) {
|
|
|
227
225
|
}
|
|
228
226
|
async function processEntry(entryPath, ctx) {
|
|
229
227
|
const { dryRun, returnDiff, signal, summary } = ctx;
|
|
228
|
+
// Discovery/read failures on a walked file are skipped silently; a failed
|
|
229
|
+
// write is the caller's edit not landing, so it is always reported.
|
|
230
|
+
const write = { started: false };
|
|
230
231
|
try {
|
|
231
232
|
// Lock the read→write per file so a concurrent writer is not lost; a dry
|
|
232
233
|
// run never writes and skips the lock.
|
|
@@ -234,8 +235,10 @@ async function processEntry(entryPath, ctx) {
|
|
|
234
235
|
? await readReplacementPlan(entryPath, ctx)
|
|
235
236
|
: await ctx.fs.lockForWrite(entryPath, async () => {
|
|
236
237
|
const planned = await readReplacementPlan(entryPath, ctx);
|
|
237
|
-
if (planned)
|
|
238
|
+
if (planned) {
|
|
239
|
+
write.started = true;
|
|
238
240
|
await ctx.fs.writeFile(entryPath, planned.updatedContent, { signal });
|
|
241
|
+
}
|
|
239
242
|
return planned;
|
|
240
243
|
});
|
|
241
244
|
if (!plan) {
|
|
@@ -259,6 +262,16 @@ async function processEntry(entryPath, ctx) {
|
|
|
259
262
|
// stoppedReason 'timeout' instead.
|
|
260
263
|
if (signal?.aborted)
|
|
261
264
|
return;
|
|
265
|
+
if (!ctx.singleFile &&
|
|
266
|
+
!write.started &&
|
|
267
|
+
((isFsError(error) &&
|
|
268
|
+
(error.code === ErrorCode.ACCESS_DENIED ||
|
|
269
|
+
error.code === ErrorCode.NOT_FOUND ||
|
|
270
|
+
error.code === ErrorCode.SYMLINK_NOT_ALLOWED)) ||
|
|
271
|
+
isSkippableErrno(error))) {
|
|
272
|
+
summary.processedFiles--;
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
262
275
|
recordFailure(summary, entryPath, error);
|
|
263
276
|
}
|
|
264
277
|
}
|
|
@@ -307,7 +320,7 @@ function maybeAppendPatchDiff(summary, params) {
|
|
|
307
320
|
summary.diffTruncated = true;
|
|
308
321
|
return;
|
|
309
322
|
}
|
|
310
|
-
const header =
|
|
323
|
+
const header = summary.paths.show(params.filePath);
|
|
311
324
|
const patch = unifiedPatch(header, params.originalContent, params.updatedContent);
|
|
312
325
|
if (patch === undefined) {
|
|
313
326
|
summary.diffTruncated = true;
|
|
@@ -319,9 +332,9 @@ function maybeAppendPatchDiff(summary, params) {
|
|
|
319
332
|
}
|
|
320
333
|
summary.diffTruncated = true;
|
|
321
334
|
}
|
|
322
|
-
function createReplaceSummary(
|
|
335
|
+
function createReplaceSummary(paths) {
|
|
323
336
|
return {
|
|
324
|
-
|
|
337
|
+
paths,
|
|
325
338
|
totalMatches: 0,
|
|
326
339
|
filesChanged: 0,
|
|
327
340
|
failedFiles: 0,
|
|
@@ -334,24 +347,6 @@ function createReplaceSummary(root) {
|
|
|
334
347
|
diffTruncated: false,
|
|
335
348
|
};
|
|
336
349
|
}
|
|
337
|
-
async function resolveSearchRoot(pathValue, fs) {
|
|
338
|
-
if (!pathValue) {
|
|
339
|
-
return { root: fs.pathGuard.resolvePathOrRoot(undefined) };
|
|
340
|
-
}
|
|
341
|
-
const resolvedPath = await fs.pathGuard.validateExistingPath(pathValue);
|
|
342
|
-
const { stats: fileStats } = await fs.stat(resolvedPath);
|
|
343
|
-
if (fileStats.isFile()) {
|
|
344
|
-
// A single explicit file target bypasses the glob machinery entirely:
|
|
345
|
-
// routing it through globEntries with baseNameMatch would rewrite the
|
|
346
|
-
// escaped basename to `**/${basename}` and match every same-named file
|
|
347
|
-
// under the parent tree, not just this one.
|
|
348
|
-
return {
|
|
349
|
-
root: dirname(resolvedPath),
|
|
350
|
-
singleFile: resolvedPath,
|
|
351
|
-
};
|
|
352
|
-
}
|
|
353
|
-
return { root: resolvedPath };
|
|
354
|
-
}
|
|
355
350
|
function buildSearchPattern(args) {
|
|
356
351
|
if (args.isRegex) {
|
|
357
352
|
return args.wholeWord ? `\\b(?:${args.searchPattern})\\b` : args.searchPattern;
|
|
@@ -373,6 +368,7 @@ function buildSearchAndReplaceStructuredResult(summary, args) {
|
|
|
373
368
|
...summary.failures.map((f) => ({ path: f.path, error: f.error })),
|
|
374
369
|
];
|
|
375
370
|
return {
|
|
371
|
+
root: summary.paths.root,
|
|
376
372
|
results,
|
|
377
373
|
// `failedFiles` counts every failure; `failures` itself is capped at
|
|
378
374
|
// MAX_FAILURES, so the summary must not be derived from the array length.
|
|
@@ -396,7 +392,7 @@ function buildSearchAndReplaceStructuredResult(summary, args) {
|
|
|
396
392
|
}
|
|
397
393
|
async function handleSearchAndReplace(args, ctx) {
|
|
398
394
|
const maxFileSize = getMaxTextFileSize();
|
|
399
|
-
const { root, singleFile } = await
|
|
395
|
+
const { basePath: root, explicitFile: singleFile } = await resolveSearchScope(args.path, ctx.fs);
|
|
400
396
|
const effectivePattern = args.pattern ?? '**/*';
|
|
401
397
|
const ceiling = ctx.fs.pathGuard.allowedRootContaining(root);
|
|
402
398
|
// An explicit single-file target bypasses baseNameMatch/exclude/hidden/
|
|
@@ -415,7 +411,9 @@ async function handleSearchAndReplace(args, ctx) {
|
|
|
415
411
|
suppressErrors: true,
|
|
416
412
|
...(args.maxDepth !== undefined ? { maxDepth: args.maxDepth } : {}),
|
|
417
413
|
});
|
|
418
|
-
|
|
414
|
+
// Relative to the root, not to the walked directory: `a.ts` from a run over
|
|
415
|
+
// `src` would name `<root>/a.ts` when passed back.
|
|
416
|
+
const summary = createReplaceSummary(ctx.fs.pathGuard.resultPaths(root));
|
|
419
417
|
// The matcher may own a compiled RE2 pattern, whose wasm memory re2-wasm
|
|
420
418
|
// never reclaims on its own. Nothing past the scan touches it, so free it the
|
|
421
419
|
// moment the scan is done — success, failure, or abort alike.
|
|
@@ -480,6 +478,7 @@ async function handleSearchAndReplace(args, ctx) {
|
|
|
480
478
|
content: summary.primary.content,
|
|
481
479
|
dryRun: args.dryRun,
|
|
482
480
|
lastModified: primaryModified,
|
|
481
|
+
transport: ctx.transport,
|
|
483
482
|
}).resourceLink
|
|
484
483
|
: undefined;
|
|
485
484
|
return link ? { structured, link } : { structured };
|
|
@@ -492,7 +491,7 @@ export const REPLACE_TEXT = defineTool({
|
|
|
492
491
|
'and ^ and $ anchor to file start and end. edit replaces one unique exact match.',
|
|
493
492
|
input: SearchAndReplaceInputSchema,
|
|
494
493
|
readOnlyHint: false,
|
|
495
|
-
|
|
494
|
+
deadline: true,
|
|
496
495
|
progress: (args) => {
|
|
497
496
|
const dryLabel = args.dryRun ? ' [dry run]' : '';
|
|
498
497
|
return {
|
|
@@ -500,7 +499,7 @@ export const REPLACE_TEXT = defineTool({
|
|
|
500
499
|
subject: `${truncateProgressPattern(args.searchPattern)} → ${truncateProgressPattern(args.replacement)}`,
|
|
501
500
|
};
|
|
502
501
|
},
|
|
503
|
-
|
|
502
|
+
mapPaths: mapOptionalPath,
|
|
504
503
|
run: async (args, ctx) => {
|
|
505
504
|
const truncatedPattern = truncateProgressPattern(args.searchPattern);
|
|
506
505
|
const { structured, link } = await handleSearchAndReplace(args, ctx);
|
|
@@ -516,8 +515,23 @@ export const REPLACE_TEXT = defineTool({
|
|
|
516
515
|
// model, so the stop state and the diff preview must ride the text.
|
|
517
516
|
const lines = [summaryText];
|
|
518
517
|
const stop = structured.stoppedReason;
|
|
519
|
-
if (stop === 'timeout')
|
|
518
|
+
if (stop === 'timeout') {
|
|
520
519
|
lines.push(stoppedEarlyLine(stop));
|
|
520
|
+
// A live run writes as it scans, so a timeout leaves the files it reached
|
|
521
|
+
// rewritten. The paths ship under `_meta`, which the model does not see; name
|
|
522
|
+
// them here, or a re-run of a self-matching replacement applies it twice.
|
|
523
|
+
if (!args.dryRun && structured.summary.succeeded > 0) {
|
|
524
|
+
const total = structured.summary.succeeded;
|
|
525
|
+
const shown = structured.results
|
|
526
|
+
.filter((x) => !('error' in x))
|
|
527
|
+
.slice(0, 5)
|
|
528
|
+
.map((x) => x.path)
|
|
529
|
+
.join(', ');
|
|
530
|
+
const more = total > 5 ? `, and ${String(total - 5)} more` : '';
|
|
531
|
+
lines.push(`// ${String(total)} file(s) were already rewritten and stay changed: ${shown}${more}. ` +
|
|
532
|
+
'Re-running the same replacement applies it to them again; exclude them, or use dryRun first.');
|
|
533
|
+
}
|
|
534
|
+
}
|
|
521
535
|
else if (stop !== undefined) {
|
|
522
536
|
// maxResults/maxFiles here are the caller's own caps, so raising one is
|
|
523
537
|
// what reaches the rest; the search engine's advice to narrow is not.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"replace-text.js","sourceRoot":"","sources":["../../src/tools/replace-text.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,OAAO,KAAK,CAAC,MAAM,QAAQ,CAAC;AAG5B,OAAO,EAAE,0BAA0B,EAAE,MAAM,wBAAwB,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAChE,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAC3D,OAAO,EAAE,gBAAgB,EAAE,uBAAuB,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAE1F,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAi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AI,CACR,kBAAkB,MAAM,CAAC,aAAa,GAAG,IAAI,CAAC,yCAAyC,CACxF,CAAC;QACJ,CAAC;QACD,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,UAAU,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QAClF,OAAO;YACL,UAAU;YACV,IAAI;YACJ,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE;YAC7B,OAAO,EAAE,cAAc,CAAC,UAAU,CAAC,OAAO,CAAC;SAC5C,CAAC;IACJ,CAAC;CACF,CAAC,CAAC","sourcesContent":["import type { ContentBlock } from '@modelcontextprotocol/server';\n\nimport { Buffer, isUtf8 } from 'node:buffer';\nimport { dirname } from 'node:path';\n\nimport * as z from 'zod/v4';\n\nimport type { StoppedReason } from '../core/concurrency.ts';\nimport { processEntriesConcurrently } from '../core/concurrency.ts';\nimport { unifiedPatch } from '../core/diff.ts';\nimport { ErrorCode, FsError, Problem } from '../core/errors.ts';\nimport { buildWrittenFileMeta } from '../core/file-uri.ts';\nimport { stoppedEarlyLine, truncateProgressPattern, zeroMatchHint } from '../core/fmt.ts';\nimport type { GuardedFileSystem } from '../core/fs.ts';\nimport { globEntries } from '../core/glob.ts';\nimport { toPosixRelative } from '../core/path.ts';\nimport { createTooLargeError, readFileBufferWithLimit } from '../core/read.ts';\nimport {\n defaultFalseBoolean,\n IncludeHidden,\n IncludeIgnored,\n MaxDepth,\n nonBlankString,\n OptionalPath,\n SafeGlobPattern,\n} from '../core/schema.ts';\nimport type { Regex } from '../core/search.ts';\nimport {\n compileRegex,\n createYielder,\n execMatches,\n freeRegex,\n RE2_MAX_INPUT_BYTES,\n} from '../core/search.ts';\nimport {\n DEFAULT_SEARCH_RESULTS,\n DEFAULT_SEARCH_TIMEOUT_MS,\n escapeRegExp,\n getMaxTextFileSize,\n MAX_SEARCH_RESULTS,\n PARALLEL_CONCURRENCY,\n} from '../core/util.ts';\nimport { type BatchResult, isTotalFailure } from './batch.ts';\nimport { defineTool, type ToolCtx } from './define.ts';\n\nconst SearchAndReplaceInputSchema = z.strictObject({\n path: OptionalPath.describe(\n 'File to rewrite, or directory to rewrite under. Omitting it targets the ENTIRE first allowed root — ' +\n 'scope it deliberately, and pair a wide scope with dryRun=true first',\n ),\n pattern: SafeGlobPattern.optional().describe(\n 'Glob to restrict replacements to specific file types (e.g. **/*.ts); default: all text files',\n ),\n searchPattern: nonBlankString('searchPattern')\n .max(10000)\n .describe(\n 'Exact literal text or RE2 regex pattern to search for. When isRegex=true, uses RE2 syntax (no lookahead, lookbehind, or backreferences are supported). Cannot be empty or whitespace-only.',\n ),\n replacement: z\n .string()\n .max(10000)\n .describe(\n 'Replacement text. Use capture group references ($1, $2, etc.) when isRegex=true. Use an empty string to delete all matches.',\n )\n .meta({ examples: ['$1_renamed', '', 'TODO: fix'] }),\n isRegex: defaultFalseBoolean('Treat searchPattern as a RE2 regex (default: literal text match)'),\n includeHidden: IncludeHidden,\n includeIgnored: IncludeIgnored,\n caseSensitive: defaultFalseBoolean('Enable case-sensitive matching (default: case-insensitive)'),\n wholeWord: defaultFalseBoolean('Match whole words only (word boundary anchoring)'),\n dryRun: defaultFalseBoolean(\n 'Preview replacements without writing to disk (default: false = apply changes)',\n ),\n returnDiff: defaultFalseBoolean('Include a unified diff of all changes in the response'),\n maxResults: z\n .uint32()\n .min(1)\n .max(MAX_SEARCH_RESULTS)\n .optional()\n .default(DEFAULT_SEARCH_RESULTS)\n .describe('Maximum total match count across all files before stopping'),\n maxFiles: z\n .uint32()\n .min(1)\n .max(MAX_SEARCH_RESULTS)\n .optional()\n .describe('Maximum number of files to process'),\n maxDepth: MaxDepth,\n});\n\ntype SearchAndReplaceOutput = BatchResult<{ matches: number }> & {\n totalMatches: number;\n filesScanned: number;\n skippedBinary?: number;\n resultsTruncated?: boolean;\n diff?: string;\n diffTruncated?: boolean;\n stoppedReason?: StoppedReason;\n};\n\nconst MAX_FAILURES = 20;\nconst REPLACE_CONCURRENCY = Math.min(PARALLEL_CONCURRENCY, 8);\nconst MAX_CHANGED_FILES = 100;\nconst MAX_DIFF_SIZE = 20 * 1024; // 20KB limit for diff output\nconst DIFF_APPEND_BUFFER = 1024;\n\ninterface Failure {\n path: string;\n error: Problem;\n}\n\n/** Counts every failure; keeps the first MAX_FAILURES of them for the result. */\nfunction recordFailure(summary: ReplaceSummary, entryPath: string, error: unknown): void {\n summary.failedFiles++;\n if (summary.failures.length >= MAX_FAILURES) return;\n summary.failures.push({\n path: toPosixRelative(summary.root, entryPath),\n error: Problem.fromUnknown(error, ErrorCode.UNKNOWN, entryPath),\n });\n}\n\nfunction recordChangedFile(summary: ReplaceSummary, plan: ReplacementPlan, filePath: string): void {\n const relativePath = toPosixRelative(summary.root, filePath);\n // The first changed file is the one the resource_link points at; keep what\n // was written so the link needs no read-back.\n summary.primary ??= { path: filePath, content: plan.updatedContent };\n if (summary.changedFiles.length < MAX_CHANGED_FILES) {\n summary.changedFiles.push({ path: relativePath, matches: plan.matchCount });\n return;\n }\n summary.changedFilesTruncated = true;\n}\n\ninterface ReplacementMatcher {\n /** Yields periodically and throws on abort: a partial splice must never be written. */\n replace(\n content: string,\n replacement: string,\n signal: AbortSignal | undefined,\n ): Promise<{ content: string; matchCount: number }>;\n testBuffer(buffer: Buffer): boolean;\n /** Releases any compiled pattern this matcher owns. Idempotent. */\n dispose(): void;\n}\n\nconst DOLLAR_TOKEN = /\\$(\\$|&|`|'|<([^>]*)>|\\d{1,2})/g;\n\n/**\n * Expand `$`-substitutions in a replacement template the way `RegExp` does.\n *\n * We cannot hand the template to RE2's own string replacer: it throws\n * `Invalid replacement string` on any `$` not followed by a substitution it\n * recognises (so a replacement of `$100` or a trailing `$` fails outright,\n * where `RegExp` inserts the `$` literally), and it renders an out-of-range\n * `$5` as `$4`. The matcher splices matches itself, so this is the single owner\n * of the syntax.\n */\nfunction expandDollarTokens(\n template: string,\n match: string,\n groups: (string | undefined)[],\n offset: number,\n input: string,\n named: Record<string, string> | undefined,\n): string {\n return template.replace(DOLLAR_TOKEN, (token: string, kind: string, name?: string) => {\n if (kind === '$') return '$';\n if (kind === '&') return match;\n if (kind === '`') return input.slice(0, offset);\n if (kind === \"'\") return input.slice(offset + match.length);\n if (name !== undefined) {\n if (named && Object.hasOwn(named, name)) return named[name] ?? '';\n return token;\n }\n // `$12` prefers group 12, then falls back to group 1 followed by a literal\n // `2`, and stays literal when neither exists — RegExp's own precedence.\n const two = Number.parseInt(kind, 10);\n if (kind.length === 2 && two >= 1 && two <= groups.length) return groups[two - 1] ?? '';\n const one = Number.parseInt(kind.slice(0, 1), 10);\n if (one >= 1 && one <= groups.length) {\n return (groups[one - 1] ?? '') + (kind.length === 2 ? kind.slice(1) : '');\n }\n return token;\n });\n}\n\nfunction createRegexReplacementMatcher(\n regex: Regex,\n expandReplacement: boolean,\n): ReplacementMatcher {\n return {\n testBuffer(buffer: Buffer): boolean {\n if (buffer.length > RE2_MAX_INPUT_BYTES) {\n throw new FsError(\n ErrorCode.TOO_LARGE,\n `File too large for a regex replace (${String(buffer.length)} > ${String(RE2_MAX_INPUT_BYTES)} bytes). Regex mode is used for isRegex, wholeWord, or the default caseSensitive: false; pass caseSensitive: true with a literal searchPattern, or split the file.`,\n );\n }\n // The regex is global and shared across every file in the batch, so a\n // previous file's match would otherwise start this scan mid-string.\n regex.lastIndex = 0;\n return regex.test(buffer.toString('utf-8'));\n },\n async replace(content, replacement, signal) {\n // Splicing by hand rather than through `String.replace(regex, …)`: RE2's\n // own replacer indexes the JS string with the code-point offsets it\n // reports, which lands the replacement short by one per astral character\n // (most emoji) earlier in the file. execMatches reports UTF-16 offsets.\n let matchCount = 0;\n let updated = '';\n let cursor = 0;\n const tick = createYielder(signal);\n for (const match of execMatches(regex, content)) {\n matchCount++;\n // Only isRegex=true opts into $1/$& substitution. A literal search\n // reaches this matcher too (case-insensitive and wholeWord both need a\n // regex), and there the replacement must be inserted verbatim.\n updated +=\n content.slice(cursor, match.start) +\n (expandReplacement\n ? expandDollarTokens(\n replacement,\n match.text,\n match.groups,\n match.start,\n content,\n match.named,\n )\n : replacement);\n cursor = match.end;\n if (await tick()) signal?.throwIfAborted();\n }\n return { content: updated + content.slice(cursor), matchCount };\n },\n dispose(): void {\n freeRegex(regex);\n },\n };\n}\n\nfunction createCaseSensitiveLiteralMatcher(searchPattern: string): ReplacementMatcher {\n const searchBuffer = Buffer.from(searchPattern, 'utf8');\n\n return {\n testBuffer(buffer: Buffer): boolean {\n return buffer.indexOf(searchBuffer) !== -1;\n },\n // One native O(n) pass; nothing to yield for.\n replace(content, replacement) {\n let matchCount = 0;\n const updated = content.replaceAll(searchPattern, () => {\n matchCount++;\n return replacement;\n });\n return Promise.resolve({ content: updated, matchCount });\n },\n dispose(): void {\n // no compiled pattern to release\n },\n };\n}\n\ntype SearchAndReplaceArgs = z.infer<typeof SearchAndReplaceInputSchema>;\n\ninterface ReplaceContext {\n dryRun: boolean;\n returnDiff: boolean;\n replacement: string;\n matcher: ReplacementMatcher;\n maxFileSize: number;\n signal: AbortSignal | undefined;\n summary: ReplaceSummary;\n fs: GuardedFileSystem;\n singleFile: boolean;\n}\n\ninterface ReplacementPlan {\n matchCount: number;\n originalContent: string;\n updatedContent: string;\n}\n\nasync function processEntry(entryPath: string, ctx: ReplaceContext): Promise<void> {\n const { dryRun, returnDiff, signal, summary } = ctx;\n\n try {\n // Lock the read→write per file so a concurrent writer is not lost; a dry\n // run never writes and skips the lock.\n const plan = dryRun\n ? await readReplacementPlan(entryPath, ctx)\n : await ctx.fs.lockForWrite(entryPath, async () => {\n const planned = await readReplacementPlan(entryPath, ctx);\n if (planned) await ctx.fs.writeFile(entryPath, planned.updatedContent, { signal });\n return planned;\n });\n if (!plan) {\n return;\n }\n\n // Bookkeep only after the write succeeds (or in dryRun, where there is no\n // write): a failed write must not count the file as changed.\n summary.totalMatches += plan.matchCount;\n summary.filesChanged++;\n\n recordChangedFile(summary, plan, entryPath);\n\n maybeAppendPatchDiff(summary, {\n filePath: entryPath,\n originalContent: plan.originalContent,\n updatedContent: plan.updatedContent,\n includeDiff: dryRun || returnDiff,\n });\n } catch (error) {\n // A timeout/cancel aborts the whole scan, not just this file: don't record\n // it as a per-file failure. processEntriesConcurrently reports the run as\n // stoppedReason 'timeout' instead.\n if (signal?.aborted) return;\n recordFailure(summary, entryPath, error);\n }\n}\n\nasync function readReplacementPlan(\n validPath: string,\n ctx: ReplaceContext,\n): Promise<ReplacementPlan | undefined> {\n const { matcher, replacement, maxFileSize, signal } = ctx;\n await using fileHandle = await ctx.fs.open(validPath);\n const stats = await fileHandle.stat();\n if (stats.size > maxFileSize) {\n throw createTooLargeError(stats.size, maxFileSize, validPath);\n }\n\n const buffer = await readFileBufferWithLimit(fileHandle, maxFileSize, validPath, signal);\n if (!matcher.testBuffer(buffer)) return undefined;\n // Only bytes that decode to UTF-8 losslessly survive the string round trip;\n // anything else would be written back with U+FFFD in place of every bad byte.\n // NUL is valid UTF-8 but marks a binary file, as it does for `read`.\n if (!isUtf8(buffer) || buffer.includes(0)) {\n if (ctx.singleFile) {\n throw new FsError(ErrorCode.INVALID_INPUT, 'Binary or non-UTF-8 file detected.', validPath);\n }\n ctx.summary.skippedBinary++;\n return undefined;\n }\n\n const originalContent = buffer.toString('utf-8');\n const { content: updatedContent, matchCount } = await matcher.replace(\n originalContent,\n replacement,\n signal,\n );\n return matchCount === 0 ? undefined : { matchCount, originalContent, updatedContent };\n}\n\nfunction maybeAppendPatchDiff(\n summary: ReplaceSummary,\n params: {\n filePath: string;\n originalContent: string;\n updatedContent: string;\n includeDiff: boolean;\n },\n): void {\n if (!params.includeDiff) return;\n\n // Budget first: a full budget must not pay for a diff it will discard, and a\n // diff past its deadline counts as truncated output, not a failed file.\n if (summary.diff.length >= MAX_DIFF_SIZE) {\n summary.diffTruncated = true;\n return;\n }\n const header = toPosixRelative(summary.root, params.filePath);\n const patch = unifiedPatch(header, params.originalContent, params.updatedContent);\n if (patch === undefined) {\n summary.diffTruncated = true;\n return;\n }\n if (summary.diff.length + patch.length <= MAX_DIFF_SIZE + DIFF_APPEND_BUFFER) {\n summary.diff += patch;\n return;\n }\n summary.diffTruncated = true;\n}\n\ninterface ReplaceSummary {\n root: string;\n totalMatches: number;\n filesChanged: number;\n failedFiles: number;\n processedFiles: number;\n skippedBinary: number;\n failures: Failure[];\n changedFiles: { path: string; matches: number }[];\n changedFilesTruncated: boolean;\n /** First file changed, as written. */\n primary?: { path: string; content: string };\n diff: string;\n diffTruncated: boolean;\n stoppedReason?: StoppedReason;\n}\n\nfunction createReplaceSummary(root: string): ReplaceSummary {\n return {\n root,\n totalMatches: 0,\n filesChanged: 0,\n failedFiles: 0,\n processedFiles: 0,\n skippedBinary: 0,\n failures: [],\n changedFiles: [],\n changedFilesTruncated: false,\n diff: '',\n diffTruncated: false,\n };\n}\n\nasync function resolveSearchRoot(\n pathValue: string | undefined,\n fs: GuardedFileSystem,\n): Promise<{ root: string; singleFile?: string }> {\n if (!pathValue) {\n return { root: fs.pathGuard.resolvePathOrRoot(undefined) };\n }\n const resolvedPath = await fs.pathGuard.validateExistingPath(pathValue);\n const { stats: fileStats } = await fs.stat(resolvedPath);\n if (fileStats.isFile()) {\n // A single explicit file target bypasses the glob machinery entirely:\n // routing it through globEntries with baseNameMatch would rewrite the\n // escaped basename to `**/${basename}` and match every same-named file\n // under the parent tree, not just this one.\n return {\n root: dirname(resolvedPath),\n singleFile: resolvedPath,\n };\n }\n return { root: resolvedPath };\n}\n\nfunction buildSearchPattern(args: SearchAndReplaceArgs): string {\n if (args.isRegex) {\n return args.wholeWord ? `\\\\b(?:${args.searchPattern})\\\\b` : args.searchPattern;\n }\n const escaped = escapeRegExp(args.searchPattern);\n return args.wholeWord ? `\\\\b(?:${escaped})\\\\b` : escaped;\n}\n\nfunction createReplacementMatcher(args: SearchAndReplaceArgs): ReplacementMatcher {\n // Use regex when isRegex, wholeWord, or case-insensitive (all require RE2)\n if (args.isRegex || args.wholeWord || !args.caseSensitive) {\n const regex = compileRegex(buildSearchPattern(args), { caseSensitive: args.caseSensitive });\n return createRegexReplacementMatcher(regex, args.isRegex);\n }\n return createCaseSensitiveLiteralMatcher(args.searchPattern);\n}\n\nfunction buildSearchAndReplaceStructuredResult(\n summary: ReplaceSummary,\n args: SearchAndReplaceArgs,\n): SearchAndReplaceOutput {\n const results = [\n ...summary.changedFiles.map((f) => ({ path: f.path, value: { matches: f.matches } })),\n ...summary.failures.map((f) => ({ path: f.path, error: f.error })),\n ];\n return {\n results,\n // `failedFiles` counts every failure; `failures` itself is capped at\n // MAX_FAILURES, so the summary must not be derived from the array length.\n summary: {\n total: summary.filesChanged + summary.failedFiles,\n succeeded: summary.filesChanged,\n failed: summary.failedFiles,\n },\n totalMatches: summary.totalMatches,\n filesScanned: summary.processedFiles,\n ...(summary.skippedBinary ? { skippedBinary: summary.skippedBinary } : {}),\n // Failures are capped at MAX_FAILURES independently of the changed-file\n // cap, so either cap can leave `results` shorter than `summary.total`.\n ...(summary.changedFilesTruncated || summary.failures.length < summary.failedFiles\n ? { resultsTruncated: true }\n : {}),\n ...((args.dryRun || args.returnDiff) && summary.diff ? { diff: summary.diff } : {}),\n ...(summary.diffTruncated ? { diffTruncated: true } : {}),\n ...(summary.stoppedReason ? { stoppedReason: summary.stoppedReason } : {}),\n };\n}\n\nasync function handleSearchAndReplace(\n args: SearchAndReplaceArgs,\n ctx: ToolCtx,\n): Promise<{\n structured: SearchAndReplaceOutput;\n link?: ContentBlock;\n}> {\n const maxFileSize = getMaxTextFileSize();\n const { root, singleFile } = await resolveSearchRoot(args.path, ctx.fs);\n const effectivePattern = args.pattern ?? '**/*';\n const ceiling = ctx.fs.pathGuard.allowedRootContaining(root);\n\n // An explicit single-file target bypasses baseNameMatch/exclude/hidden/\n // gitignore filtering — it should always be processed as the one file named.\n const entries: AsyncIterable<{ path: string }> | Iterable<{ path: string }> = singleFile\n ? [{ path: singleFile }]\n : globEntries({\n cwd: root,\n pattern: effectivePattern,\n includeHidden: args.includeHidden,\n skipIgnored: !args.includeIgnored,\n ...(ceiling !== undefined ? { ignoreCeiling: ceiling } : {}),\n signal: ctx.signal,\n baseNameMatch: true,\n onlyFiles: true,\n suppressErrors: true,\n ...(args.maxDepth !== undefined ? { maxDepth: args.maxDepth } : {}),\n });\n\n const summary = createReplaceSummary(root);\n\n // The matcher may own a compiled RE2 pattern, whose wasm memory re2-wasm\n // never reclaims on its own. Nothing past the scan touches it, so free it the\n // moment the scan is done — success, failure, or abort alike.\n const matcher = createReplacementMatcher(args);\n let stoppedReason: StoppedReason | undefined;\n try {\n const context: ReplaceContext = {\n dryRun: args.dryRun,\n returnDiff: args.returnDiff,\n replacement: args.replacement,\n matcher,\n maxFileSize,\n signal: ctx.signal,\n summary,\n fs: ctx.fs,\n singleFile: singleFile !== undefined,\n };\n\n stoppedReason = await processEntriesConcurrently(entries, {\n signal: ctx.signal,\n concurrency: REPLACE_CONCURRENCY,\n ...(args.maxFiles !== undefined ? { maxEntries: args.maxFiles } : {}),\n shouldStop: () => summary.totalMatches >= args.maxResults,\n onEntry: () => {\n summary.processedFiles++;\n ctx.onProgress({ current: summary.processedFiles });\n },\n onError: (entryPath, err) => {\n recordFailure(summary, entryPath, err);\n },\n runEntry: (entryPath) => processEntry(entryPath, context),\n });\n } finally {\n matcher.dispose();\n }\n if (stoppedReason !== undefined) summary.stoppedReason = stoppedReason;\n // A single file that aborts mid-scan throws inside processEntry (caught and\n // swallowed as a timeout) and leaves the dispatch loop with no more entries,\n // so processEntriesConcurrently returns undefined; surface the timeout here so\n // the result is not reported as a clean \"0 matches\". search_text's engine\n // checks the signal directly, so only replace needs this fallback.\n else if (ctx.signal.aborted) summary.stoppedReason = 'timeout';\n\n ctx.onProgress({ current: summary.processedFiles });\n\n if (!args.dryRun && summary.totalMatches > 0) {\n ctx.log(\n 'info',\n `${summary.filesChanged} file(s) changed, ${summary.totalMatches} match(es)`,\n 'replace_text',\n );\n }\n\n const structured = buildSearchAndReplaceStructuredResult(summary, args);\n\n let primaryModified: string | undefined;\n if (summary.primary && !args.dryRun) {\n try {\n primaryModified = (\n await ctx.fs.stat(summary.primary.path, { signal: ctx.signal })\n ).stats.mtime.toISOString();\n } catch {\n /* result metadata only; the write already succeeded */\n }\n }\n const link = summary.primary\n ? buildWrittenFileMeta({\n validPath: summary.primary.path,\n content: summary.primary.content,\n dryRun: args.dryRun,\n lastModified: primaryModified,\n }).resourceLink\n : undefined;\n return link ? { structured, link } : { structured };\n}\n\nexport const REPLACE_TEXT = defineTool<typeof SearchAndReplaceInputSchema, SearchAndReplaceOutput>({\n name: 'replace_text',\n title: 'Search and Replace',\n description:\n 'Find and replace every occurrence of literal text or an RE2 regex, like sed -i, in one file or all files ' +\n 'under a directory, optionally filtered by glob. The pattern runs over the whole file: it can span lines, ' +\n 'and ^ and $ anchor to file start and end. edit replaces one unique exact match.',\n input: SearchAndReplaceInputSchema,\n readOnlyHint: false,\n timeoutMs: DEFAULT_SEARCH_TIMEOUT_MS,\n progress: (args) => {\n const dryLabel = args.dryRun ? ' [dry run]' : '';\n return {\n label: `Replace${dryLabel}`,\n subject: `${truncateProgressPattern(args.searchPattern)} → ${truncateProgressPattern(args.replacement)}`,\n };\n },\n accessPaths: (args) => (args.path ? [args.path] : []),\n run: async (args, ctx) => {\n const truncatedPattern = truncateProgressPattern(args.searchPattern);\n const { structured, link } = await handleSearchAndReplace(args, ctx);\n const dryLabel = args.dryRun ? ' [dry run]' : '';\n const summaryText =\n `replace_text: '${truncatedPattern}'${dryLabel}` +\n ` \\u00b7 ${String(structured.totalMatches)} match(es)` +\n ` in ${String(structured.summary.succeeded)} file(s)` +\n (structured.summary.failed > 0 ? ` \\u00b7 ${String(structured.summary.failed)} failed` : '') +\n (structured.skippedBinary\n ? ` \\u00b7 ${String(structured.skippedBinary)} binary skipped`\n : '');\n // The structured half ships under `_meta`, which clients do not show the\n // model, so the stop state and the diff preview must ride the text.\n const lines = [summaryText];\n const stop = structured.stoppedReason;\n if (stop === 'timeout') lines.push(stoppedEarlyLine(stop));\n else if (stop !== undefined) {\n // maxResults/maxFiles here are the caller's own caps, so raising one is\n // what reaches the rest; the search engine's advice to narrow is not.\n lines.push(\n `// scan stopped early: hit the ${stop} limit; later files were not scanned. A higher ${stop} reaches them.`,\n );\n }\n const hint =\n structured.totalMatches === 0\n ? zeroMatchHint({\n ...args,\n filesScanned: structured.filesScanned,\n // A failed file, or a binary one the needle did hit, explains the\n // zero better than a guess about the pattern.\n incomplete:\n stop !== undefined ||\n structured.summary.failed > 0 ||\n Boolean(structured.skippedBinary),\n })\n : undefined;\n if (hint) lines.push(hint);\n // A failure's reason lives in `results`, which ships under `_meta`. The\n // full relative path, not the basename: the error message may not carry\n // the path, and two same-named files in different directories must not\n // render as the same row.\n for (const r of structured.results.filter((x) => 'error' in x).slice(0, 3)) {\n lines.push(`// ${r.path}: ${r.error.code} ${r.error.message}`);\n }\n if (structured.diffTruncated) {\n lines.push(\n `// diff cut at ${String(MAX_DIFF_SIZE / 1024)} KB: some files' changes are not shown.`,\n );\n }\n const text = lines.join('\\n') + (structured.diff ? `\\n\\n${structured.diff}` : '');\n return {\n structured,\n text,\n resources: link ? [link] : [],\n isError: isTotalFailure(structured.summary),\n };\n },\n});\n"]}
|
|
1
|
+
{"version":3,"file":"replace-text.js","sourceRoot":"","sources":["../../src/tools/replace-text.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE7C,OAAO,KAAK,CAAC,MAAM,QAAQ,CAAC;AAG5B,OAAO,EAAE,0BAA0B,EAAE,MAAM,wBAAwB,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,OAAO,EAA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C,CAAC,EAAE,CAAC,CAAC;QAClF,OAAO;YACL,UAAU;YACV,IAAI;YACJ,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE;YAC7B,OAAO,EAAE,cAAc,CAAC,UAAU,CAAC,OAAO,CAAC;SAC5C,CAAC;IACJ,CAAC;CACF,CAAC,CAAC","sourcesContent":["import type { ContentBlock } from '@modelcontextprotocol/server';\n\nimport { Buffer, isUtf8 } from 'node:buffer';\n\nimport * as z from 'zod/v4';\n\nimport type { StoppedReason } from '../core/concurrency.ts';\nimport { processEntriesConcurrently } from '../core/concurrency.ts';\nimport { unifiedPatch } from '../core/diff.ts';\nimport { ErrorCode, FsError, isFsError, isSkippableErrno, Problem } from '../core/errors.ts';\nimport { buildWrittenFileMeta } from '../core/file-uri.ts';\nimport { stoppedEarlyLine, truncateProgressPattern, zeroMatchHint } from '../core/fmt.ts';\nimport type { GuardedFileSystem } from '../core/fs.ts';\nimport { globEntries } from '../core/glob.ts';\nimport type { ResultPaths } from '../core/path.ts';\nimport { createTooLargeError, readFileBufferWithLimit } from '../core/read.ts';\nimport {\n defaultFalseBoolean,\n IncludeHidden,\n IncludeIgnored,\n mapOptionalPath,\n MaxDepth,\n nonBlankString,\n OptionalPath,\n SafeGlobPattern,\n} from '../core/schema.ts';\nimport type { Regex } from '../core/search.ts';\nimport {\n compileRegex,\n createYielder,\n execMatches,\n freeRegex,\n RE2_MAX_INPUT_BYTES,\n resolveSearchScope,\n} from '../core/search.ts';\nimport {\n DEFAULT_SEARCH_RESULTS,\n escapeRegExp,\n getMaxTextFileSize,\n MAX_SEARCH_RESULTS,\n PARALLEL_CONCURRENCY,\n} from '../core/util.ts';\nimport { type BatchResult, isTotalFailure } from './batch.ts';\nimport { defineTool, type ToolCtx } from './define.ts';\n\nconst SearchAndReplaceInputSchema = z.strictObject({\n path: OptionalPath.describe(\n 'File to rewrite, or directory to rewrite under. Omitting it targets the ENTIRE first allowed root — ' +\n 'scope it deliberately, and pair a wide scope with dryRun=true first',\n ),\n pattern: SafeGlobPattern.optional().describe(\n 'Glob to restrict replacements to specific file types (e.g. **/*.ts); default: all text files',\n ),\n searchPattern: nonBlankString('searchPattern')\n .max(10000)\n .describe(\n 'Exact literal text or RE2 regex pattern to search for. When isRegex=true, uses RE2 syntax (no lookahead, lookbehind, or backreferences are supported). Cannot be empty or whitespace-only.',\n ),\n replacement: z\n .string()\n .max(10000)\n .describe(\n 'Replacement text. Use capture group references ($1, $2, etc.) when isRegex=true. Use an empty string to delete all matches.',\n )\n .meta({ examples: ['$1_renamed', '', 'TODO: fix'] }),\n isRegex: defaultFalseBoolean('Treat searchPattern as a RE2 regex (default: literal text match)'),\n includeHidden: IncludeHidden,\n includeIgnored: IncludeIgnored,\n caseSensitive: defaultFalseBoolean('Enable case-sensitive matching (default: case-insensitive)'),\n wholeWord: defaultFalseBoolean('Match whole words only (word boundary anchoring)'),\n dryRun: defaultFalseBoolean(\n 'Preview replacements without writing to disk (default: false = apply changes)',\n ),\n returnDiff: defaultFalseBoolean('Include a unified diff of all changes in the response'),\n maxResults: z\n .uint32()\n .min(1)\n .max(MAX_SEARCH_RESULTS)\n .optional()\n .default(DEFAULT_SEARCH_RESULTS)\n .describe('Maximum total match count across all files before stopping'),\n maxFiles: z\n .uint32()\n .min(1)\n .max(MAX_SEARCH_RESULTS)\n .optional()\n .describe('Maximum number of files to process'),\n maxDepth: MaxDepth,\n});\n\ntype SearchAndReplaceOutput = BatchResult<{ matches: number }> & {\n /** The root `results[].path` is relative to; a path that would not read back from it is absolute. */\n root: string;\n totalMatches: number;\n filesScanned: number;\n skippedBinary?: number;\n resultsTruncated?: boolean;\n diff?: string;\n diffTruncated?: boolean;\n stoppedReason?: StoppedReason;\n};\n\nconst MAX_FAILURES = 20;\nconst REPLACE_CONCURRENCY = Math.min(PARALLEL_CONCURRENCY, 8);\nconst MAX_CHANGED_FILES = 100;\nconst MAX_DIFF_SIZE = 20 * 1024; // 20KB limit for diff output\nconst DIFF_APPEND_BUFFER = 1024;\n\ninterface Failure {\n path: string;\n error: Problem;\n}\n\n/** Counts every failure; keeps the first MAX_FAILURES of them for the result. */\nfunction recordFailure(summary: ReplaceSummary, entryPath: string, error: unknown): void {\n summary.failedFiles++;\n if (summary.failures.length >= MAX_FAILURES) return;\n summary.failures.push({\n path: summary.paths.show(entryPath),\n error: Problem.fromUnknown(error, ErrorCode.UNKNOWN, entryPath),\n });\n}\n\nfunction recordChangedFile(summary: ReplaceSummary, plan: ReplacementPlan, filePath: string): void {\n const relativePath = summary.paths.show(filePath);\n // The first changed file is the one the resource_link points at; keep what\n // was written so the link needs no read-back.\n summary.primary ??= { path: filePath, content: plan.updatedContent };\n if (summary.changedFiles.length < MAX_CHANGED_FILES) {\n summary.changedFiles.push({ path: relativePath, matches: plan.matchCount });\n return;\n }\n summary.changedFilesTruncated = true;\n}\n\ninterface ReplacementMatcher {\n /** Yields periodically and throws on abort: a partial splice must never be written. */\n replace(\n content: string,\n replacement: string,\n signal: AbortSignal | undefined,\n ): Promise<{ content: string; matchCount: number }>;\n testBuffer(buffer: Buffer): boolean;\n /** Releases any compiled pattern this matcher owns. Idempotent. */\n dispose(): void;\n}\n\nconst DOLLAR_TOKEN = /\\$(\\$|&|`|'|<([^>]*)>|\\d{1,2})/g;\n\n/**\n * Expand `$`-substitutions in a replacement template the way `RegExp` does.\n *\n * We cannot hand the template to RE2's own string replacer: it throws\n * `Invalid replacement string` on any `$` not followed by a substitution it\n * recognises (so a replacement of `$100` or a trailing `$` fails outright,\n * where `RegExp` inserts the `$` literally), and it renders an out-of-range\n * `$5` as `$4`. The matcher splices matches itself, so this is the single owner\n * of the syntax.\n */\nfunction expandDollarTokens(\n template: string,\n match: string,\n groups: (string | undefined)[],\n offset: number,\n input: string,\n named: Record<string, string> | undefined,\n): string {\n return template.replace(DOLLAR_TOKEN, (token: string, kind: string, name?: string) => {\n if (kind === '$') return '$';\n if (kind === '&') return match;\n if (kind === '`') return input.slice(0, offset);\n if (kind === \"'\") return input.slice(offset + match.length);\n if (name !== undefined) {\n if (named && Object.hasOwn(named, name)) return named[name] ?? '';\n return token;\n }\n // `$12` prefers group 12, then falls back to group 1 followed by a literal\n // `2`, and stays literal when neither exists — RegExp's own precedence.\n const two = Number.parseInt(kind, 10);\n if (kind.length === 2 && two >= 1 && two <= groups.length) return groups[two - 1] ?? '';\n const one = Number.parseInt(kind.slice(0, 1), 10);\n if (one >= 1 && one <= groups.length) {\n return (groups[one - 1] ?? '') + (kind.length === 2 ? kind.slice(1) : '');\n }\n return token;\n });\n}\n\nfunction createRegexReplacementMatcher(\n regex: Regex,\n expandReplacement: boolean,\n): ReplacementMatcher {\n return {\n testBuffer(buffer: Buffer): boolean {\n if (buffer.length > RE2_MAX_INPUT_BYTES) {\n throw new FsError(\n ErrorCode.TOO_LARGE,\n `File too large for a regex replace (${String(buffer.length)} > ${String(RE2_MAX_INPUT_BYTES)} bytes). Regex mode is used for isRegex, wholeWord, or the default caseSensitive: false; pass caseSensitive: true with a literal searchPattern, or split the file.`,\n );\n }\n // The regex is global and shared across every file in the batch, so a\n // previous file's match would otherwise start this scan mid-string.\n regex.lastIndex = 0;\n return regex.test(buffer.toString('utf-8'));\n },\n async replace(content, replacement, signal) {\n // Splicing by hand rather than through `String.replace(regex, …)`: RE2's\n // own replacer indexes the JS string with the code-point offsets it\n // reports, which lands the replacement short by one per astral character\n // (most emoji) earlier in the file. execMatches reports UTF-16 offsets.\n let matchCount = 0;\n let updated = '';\n let cursor = 0;\n const tick = createYielder(signal);\n for (const match of execMatches(regex, content)) {\n matchCount++;\n // Only isRegex=true opts into $1/$& substitution. A literal search\n // reaches this matcher too (case-insensitive and wholeWord both need a\n // regex), and there the replacement must be inserted verbatim.\n updated +=\n content.slice(cursor, match.start) +\n (expandReplacement\n ? expandDollarTokens(\n replacement,\n match.text,\n match.groups,\n match.start,\n content,\n match.named,\n )\n : replacement);\n cursor = match.end;\n if (await tick()) signal?.throwIfAborted();\n }\n return { content: updated + content.slice(cursor), matchCount };\n },\n dispose(): void {\n freeRegex(regex);\n },\n };\n}\n\nfunction createCaseSensitiveLiteralMatcher(searchPattern: string): ReplacementMatcher {\n const searchBuffer = Buffer.from(searchPattern, 'utf8');\n\n return {\n testBuffer(buffer: Buffer): boolean {\n return buffer.indexOf(searchBuffer) !== -1;\n },\n // One native O(n) pass; nothing to yield for.\n replace(content, replacement) {\n let matchCount = 0;\n const updated = content.replaceAll(searchPattern, () => {\n matchCount++;\n return replacement;\n });\n return Promise.resolve({ content: updated, matchCount });\n },\n dispose(): void {\n // no compiled pattern to release\n },\n };\n}\n\ntype SearchAndReplaceArgs = z.infer<typeof SearchAndReplaceInputSchema>;\n\ninterface ReplaceContext {\n dryRun: boolean;\n returnDiff: boolean;\n replacement: string;\n matcher: ReplacementMatcher;\n maxFileSize: number;\n signal: AbortSignal | undefined;\n summary: ReplaceSummary;\n fs: GuardedFileSystem;\n singleFile: boolean;\n}\n\ninterface ReplacementPlan {\n matchCount: number;\n originalContent: string;\n updatedContent: string;\n}\n\nasync function processEntry(entryPath: string, ctx: ReplaceContext): Promise<void> {\n const { dryRun, returnDiff, signal, summary } = ctx;\n // Discovery/read failures on a walked file are skipped silently; a failed\n // write is the caller's edit not landing, so it is always reported.\n const write = { started: false };\n\n try {\n // Lock the read→write per file so a concurrent writer is not lost; a dry\n // run never writes and skips the lock.\n const plan = dryRun\n ? await readReplacementPlan(entryPath, ctx)\n : await ctx.fs.lockForWrite(entryPath, async () => {\n const planned = await readReplacementPlan(entryPath, ctx);\n if (planned) {\n write.started = true;\n await ctx.fs.writeFile(entryPath, planned.updatedContent, { signal });\n }\n return planned;\n });\n if (!plan) {\n return;\n }\n\n // Bookkeep only after the write succeeds (or in dryRun, where there is no\n // write): a failed write must not count the file as changed.\n summary.totalMatches += plan.matchCount;\n summary.filesChanged++;\n\n recordChangedFile(summary, plan, entryPath);\n\n maybeAppendPatchDiff(summary, {\n filePath: entryPath,\n originalContent: plan.originalContent,\n updatedContent: plan.updatedContent,\n includeDiff: dryRun || returnDiff,\n });\n } catch (error) {\n // A timeout/cancel aborts the whole scan, not just this file: don't record\n // it as a per-file failure. processEntriesConcurrently reports the run as\n // stoppedReason 'timeout' instead.\n if (signal?.aborted) return;\n if (\n !ctx.singleFile &&\n !write.started &&\n ((isFsError(error) &&\n (error.code === ErrorCode.ACCESS_DENIED ||\n error.code === ErrorCode.NOT_FOUND ||\n error.code === ErrorCode.SYMLINK_NOT_ALLOWED)) ||\n isSkippableErrno(error))\n ) {\n summary.processedFiles--;\n return;\n }\n recordFailure(summary, entryPath, error);\n }\n}\n\nasync function readReplacementPlan(\n validPath: string,\n ctx: ReplaceContext,\n): Promise<ReplacementPlan | undefined> {\n const { matcher, replacement, maxFileSize, signal } = ctx;\n await using fileHandle = await ctx.fs.open(validPath);\n const stats = await fileHandle.stat();\n if (stats.size > maxFileSize) {\n throw createTooLargeError(stats.size, maxFileSize, validPath);\n }\n\n const buffer = await readFileBufferWithLimit(fileHandle, maxFileSize, validPath, signal);\n if (!matcher.testBuffer(buffer)) return undefined;\n // Only bytes that decode to UTF-8 losslessly survive the string round trip;\n // anything else would be written back with U+FFFD in place of every bad byte.\n // NUL is valid UTF-8 but marks a binary file, as it does for `read`.\n if (!isUtf8(buffer) || buffer.includes(0)) {\n if (ctx.singleFile) {\n throw new FsError(ErrorCode.INVALID_INPUT, 'Binary or non-UTF-8 file detected.', validPath);\n }\n ctx.summary.skippedBinary++;\n return undefined;\n }\n\n const originalContent = buffer.toString('utf-8');\n const { content: updatedContent, matchCount } = await matcher.replace(\n originalContent,\n replacement,\n signal,\n );\n return matchCount === 0 ? undefined : { matchCount, originalContent, updatedContent };\n}\n\nfunction maybeAppendPatchDiff(\n summary: ReplaceSummary,\n params: {\n filePath: string;\n originalContent: string;\n updatedContent: string;\n includeDiff: boolean;\n },\n): void {\n if (!params.includeDiff) return;\n\n // Budget first: a full budget must not pay for a diff it will discard, and a\n // diff past its deadline counts as truncated output, not a failed file.\n if (summary.diff.length >= MAX_DIFF_SIZE) {\n summary.diffTruncated = true;\n return;\n }\n const header = summary.paths.show(params.filePath);\n const patch = unifiedPatch(header, params.originalContent, params.updatedContent);\n if (patch === undefined) {\n summary.diffTruncated = true;\n return;\n }\n if (summary.diff.length + patch.length <= MAX_DIFF_SIZE + DIFF_APPEND_BUFFER) {\n summary.diff += patch;\n return;\n }\n summary.diffTruncated = true;\n}\n\ninterface ReplaceSummary {\n /** How changed files, failures and diff headers name a path: relative to the root. */\n paths: ResultPaths;\n totalMatches: number;\n filesChanged: number;\n failedFiles: number;\n processedFiles: number;\n skippedBinary: number;\n failures: Failure[];\n changedFiles: { path: string; matches: number }[];\n changedFilesTruncated: boolean;\n /** First file changed, as written. */\n primary?: { path: string; content: string };\n diff: string;\n diffTruncated: boolean;\n stoppedReason?: StoppedReason;\n}\n\nfunction createReplaceSummary(paths: ResultPaths): ReplaceSummary {\n return {\n paths,\n totalMatches: 0,\n filesChanged: 0,\n failedFiles: 0,\n processedFiles: 0,\n skippedBinary: 0,\n failures: [],\n changedFiles: [],\n changedFilesTruncated: false,\n diff: '',\n diffTruncated: false,\n };\n}\n\nfunction buildSearchPattern(args: SearchAndReplaceArgs): string {\n if (args.isRegex) {\n return args.wholeWord ? `\\\\b(?:${args.searchPattern})\\\\b` : args.searchPattern;\n }\n const escaped = escapeRegExp(args.searchPattern);\n return args.wholeWord ? `\\\\b(?:${escaped})\\\\b` : escaped;\n}\n\nfunction createReplacementMatcher(args: SearchAndReplaceArgs): ReplacementMatcher {\n // Use regex when isRegex, wholeWord, or case-insensitive (all require RE2)\n if (args.isRegex || args.wholeWord || !args.caseSensitive) {\n const regex = compileRegex(buildSearchPattern(args), { caseSensitive: args.caseSensitive });\n return createRegexReplacementMatcher(regex, args.isRegex);\n }\n return createCaseSensitiveLiteralMatcher(args.searchPattern);\n}\n\nfunction buildSearchAndReplaceStructuredResult(\n summary: ReplaceSummary,\n args: SearchAndReplaceArgs,\n): SearchAndReplaceOutput {\n const results = [\n ...summary.changedFiles.map((f) => ({ path: f.path, value: { matches: f.matches } })),\n ...summary.failures.map((f) => ({ path: f.path, error: f.error })),\n ];\n return {\n root: summary.paths.root,\n results,\n // `failedFiles` counts every failure; `failures` itself is capped at\n // MAX_FAILURES, so the summary must not be derived from the array length.\n summary: {\n total: summary.filesChanged + summary.failedFiles,\n succeeded: summary.filesChanged,\n failed: summary.failedFiles,\n },\n totalMatches: summary.totalMatches,\n filesScanned: summary.processedFiles,\n ...(summary.skippedBinary ? { skippedBinary: summary.skippedBinary } : {}),\n // Failures are capped at MAX_FAILURES independently of the changed-file\n // cap, so either cap can leave `results` shorter than `summary.total`.\n ...(summary.changedFilesTruncated || summary.failures.length < summary.failedFiles\n ? { resultsTruncated: true }\n : {}),\n ...((args.dryRun || args.returnDiff) && summary.diff ? { diff: summary.diff } : {}),\n ...(summary.diffTruncated ? { diffTruncated: true } : {}),\n ...(summary.stoppedReason ? { stoppedReason: summary.stoppedReason } : {}),\n };\n}\n\nasync function handleSearchAndReplace(\n args: SearchAndReplaceArgs,\n ctx: ToolCtx,\n): Promise<{\n structured: SearchAndReplaceOutput;\n link?: ContentBlock;\n}> {\n const maxFileSize = getMaxTextFileSize();\n const { basePath: root, explicitFile: singleFile } = await resolveSearchScope(args.path, ctx.fs);\n const effectivePattern = args.pattern ?? '**/*';\n const ceiling = ctx.fs.pathGuard.allowedRootContaining(root);\n\n // An explicit single-file target bypasses baseNameMatch/exclude/hidden/\n // gitignore filtering — it should always be processed as the one file named.\n const entries: AsyncIterable<{ path: string }> | Iterable<{ path: string }> = singleFile\n ? [{ path: singleFile }]\n : globEntries({\n cwd: root,\n pattern: effectivePattern,\n includeHidden: args.includeHidden,\n skipIgnored: !args.includeIgnored,\n ...(ceiling !== undefined ? { ignoreCeiling: ceiling } : {}),\n signal: ctx.signal,\n baseNameMatch: true,\n onlyFiles: true,\n suppressErrors: true,\n ...(args.maxDepth !== undefined ? { maxDepth: args.maxDepth } : {}),\n });\n\n // Relative to the root, not to the walked directory: `a.ts` from a run over\n // `src` would name `<root>/a.ts` when passed back.\n const summary = createReplaceSummary(ctx.fs.pathGuard.resultPaths(root));\n\n // The matcher may own a compiled RE2 pattern, whose wasm memory re2-wasm\n // never reclaims on its own. Nothing past the scan touches it, so free it the\n // moment the scan is done — success, failure, or abort alike.\n const matcher = createReplacementMatcher(args);\n let stoppedReason: StoppedReason | undefined;\n try {\n const context: ReplaceContext = {\n dryRun: args.dryRun,\n returnDiff: args.returnDiff,\n replacement: args.replacement,\n matcher,\n maxFileSize,\n signal: ctx.signal,\n summary,\n fs: ctx.fs,\n singleFile: singleFile !== undefined,\n };\n\n stoppedReason = await processEntriesConcurrently(entries, {\n signal: ctx.signal,\n concurrency: REPLACE_CONCURRENCY,\n ...(args.maxFiles !== undefined ? { maxEntries: args.maxFiles } : {}),\n shouldStop: () => summary.totalMatches >= args.maxResults,\n onEntry: () => {\n summary.processedFiles++;\n ctx.onProgress({ current: summary.processedFiles });\n },\n onError: (entryPath, err) => {\n recordFailure(summary, entryPath, err);\n },\n runEntry: (entryPath) => processEntry(entryPath, context),\n });\n } finally {\n matcher.dispose();\n }\n if (stoppedReason !== undefined) summary.stoppedReason = stoppedReason;\n // A single file that aborts mid-scan throws inside processEntry (caught and\n // swallowed as a timeout) and leaves the dispatch loop with no more entries,\n // so processEntriesConcurrently returns undefined; surface the timeout here so\n // the result is not reported as a clean \"0 matches\". search_text's engine\n // checks the signal directly, so only replace needs this fallback.\n else if (ctx.signal.aborted) summary.stoppedReason = 'timeout';\n\n ctx.onProgress({ current: summary.processedFiles });\n\n if (!args.dryRun && summary.totalMatches > 0) {\n ctx.log(\n 'info',\n `${summary.filesChanged} file(s) changed, ${summary.totalMatches} match(es)`,\n 'replace_text',\n );\n }\n\n const structured = buildSearchAndReplaceStructuredResult(summary, args);\n\n let primaryModified: string | undefined;\n if (summary.primary && !args.dryRun) {\n try {\n primaryModified = (\n await ctx.fs.stat(summary.primary.path, { signal: ctx.signal })\n ).stats.mtime.toISOString();\n } catch {\n /* result metadata only; the write already succeeded */\n }\n }\n const link = summary.primary\n ? buildWrittenFileMeta({\n validPath: summary.primary.path,\n content: summary.primary.content,\n dryRun: args.dryRun,\n lastModified: primaryModified,\n transport: ctx.transport,\n }).resourceLink\n : undefined;\n return link ? { structured, link } : { structured };\n}\n\nexport const REPLACE_TEXT = defineTool<typeof SearchAndReplaceInputSchema, SearchAndReplaceOutput>({\n name: 'replace_text',\n title: 'Search and Replace',\n description:\n 'Find and replace every occurrence of literal text or an RE2 regex, like sed -i, in one file or all files ' +\n 'under a directory, optionally filtered by glob. The pattern runs over the whole file: it can span lines, ' +\n 'and ^ and $ anchor to file start and end. edit replaces one unique exact match.',\n input: SearchAndReplaceInputSchema,\n readOnlyHint: false,\n deadline: true,\n progress: (args) => {\n const dryLabel = args.dryRun ? ' [dry run]' : '';\n return {\n label: `Replace${dryLabel}`,\n subject: `${truncateProgressPattern(args.searchPattern)} → ${truncateProgressPattern(args.replacement)}`,\n };\n },\n mapPaths: mapOptionalPath,\n run: async (args, ctx) => {\n const truncatedPattern = truncateProgressPattern(args.searchPattern);\n const { structured, link } = await handleSearchAndReplace(args, ctx);\n const dryLabel = args.dryRun ? ' [dry run]' : '';\n const summaryText =\n `replace_text: '${truncatedPattern}'${dryLabel}` +\n ` \\u00b7 ${String(structured.totalMatches)} match(es)` +\n ` in ${String(structured.summary.succeeded)} file(s)` +\n (structured.summary.failed > 0 ? ` \\u00b7 ${String(structured.summary.failed)} failed` : '') +\n (structured.skippedBinary\n ? ` \\u00b7 ${String(structured.skippedBinary)} binary skipped`\n : '');\n // The structured half ships under `_meta`, which clients do not show the\n // model, so the stop state and the diff preview must ride the text.\n const lines = [summaryText];\n const stop = structured.stoppedReason;\n if (stop === 'timeout') {\n lines.push(stoppedEarlyLine(stop));\n // A live run writes as it scans, so a timeout leaves the files it reached\n // rewritten. The paths ship under `_meta`, which the model does not see; name\n // them here, or a re-run of a self-matching replacement applies it twice.\n if (!args.dryRun && structured.summary.succeeded > 0) {\n const total = structured.summary.succeeded;\n const shown = structured.results\n .filter((x) => !('error' in x))\n .slice(0, 5)\n .map((x) => x.path)\n .join(', ');\n const more = total > 5 ? `, and ${String(total - 5)} more` : '';\n lines.push(\n `// ${String(total)} file(s) were already rewritten and stay changed: ${shown}${more}. ` +\n 'Re-running the same replacement applies it to them again; exclude them, or use dryRun first.',\n );\n }\n } else if (stop !== undefined) {\n // maxResults/maxFiles here are the caller's own caps, so raising one is\n // what reaches the rest; the search engine's advice to narrow is not.\n lines.push(\n `// scan stopped early: hit the ${stop} limit; later files were not scanned. A higher ${stop} reaches them.`,\n );\n }\n const hint =\n structured.totalMatches === 0\n ? zeroMatchHint({\n ...args,\n filesScanned: structured.filesScanned,\n // A failed file, or a binary one the needle did hit, explains the\n // zero better than a guess about the pattern.\n incomplete:\n stop !== undefined ||\n structured.summary.failed > 0 ||\n Boolean(structured.skippedBinary),\n })\n : undefined;\n if (hint) lines.push(hint);\n // A failure's reason lives in `results`, which ships under `_meta`. The\n // full relative path, not the basename: the error message may not carry\n // the path, and two same-named files in different directories must not\n // render as the same row.\n for (const r of structured.results.filter((x) => 'error' in x).slice(0, 3)) {\n lines.push(`// ${r.path}: ${r.error.code} ${r.error.message}`);\n }\n if (structured.diffTruncated) {\n lines.push(\n `// diff cut at ${String(MAX_DIFF_SIZE / 1024)} KB: some files' changes are not shown.`,\n );\n }\n const text = lines.join('\\n') + (structured.diff ? `\\n\\n${structured.diff}` : '');\n return {\n structured,\n text,\n resources: link ? [link] : [],\n isError: isTotalFailure(structured.summary),\n };\n },\n});\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"search-text.d.ts","sourceRoot":"","sources":["../../src/tools/search-text.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"search-text.d.ts","sourceRoot":"","sources":["../../src/tools/search-text.ts"],"names":[],"mappings":"AAgLA,eAAO,MAAM,WAAW,mCA6GtB,CAAC"}
|
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
import { stat } from 'node:fs/promises';
|
|
2
|
-
import { dirname } from 'node:path';
|
|
3
1
|
import * as z from 'zod/v4';
|
|
4
|
-
import { paginate } from "../core/cursor.js";
|
|
5
|
-
import { ErrorCode, FsError } from "../core/errors.js";
|
|
2
|
+
import { pageQueryKey, paginate } from "../core/cursor.js";
|
|
6
3
|
import { formatCount, pageTrailer, truncateProgressPattern, zeroMatchHint } from "../core/fmt.js";
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { searchContent } from "../core/search.js";
|
|
4
|
+
import { CursorSchema, defaultFalseBoolean, IncludeHidden, IncludeIgnored, mapOptionalPath, MaxDepth, nonBlankString, OptionalPath, SafeGlobPattern, } from "../core/schema.js";
|
|
5
|
+
import { resolveSearchScope, searchContent } from "../core/search.js";
|
|
10
6
|
import { putJsonResource } from "../core/store.js";
|
|
11
|
-
import { DEFAULT_SEARCH_CONTENT_RESULTS,
|
|
7
|
+
import { DEFAULT_SEARCH_CONTENT_RESULTS, MAX_SEARCH_RESULTS } from "../core/util.js";
|
|
12
8
|
import { defineTool } from "./define.js";
|
|
13
9
|
const GrepInputSchema = z.strictObject({
|
|
14
10
|
path: OptionalPath.describe('File to search, or directory to search under (default: the whole first allowed root). Naming a file searches that file alone: pattern is ignored and hidden/ignored filtering does not apply.'),
|
|
@@ -88,6 +84,7 @@ function buildSearchMatchDetail(totalMatches, filesMatched) {
|
|
|
88
84
|
}
|
|
89
85
|
function searchContentOutput(matches, metadata, nextCursor, resourceUri) {
|
|
90
86
|
return {
|
|
87
|
+
root: metadata.root,
|
|
91
88
|
matches: [...matches],
|
|
92
89
|
totalMatches: metadata.matchingLines,
|
|
93
90
|
filesScanned: metadata.filesScanned,
|
|
@@ -101,13 +98,13 @@ function searchContentOutput(matches, metadata, nextCursor, resourceUri) {
|
|
|
101
98
|
...(nextCursor !== undefined ? { nextCursor } : {}),
|
|
102
99
|
};
|
|
103
100
|
}
|
|
104
|
-
function buildSortedPayloads(result) {
|
|
101
|
+
function buildSortedPayloads(result, paths) {
|
|
105
102
|
const relativeByFile = new Map();
|
|
106
103
|
const getRelativeFile = (file) => {
|
|
107
104
|
const cached = relativeByFile.get(file);
|
|
108
105
|
if (cached !== undefined)
|
|
109
106
|
return cached;
|
|
110
|
-
const rel =
|
|
107
|
+
const rel = paths.show(file);
|
|
111
108
|
relativeByFile.set(file, rel);
|
|
112
109
|
return rel;
|
|
113
110
|
};
|
|
@@ -123,34 +120,14 @@ function buildSortedPayloads(result) {
|
|
|
123
120
|
payloads.sort((l, r) => l.file.localeCompare(r.file) || l.line - r.line);
|
|
124
121
|
return payloads;
|
|
125
122
|
}
|
|
126
|
-
/** Explicit files bypass glob filtering; their parent anchors the relative result paths. */
|
|
127
|
-
async function resolveSearchScope(path, ctx) {
|
|
128
|
-
const requested = ctx.fs.pathGuard.resolvePathOrRoot(path);
|
|
129
|
-
// One resolution, one stat: validateExistingDirectory would redo both.
|
|
130
|
-
const resolved = await ctx.fs.pathGuard.validateExistingPath(requested);
|
|
131
|
-
const stats = await stat(resolved);
|
|
132
|
-
if (!stats.isFile()) {
|
|
133
|
-
if (!stats.isDirectory()) {
|
|
134
|
-
throw new FsError(ErrorCode.NOT_DIRECTORY, 'Not a directory', requested);
|
|
135
|
-
}
|
|
136
|
-
return { basePath: resolved };
|
|
137
|
-
}
|
|
138
|
-
return {
|
|
139
|
-
basePath: dirname(resolved),
|
|
140
|
-
// The name is NOT turned into a glob: `[slug].tsx` would read as a
|
|
141
|
-
// character class. searchContent scans exactly this file instead, and
|
|
142
|
-
// hidden/ignored filtering is moot for a file the caller named.
|
|
143
|
-
explicitFile: resolved,
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
123
|
export const SEARCH_TEXT = defineTool({
|
|
147
124
|
name: 'search_text',
|
|
148
125
|
title: 'Search Content',
|
|
149
126
|
description: 'Search file contents for literal text or an RE2 regex, like grep; returns matching lines with line numbers ' +
|
|
150
|
-
'and paths relative to the
|
|
127
|
+
'and paths relative to the root, ready to pass to read or edit. Patterns match within one line. find_files matches file names.',
|
|
151
128
|
input: GrepInputSchema,
|
|
152
129
|
readOnlyHint: true,
|
|
153
|
-
|
|
130
|
+
deadline: true,
|
|
154
131
|
progress: (args) => ({
|
|
155
132
|
label: 'Search',
|
|
156
133
|
subject: truncateProgressPattern(args.searchPattern),
|
|
@@ -158,28 +135,19 @@ export const SEARCH_TEXT = defineTool({
|
|
|
158
135
|
progressDone: (_args, result) => ({
|
|
159
136
|
detail: buildSearchMatchDetail(result.totalMatches ?? 0, result.filesMatched ?? 0),
|
|
160
137
|
}),
|
|
161
|
-
|
|
138
|
+
mapPaths: mapOptionalPath,
|
|
162
139
|
run: async (args, ctx) => {
|
|
163
140
|
const requestedPath = ctx.fs.pathGuard.resolvePathOrRoot(args.path);
|
|
164
|
-
const queryKey = JSON.stringify({
|
|
165
|
-
method: 'search_text',
|
|
166
|
-
path: requestedPath,
|
|
167
|
-
pattern: args.pattern,
|
|
168
|
-
searchPattern: args.searchPattern,
|
|
169
|
-
isRegex: args.isRegex,
|
|
170
|
-
includeHidden: args.includeHidden,
|
|
171
|
-
includeIgnored: args.includeIgnored,
|
|
172
|
-
caseSensitive: args.caseSensitive,
|
|
173
|
-
maxDepth: args.maxDepth,
|
|
174
|
-
context: args.context,
|
|
175
|
-
});
|
|
176
141
|
const paged = await paginate({
|
|
177
142
|
store: ctx.pageStore,
|
|
178
|
-
queryKey,
|
|
143
|
+
queryKey: pageQueryKey('search_text', { ...args, path: requestedPath }, [
|
|
144
|
+
'cursor',
|
|
145
|
+
'maxResults',
|
|
146
|
+
]),
|
|
179
147
|
cursor: args.cursor,
|
|
180
148
|
pageSize: args.maxResults,
|
|
181
149
|
produce: async () => {
|
|
182
|
-
const { basePath, explicitFile } = await resolveSearchScope(args.path, ctx);
|
|
150
|
+
const { basePath, explicitFile } = await resolveSearchScope(args.path, ctx.fs, ctx.signal);
|
|
183
151
|
const result = await searchContent(basePath, args.searchPattern, {
|
|
184
152
|
includeHidden: args.includeHidden,
|
|
185
153
|
filePattern: args.pattern ?? '**/*',
|
|
@@ -192,9 +160,12 @@ export const SEARCH_TEXT = defineTool({
|
|
|
192
160
|
...(args.maxDepth !== undefined ? { maxDepth: args.maxDepth } : {}),
|
|
193
161
|
signal: ctx.signal,
|
|
194
162
|
}, ctx.fs.pathGuard);
|
|
163
|
+
// Relative to the root, not to basePath: `a.ts` from a search of
|
|
164
|
+
// `src` would name `<root>/a.ts` when passed back.
|
|
165
|
+
const paths = ctx.fs.pathGuard.resultPaths(basePath);
|
|
195
166
|
return {
|
|
196
|
-
items: buildSortedPayloads(result),
|
|
197
|
-
metadata: result.summary,
|
|
167
|
+
items: buildSortedPayloads(result, paths),
|
|
168
|
+
metadata: { ...result.summary, root: paths.root },
|
|
198
169
|
truncated: result.summary.truncated,
|
|
199
170
|
};
|
|
200
171
|
},
|