@causyne/agent 1.20.91 → 1.20.94
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/package.json +1 -1
- package/src/agentLoop.js +116 -26
- package/src/agentLoop.ts +121 -26
- package/src/contextManager.js +2 -0
- package/src/decideTools.js +9 -0
- package/src/decideTools.ts +9 -0
- package/src/decisionPolicy.js +119 -1
- package/src/decisionPolicy.ts +129 -1
- package/src/historyPruner.js +16 -1
- package/src/historyPruner.ts +13 -1
- package/src/sandbox.js +33 -7
- package/src/skills/skillResourceSafety.js +70 -0
- package/src/skills/skillResourceSafety.ts +77 -0
- package/src/smartToolsUse.js +28 -11
- package/src/smartToolsUse.ts +34 -11
- package/src/taskShape.js +28 -13
- package/src/taskShape.ts +31 -12
- package/src/tools/codeSearcher.js +349 -210
- package/src/tools/codeSearcher.ts +408 -0
- package/src/tools/codebaseSearch.js +182 -68
- package/src/tools/codebaseSearch.ts +221 -0
- package/src/tools/deferredMcpSearch.js +94 -40
- package/src/tools/deferredMcpSearch.ts +89 -30
- package/src/tools/fsFind.js +180 -115
- package/src/tools/fsFind.ts +228 -0
- package/src/tools/fsList.js +149 -69
- package/src/tools/fsList.ts +224 -0
- package/src/tools/fsSearch.js +192 -189
- package/src/tools/fsSearch.ts +241 -0
- package/src/tools/readNativeSkill.js +34 -42
- package/src/tools/readNativeSkill.ts +38 -36
- package/src/tools/searchRelevance.js +70 -0
- package/src/tools/searchRelevance.ts +72 -0
- package/src/tools/toolSelfRepair.js +56 -20
- package/src/tools/toolSelfRepair.ts +58 -19
- package/src/tools/walkerSafety.js +445 -0
- package/src/tools/walkerSafety.ts +516 -0
- package/src/tui/composerGeometry.js +45 -0
- package/src/tui/composerGeometry.ts +80 -0
- package/src/tui/homeView.js +10 -6
- package/src/tui/homeView.ts +10 -6
- package/src/tui/lineEditor.js +8 -0
- package/src/tui/prompt.js +9 -4
- package/src/userSkills.js +183 -203
- package/src/userSkills.ts +301 -0
- package/src/userSkills.d.ts +0 -26
package/src/tools/fsFind.js
CHANGED
|
@@ -1,128 +1,193 @@
|
|
|
1
|
-
import fs from 'fs/promises';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { checkSearchScope, createTraversalGuard, isSafeSymlinkTarget, resolveSearchRoot, shouldExcludeChild } from './walkerSafety.js';
|
|
4
|
+
const MAX_MATCHES = 50;
|
|
5
|
+
const MAX_FUZZY_LENGTH = 128;
|
|
6
6
|
function levenshteinDistance(a, b) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
for (let j =
|
|
16
|
-
|
|
17
|
-
matrix[i][j] = matrix[i - 1][j - 1];
|
|
18
|
-
} else {
|
|
19
|
-
matrix[i][j] = Math.min(
|
|
20
|
-
matrix[i - 1][j - 1] + 1, // substitution
|
|
21
|
-
matrix[i][j - 1] + 1, // insertion
|
|
22
|
-
matrix[i - 1][j] + 1 // deletion
|
|
23
|
-
);
|
|
24
|
-
}
|
|
7
|
+
const m = a.length;
|
|
8
|
+
const n = b.length;
|
|
9
|
+
if (m === 0)
|
|
10
|
+
return n;
|
|
11
|
+
if (n === 0)
|
|
12
|
+
return m;
|
|
13
|
+
let prev = new Uint16Array(m + 1);
|
|
14
|
+
let curr = new Uint16Array(m + 1);
|
|
15
|
+
for (let j = 0; j <= m; j++) {
|
|
16
|
+
prev[j] = j;
|
|
25
17
|
}
|
|
26
|
-
|
|
27
|
-
|
|
18
|
+
for (let i = 1; i <= n; i++) {
|
|
19
|
+
curr[0] = i;
|
|
20
|
+
const bi = b.charCodeAt(i - 1);
|
|
21
|
+
for (let j = 1; j <= m; j++) {
|
|
22
|
+
const cost = a.charCodeAt(j - 1) === bi ? 0 : 1;
|
|
23
|
+
const del = prev[j] + 1;
|
|
24
|
+
const ins = curr[j - 1] + 1;
|
|
25
|
+
const sub = prev[j - 1] + cost;
|
|
26
|
+
curr[j] = Math.min(del, ins, sub);
|
|
27
|
+
}
|
|
28
|
+
const tmp = prev;
|
|
29
|
+
prev = curr;
|
|
30
|
+
curr = tmp;
|
|
31
|
+
}
|
|
32
|
+
return prev[m];
|
|
28
33
|
}
|
|
29
|
-
|
|
30
34
|
function isMatch(filename, keywords, extensions, fuzzyMatch) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (!keywords || keywords.length === 0) return true;
|
|
39
|
-
|
|
40
|
-
for (const kw of keywords) {
|
|
41
|
-
const lowerKw = kw.toLowerCase();
|
|
42
|
-
if (lowerFilename.includes(lowerKw)) return true;
|
|
43
|
-
|
|
44
|
-
// Fuzzy matching for typos (only applied if keyword is >= 4 chars)
|
|
45
|
-
if (fuzzyMatch && lowerKw.length >= 4) {
|
|
46
|
-
for (let i = 0; i <= lowerFilename.length - lowerKw.length + 1; i++) {
|
|
47
|
-
const chunk = lowerFilename.substr(i, lowerKw.length + 1);
|
|
48
|
-
const distance = levenshteinDistance(chunk, lowerKw);
|
|
49
|
-
const maxDistance = Math.floor(lowerKw.length / 4);
|
|
50
|
-
if (distance <= maxDistance) return true;
|
|
51
|
-
}
|
|
35
|
+
const lowerFilename = filename.toLowerCase();
|
|
36
|
+
if (extensions.length > 0) {
|
|
37
|
+
const extMatch = extensions.some((ext) => lowerFilename.endsWith(String(ext).toLowerCase()));
|
|
38
|
+
if (!extMatch)
|
|
39
|
+
return false;
|
|
52
40
|
}
|
|
53
|
-
|
|
54
|
-
|
|
41
|
+
if (keywords.length === 0)
|
|
42
|
+
return true;
|
|
43
|
+
for (const kw of keywords) {
|
|
44
|
+
const lowerKw = String(kw).toLowerCase();
|
|
45
|
+
if (!lowerKw)
|
|
46
|
+
continue;
|
|
47
|
+
if (lowerFilename.includes(lowerKw))
|
|
48
|
+
return true;
|
|
49
|
+
if (fuzzyMatch &&
|
|
50
|
+
lowerKw.length >= 4 &&
|
|
51
|
+
lowerKw.length <= MAX_FUZZY_LENGTH &&
|
|
52
|
+
lowerFilename.length <= MAX_FUZZY_LENGTH) {
|
|
53
|
+
const maxDistance = Math.floor(lowerKw.length / 4);
|
|
54
|
+
const winLen = lowerKw.length + 1;
|
|
55
|
+
for (let i = 0; i <= lowerFilename.length - lowerKw.length + 1; i++) {
|
|
56
|
+
const chunk = lowerFilename.slice(i, i + winLen);
|
|
57
|
+
if (levenshteinDistance(chunk, lowerKw) <= maxDistance)
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
55
63
|
}
|
|
56
|
-
|
|
57
64
|
export const fsFind = {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
},
|
|
71
|
-
|
|
72
|
-
async execute(args) {
|
|
73
|
-
try {
|
|
74
|
-
const targetDir = (args.path || process.cwd()).replace(/^~\//, os.homedir() + '/');
|
|
75
|
-
const keywords = args.keywords || [];
|
|
76
|
-
const extensions = args.extensions || [];
|
|
77
|
-
const fuzzyMatch = args.fuzzyMatch === true;
|
|
78
|
-
|
|
79
|
-
const EXCLUDE_DIRS = new Set(['node_modules', '.git', '.gradle', 'build', 'dist', '.idea', '.causyne_trash']);
|
|
80
|
-
|
|
81
|
-
const matches = [];
|
|
82
|
-
let scanned = 0;
|
|
83
|
-
const MAX_MATCHES = 50;
|
|
84
|
-
|
|
85
|
-
// Phase 6 AGI Upgrade: Asynchronous Non-Blocking Traversal to prevent Event Loop freezing
|
|
86
|
-
async function walkAsync(dir) {
|
|
87
|
-
if (matches.length >= MAX_MATCHES) return;
|
|
88
|
-
|
|
89
|
-
let dirents;
|
|
90
|
-
try {
|
|
91
|
-
dirents = await fs.readdir(dir, { withFileTypes: true });
|
|
92
|
-
} catch (err) {
|
|
93
|
-
return; // Ignore permission/missing errors gracefully
|
|
65
|
+
schema: {
|
|
66
|
+
name: 'fs.find',
|
|
67
|
+
description: 'Fast file search by filename keywords and extensions with optional typo tolerance and bounded traversal.',
|
|
68
|
+
parameters: {
|
|
69
|
+
type: 'object',
|
|
70
|
+
properties: {
|
|
71
|
+
path: { type: 'string', description: 'Root directory to start the search. Defaults to workspace root.' },
|
|
72
|
+
keywords: { type: 'array', items: { type: 'string' } },
|
|
73
|
+
extensions: { type: 'array', items: { type: 'string' } },
|
|
74
|
+
fuzzyMatch: { type: 'boolean', description: 'Set to true to enable typo tolerance.' }
|
|
75
|
+
}
|
|
94
76
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
77
|
+
},
|
|
78
|
+
async execute(args = {}, context) {
|
|
79
|
+
try {
|
|
80
|
+
const targetDir = resolveSearchRoot(args?.path, context);
|
|
81
|
+
const scope = checkSearchScope(targetDir, context);
|
|
82
|
+
if (!scope.allowed || !scope.realPath) {
|
|
83
|
+
return { success: false, error: scope.error || `Search path not allowed: ${targetDir}` };
|
|
84
|
+
}
|
|
85
|
+
const rootReal = scope.realPath;
|
|
86
|
+
let stat;
|
|
87
|
+
try {
|
|
88
|
+
stat = await fs.stat(rootReal);
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return { success: false, error: `Search directory does not exist: ${targetDir}` };
|
|
103
92
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
if (isMatch(dirent.name, keywords, extensions, fuzzyMatch)) {
|
|
107
|
-
matches.push(path.join(dir, dirent.name));
|
|
93
|
+
if (!stat.isDirectory()) {
|
|
94
|
+
return { success: false, error: `Search path is not a directory: ${targetDir}` };
|
|
108
95
|
}
|
|
109
|
-
|
|
96
|
+
const keywords = Array.isArray(args?.keywords) ? args.keywords : [];
|
|
97
|
+
const extensions = Array.isArray(args?.extensions) ? args.extensions : [];
|
|
98
|
+
const fuzzyMatch = args?.fuzzyMatch === true;
|
|
99
|
+
const maxDepth = typeof args?.maxDepth === 'number' && Number.isInteger(args.maxDepth) && args.maxDepth > 0
|
|
100
|
+
? Math.min(args.maxDepth, 24)
|
|
101
|
+
: undefined;
|
|
102
|
+
const guard = createTraversalGuard({ maxFiles: 30_000, maxDepth, signal: context?.signal });
|
|
103
|
+
const seen = new Set();
|
|
104
|
+
const matches = [];
|
|
105
|
+
let scanned = 0;
|
|
106
|
+
const queue = [
|
|
107
|
+
{ dir: rootReal, real: rootReal, depth: 0 }
|
|
108
|
+
];
|
|
109
|
+
while (queue.length > 0 && matches.length < MAX_MATCHES) {
|
|
110
|
+
if (guard.checkStopped())
|
|
111
|
+
break;
|
|
112
|
+
const current = queue.shift();
|
|
113
|
+
if (seen.has(current.real))
|
|
114
|
+
continue;
|
|
115
|
+
if (!guard.enterDir(current.depth))
|
|
116
|
+
continue;
|
|
117
|
+
seen.add(current.real);
|
|
118
|
+
let dirents;
|
|
119
|
+
try {
|
|
120
|
+
dirents = await fs.readdir(current.dir, { withFileTypes: true });
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
for (const dirent of dirents) {
|
|
126
|
+
if (matches.length >= MAX_MATCHES || guard.checkStopped())
|
|
127
|
+
break;
|
|
128
|
+
if (shouldExcludeChild(current.real, dirent.name))
|
|
129
|
+
continue;
|
|
130
|
+
const full = path.join(current.dir, dirent.name);
|
|
131
|
+
if (dirent.isDirectory()) {
|
|
132
|
+
try {
|
|
133
|
+
const childReal = await fs.realpath(full);
|
|
134
|
+
if (isSafeSymlinkTarget(childReal, rootReal, context) && !seen.has(childReal)) {
|
|
135
|
+
queue.push({ dir: full, real: childReal, depth: current.depth + 1 });
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
// Ignore unreadable directories.
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
else if (dirent.isFile()) {
|
|
143
|
+
if (!guard.countFile())
|
|
144
|
+
break;
|
|
145
|
+
scanned++;
|
|
146
|
+
if (isMatch(dirent.name, keywords, extensions, fuzzyMatch)) {
|
|
147
|
+
matches.push(full);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
else if (dirent.isSymbolicLink()) {
|
|
151
|
+
try {
|
|
152
|
+
const targetReal = await fs.realpath(full);
|
|
153
|
+
if (!isSafeSymlinkTarget(targetReal, rootReal, context))
|
|
154
|
+
continue;
|
|
155
|
+
const targetStat = await fs.stat(targetReal);
|
|
156
|
+
if (targetStat.isDirectory()) {
|
|
157
|
+
if (!seen.has(targetReal)) {
|
|
158
|
+
queue.push({ dir: targetReal, real: targetReal, depth: current.depth + 1 });
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
else if (targetStat.isFile()) {
|
|
162
|
+
if (!guard.countFile())
|
|
163
|
+
break;
|
|
164
|
+
scanned++;
|
|
165
|
+
if (isMatch(dirent.name, keywords, extensions, fuzzyMatch)) {
|
|
166
|
+
matches.push(full);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
// Ignore broken symlinks.
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (guard.stopReason === 'aborted') {
|
|
177
|
+
return { success: false, aborted: true, error: 'File find aborted.' };
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
success: true,
|
|
181
|
+
output: JSON.stringify({
|
|
182
|
+
scanned_files: scanned,
|
|
183
|
+
total_matches: matches.length,
|
|
184
|
+
...(guard.stopReason ? { stopped_reason: guard.stopNotice() } : {}),
|
|
185
|
+
matches: matches.length > 0 ? matches : 'No files matched your search parameters.'
|
|
186
|
+
}, null, 2)
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
catch (error) {
|
|
190
|
+
return { success: false, error: error instanceof Error ? error.message : String(error) };
|
|
110
191
|
}
|
|
111
|
-
await Promise.all(promises);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
await walkAsync(targetDir);
|
|
115
|
-
|
|
116
|
-
return {
|
|
117
|
-
success: true,
|
|
118
|
-
output: JSON.stringify({
|
|
119
|
-
scanned_files: scanned,
|
|
120
|
-
total_matches: matches.length,
|
|
121
|
-
matches: matches.length > 0 ? matches : "No files matched your semantic parameters."
|
|
122
|
-
}, null, 2)
|
|
123
|
-
};
|
|
124
|
-
} catch (e) {
|
|
125
|
-
return { success: false, error: e.message };
|
|
126
192
|
}
|
|
127
|
-
}
|
|
128
193
|
};
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import {
|
|
4
|
+
checkSearchScope,
|
|
5
|
+
createTraversalGuard,
|
|
6
|
+
isSafeSymlinkTarget,
|
|
7
|
+
resolveSearchRoot,
|
|
8
|
+
shouldExcludeChild,
|
|
9
|
+
type SearchExecutionContext
|
|
10
|
+
} from './walkerSafety.js';
|
|
11
|
+
|
|
12
|
+
const MAX_MATCHES = 50;
|
|
13
|
+
const MAX_FUZZY_LENGTH = 128;
|
|
14
|
+
|
|
15
|
+
export interface FsFindArgs {
|
|
16
|
+
path?: string | undefined;
|
|
17
|
+
keywords?: string[] | undefined;
|
|
18
|
+
extensions?: string[] | undefined;
|
|
19
|
+
fuzzyMatch?: boolean | undefined;
|
|
20
|
+
maxDepth?: number | undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface FsFindResult {
|
|
24
|
+
success: boolean;
|
|
25
|
+
output?: string;
|
|
26
|
+
error?: string;
|
|
27
|
+
aborted?: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function levenshteinDistance(a: string, b: string): number {
|
|
31
|
+
const m = a.length;
|
|
32
|
+
const n = b.length;
|
|
33
|
+
if (m === 0) return n;
|
|
34
|
+
if (n === 0) return m;
|
|
35
|
+
|
|
36
|
+
let prev = new Uint16Array(m + 1);
|
|
37
|
+
let curr = new Uint16Array(m + 1);
|
|
38
|
+
for (let j = 0; j <= m; j++) {
|
|
39
|
+
prev[j] = j;
|
|
40
|
+
}
|
|
41
|
+
for (let i = 1; i <= n; i++) {
|
|
42
|
+
curr[0] = i;
|
|
43
|
+
const bi = b.charCodeAt(i - 1);
|
|
44
|
+
for (let j = 1; j <= m; j++) {
|
|
45
|
+
const cost = a.charCodeAt(j - 1) === bi ? 0 : 1;
|
|
46
|
+
const del = prev[j]! + 1;
|
|
47
|
+
const ins = curr[j - 1]! + 1;
|
|
48
|
+
const sub = prev[j - 1]! + cost;
|
|
49
|
+
curr[j] = Math.min(del, ins, sub);
|
|
50
|
+
}
|
|
51
|
+
const tmp = prev;
|
|
52
|
+
prev = curr;
|
|
53
|
+
curr = tmp;
|
|
54
|
+
}
|
|
55
|
+
return prev[m]!;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function isMatch(
|
|
59
|
+
filename: string,
|
|
60
|
+
keywords: readonly string[],
|
|
61
|
+
extensions: readonly string[],
|
|
62
|
+
fuzzyMatch: boolean
|
|
63
|
+
): boolean {
|
|
64
|
+
const lowerFilename = filename.toLowerCase();
|
|
65
|
+
|
|
66
|
+
if (extensions.length > 0) {
|
|
67
|
+
const extMatch = extensions.some((ext) => lowerFilename.endsWith(String(ext).toLowerCase()));
|
|
68
|
+
if (!extMatch) return false;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (keywords.length === 0) return true;
|
|
72
|
+
|
|
73
|
+
for (const kw of keywords) {
|
|
74
|
+
const lowerKw = String(kw).toLowerCase();
|
|
75
|
+
if (!lowerKw) continue;
|
|
76
|
+
if (lowerFilename.includes(lowerKw)) return true;
|
|
77
|
+
|
|
78
|
+
if (
|
|
79
|
+
fuzzyMatch &&
|
|
80
|
+
lowerKw.length >= 4 &&
|
|
81
|
+
lowerKw.length <= MAX_FUZZY_LENGTH &&
|
|
82
|
+
lowerFilename.length <= MAX_FUZZY_LENGTH
|
|
83
|
+
) {
|
|
84
|
+
const maxDistance = Math.floor(lowerKw.length / 4);
|
|
85
|
+
const winLen = lowerKw.length + 1;
|
|
86
|
+
for (let i = 0; i <= lowerFilename.length - lowerKw.length + 1; i++) {
|
|
87
|
+
const chunk = lowerFilename.slice(i, i + winLen);
|
|
88
|
+
if (levenshteinDistance(chunk, lowerKw) <= maxDistance) return true;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export const fsFind = {
|
|
96
|
+
schema: {
|
|
97
|
+
name: 'fs.find',
|
|
98
|
+
description:
|
|
99
|
+
'Fast file search by filename keywords and extensions with optional typo tolerance and bounded traversal.',
|
|
100
|
+
parameters: {
|
|
101
|
+
type: 'object',
|
|
102
|
+
properties: {
|
|
103
|
+
path: { type: 'string', description: 'Root directory to start the search. Defaults to workspace root.' },
|
|
104
|
+
keywords: { type: 'array', items: { type: 'string' } },
|
|
105
|
+
extensions: { type: 'array', items: { type: 'string' } },
|
|
106
|
+
fuzzyMatch: { type: 'boolean', description: 'Set to true to enable typo tolerance.' }
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
|
|
111
|
+
async execute(
|
|
112
|
+
args: FsFindArgs = {},
|
|
113
|
+
context?: SearchExecutionContext
|
|
114
|
+
): Promise<FsFindResult> {
|
|
115
|
+
try {
|
|
116
|
+
const targetDir = resolveSearchRoot(args?.path, context);
|
|
117
|
+
const scope = checkSearchScope(targetDir, context);
|
|
118
|
+
if (!scope.allowed || !scope.realPath) {
|
|
119
|
+
return { success: false, error: scope.error || `Search path not allowed: ${targetDir}` };
|
|
120
|
+
}
|
|
121
|
+
const rootReal = scope.realPath;
|
|
122
|
+
|
|
123
|
+
let stat;
|
|
124
|
+
try {
|
|
125
|
+
stat = await fs.stat(rootReal);
|
|
126
|
+
} catch {
|
|
127
|
+
return { success: false, error: `Search directory does not exist: ${targetDir}` };
|
|
128
|
+
}
|
|
129
|
+
if (!stat.isDirectory()) {
|
|
130
|
+
return { success: false, error: `Search path is not a directory: ${targetDir}` };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const keywords = Array.isArray(args?.keywords) ? args.keywords : [];
|
|
134
|
+
const extensions = Array.isArray(args?.extensions) ? args.extensions : [];
|
|
135
|
+
const fuzzyMatch = args?.fuzzyMatch === true;
|
|
136
|
+
const maxDepth =
|
|
137
|
+
typeof args?.maxDepth === 'number' && Number.isInteger(args.maxDepth) && args.maxDepth > 0
|
|
138
|
+
? Math.min(args.maxDepth, 24)
|
|
139
|
+
: undefined;
|
|
140
|
+
|
|
141
|
+
const guard = createTraversalGuard({ maxFiles: 30_000, maxDepth, signal: context?.signal });
|
|
142
|
+
const seen = new Set<string>();
|
|
143
|
+
const matches: string[] = [];
|
|
144
|
+
let scanned = 0;
|
|
145
|
+
|
|
146
|
+
const queue: Array<{ dir: string; real: string; depth: number }> = [
|
|
147
|
+
{ dir: rootReal, real: rootReal, depth: 0 }
|
|
148
|
+
];
|
|
149
|
+
|
|
150
|
+
while (queue.length > 0 && matches.length < MAX_MATCHES) {
|
|
151
|
+
if (guard.checkStopped()) break;
|
|
152
|
+
const current = queue.shift()!;
|
|
153
|
+
if (seen.has(current.real)) continue;
|
|
154
|
+
if (!guard.enterDir(current.depth)) continue;
|
|
155
|
+
seen.add(current.real);
|
|
156
|
+
|
|
157
|
+
let dirents;
|
|
158
|
+
try {
|
|
159
|
+
dirents = await fs.readdir(current.dir, { withFileTypes: true });
|
|
160
|
+
} catch {
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
for (const dirent of dirents) {
|
|
165
|
+
if (matches.length >= MAX_MATCHES || guard.checkStopped()) break;
|
|
166
|
+
if (shouldExcludeChild(current.real, dirent.name)) continue;
|
|
167
|
+
const full = path.join(current.dir, dirent.name);
|
|
168
|
+
|
|
169
|
+
if (dirent.isDirectory()) {
|
|
170
|
+
try {
|
|
171
|
+
const childReal = await fs.realpath(full);
|
|
172
|
+
if (isSafeSymlinkTarget(childReal, rootReal, context) && !seen.has(childReal)) {
|
|
173
|
+
queue.push({ dir: full, real: childReal, depth: current.depth + 1 });
|
|
174
|
+
}
|
|
175
|
+
} catch {
|
|
176
|
+
// Ignore unreadable directories.
|
|
177
|
+
}
|
|
178
|
+
} else if (dirent.isFile()) {
|
|
179
|
+
if (!guard.countFile()) break;
|
|
180
|
+
scanned++;
|
|
181
|
+
if (isMatch(dirent.name, keywords, extensions, fuzzyMatch)) {
|
|
182
|
+
matches.push(full);
|
|
183
|
+
}
|
|
184
|
+
} else if (dirent.isSymbolicLink()) {
|
|
185
|
+
try {
|
|
186
|
+
const targetReal = await fs.realpath(full);
|
|
187
|
+
if (!isSafeSymlinkTarget(targetReal, rootReal, context)) continue;
|
|
188
|
+
const targetStat = await fs.stat(targetReal);
|
|
189
|
+
if (targetStat.isDirectory()) {
|
|
190
|
+
if (!seen.has(targetReal)) {
|
|
191
|
+
queue.push({ dir: targetReal, real: targetReal, depth: current.depth + 1 });
|
|
192
|
+
}
|
|
193
|
+
} else if (targetStat.isFile()) {
|
|
194
|
+
if (!guard.countFile()) break;
|
|
195
|
+
scanned++;
|
|
196
|
+
if (isMatch(dirent.name, keywords, extensions, fuzzyMatch)) {
|
|
197
|
+
matches.push(full);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
} catch {
|
|
201
|
+
// Ignore broken symlinks.
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (guard.stopReason === 'aborted') {
|
|
208
|
+
return { success: false, aborted: true, error: 'File find aborted.' };
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return {
|
|
212
|
+
success: true,
|
|
213
|
+
output: JSON.stringify(
|
|
214
|
+
{
|
|
215
|
+
scanned_files: scanned,
|
|
216
|
+
total_matches: matches.length,
|
|
217
|
+
...(guard.stopReason ? { stopped_reason: guard.stopNotice() } : {}),
|
|
218
|
+
matches: matches.length > 0 ? matches : 'No files matched your search parameters.'
|
|
219
|
+
},
|
|
220
|
+
null,
|
|
221
|
+
2
|
|
222
|
+
)
|
|
223
|
+
};
|
|
224
|
+
} catch (error: unknown) {
|
|
225
|
+
return { success: false, error: error instanceof Error ? error.message : String(error) };
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
};
|