@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
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
import fsSync from 'node:fs';
|
|
2
|
+
import fs from 'node:fs/promises';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { isPhysicalPathAllowed, isProtectedPhysicalPath, isWithinPhysicalPath, physicalPath } from '../agent/workspaceBoundary.js';
|
|
6
|
+
export const DEFAULT_TIMEOUT_MS = 15_000;
|
|
7
|
+
export const DEFAULT_MAX_DIRS = 10_000;
|
|
8
|
+
export const DEFAULT_MAX_FILES = 25_000;
|
|
9
|
+
export const DEFAULT_MAX_DEPTH = 16;
|
|
10
|
+
export const MAX_FILE_BYTES = 2 * 1024 * 1024;
|
|
11
|
+
export const SNIFF_BYTES = 8192;
|
|
12
|
+
export const MAX_REGEX_LENGTH = 500;
|
|
13
|
+
export const MAX_QUANTIFIER_REPEAT = 1000;
|
|
14
|
+
export const HEAVY_DEV_EXCLUDES = new Set([
|
|
15
|
+
'node_modules', '.git', '.hg', '.svn', 'dist', 'build', 'out', 'target',
|
|
16
|
+
'.gradle', '.m2', '.idea', '.vscode', '.next', '.nuxt', '.cache', '__pycache__',
|
|
17
|
+
'venv', '.venv', 'vendor', 'Pods', 'DerivedData', 'coverage', '.terraform',
|
|
18
|
+
'.causyne_trash', '.npm', '.nvm', '.pyenv', '.rustup', '.cargo', '.cocoapods',
|
|
19
|
+
'.pub-cache'
|
|
20
|
+
]);
|
|
21
|
+
/**
|
|
22
|
+
* Heavy user-home folders skipped ONLY when they sit directly inside $HOME.
|
|
23
|
+
* Explicitly passing e.g. ~/Library/Logs or ~/.causyne/logs as searchPath still works.
|
|
24
|
+
*/
|
|
25
|
+
export const HOME_NOISE_EXCLUDES = new Set([
|
|
26
|
+
'Library', '.Trash', 'Applications', 'Movies', 'Music', 'Pictures',
|
|
27
|
+
'CloudStorage', 'CoreSimulator', '.causyne', '.opencode', '.local',
|
|
28
|
+
'.android', 'AppData', '$RECYCLE.BIN', 'System Volume Information'
|
|
29
|
+
]);
|
|
30
|
+
export const BINARY_EXTENSIONS_RE = /\.(png|jpe?g|webp|gif|svg|bmp|ico|woff2?|ttf|otf|eot|blend\d?|glb|gltf|wasm|zip|tar|gz|bz2|xz|7z|rar|mp4|mov|avi|mkv|webm|mp3|wav|ogg|flac|aac|m4a|pdf|exe|dll|dylib|so|jar|class|o|a|bin|iso|dmg|img|sqlite|db)$/i;
|
|
31
|
+
export function expandHomePath(raw) {
|
|
32
|
+
const trimmed = raw.trim();
|
|
33
|
+
const home = os.homedir();
|
|
34
|
+
if (trimmed === '~')
|
|
35
|
+
return home;
|
|
36
|
+
if (trimmed.startsWith('~/') || trimmed.startsWith('~\\')) {
|
|
37
|
+
return path.join(home, trimmed.slice(2));
|
|
38
|
+
}
|
|
39
|
+
return trimmed;
|
|
40
|
+
}
|
|
41
|
+
function defaultBaseDir(context) {
|
|
42
|
+
const fromLoop = context?.parentLoop?.workspaceRoot;
|
|
43
|
+
if (typeof fromLoop === 'string' && fromLoop.trim()) {
|
|
44
|
+
return path.resolve(expandHomePath(fromLoop));
|
|
45
|
+
}
|
|
46
|
+
const firstWs = context?.workspacePaths?.find((entry) => typeof entry === 'string' && entry.trim().length > 0);
|
|
47
|
+
if (firstWs) {
|
|
48
|
+
return path.resolve(expandHomePath(firstWs));
|
|
49
|
+
}
|
|
50
|
+
return process.cwd();
|
|
51
|
+
}
|
|
52
|
+
export function resolveSearchRoot(rawPath, context) {
|
|
53
|
+
const baseDir = defaultBaseDir(context);
|
|
54
|
+
if (typeof rawPath === 'string' && rawPath.trim()) {
|
|
55
|
+
const expanded = expandHomePath(rawPath);
|
|
56
|
+
return path.resolve(baseDir, expanded);
|
|
57
|
+
}
|
|
58
|
+
return baseDir;
|
|
59
|
+
}
|
|
60
|
+
export function checkSearchScope(candidatePath, context) {
|
|
61
|
+
const expanded = expandHomePath(candidatePath);
|
|
62
|
+
const resolved = path.resolve(defaultBaseDir(context), expanded);
|
|
63
|
+
const real = physicalPath(resolved);
|
|
64
|
+
if (!real) {
|
|
65
|
+
return {
|
|
66
|
+
allowed: false,
|
|
67
|
+
resolvedPath: resolved,
|
|
68
|
+
realPath: null,
|
|
69
|
+
error: `Path is inaccessible or resolves through a broken/looping symlink: ${candidatePath}`
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
if (isProtectedPhysicalPath(resolved) || isProtectedPhysicalPath(real)) {
|
|
73
|
+
return {
|
|
74
|
+
allowed: false,
|
|
75
|
+
resolvedPath: resolved,
|
|
76
|
+
realPath: real,
|
|
77
|
+
error: `Cannot search protected system path: ${candidatePath}`
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
if (Array.isArray(context?.workspacePaths) &&
|
|
81
|
+
context.workspacePaths.length > 0 &&
|
|
82
|
+
context.grantAllAccess !== true) {
|
|
83
|
+
const allowedRoots = context.workspacePaths
|
|
84
|
+
.filter((entry) => typeof entry === 'string' && entry.trim().length > 0)
|
|
85
|
+
.map(expandHomePath);
|
|
86
|
+
if (allowedRoots.length > 0 && !isPhysicalPathAllowed(resolved, allowedRoots, false)) {
|
|
87
|
+
return {
|
|
88
|
+
allowed: false,
|
|
89
|
+
resolvedPath: resolved,
|
|
90
|
+
realPath: real,
|
|
91
|
+
error: `Path outside allowed workspace: ${candidatePath}`
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
allowed: true,
|
|
97
|
+
resolvedPath: resolved,
|
|
98
|
+
realPath: real
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
export function isSafeSymlinkTarget(linkTargetReal, rootReal, context) {
|
|
102
|
+
if (isProtectedPhysicalPath(linkTargetReal))
|
|
103
|
+
return false;
|
|
104
|
+
if (!isWithinPhysicalPath(linkTargetReal, rootReal))
|
|
105
|
+
return false;
|
|
106
|
+
if (Array.isArray(context?.workspacePaths) &&
|
|
107
|
+
context.workspacePaths.length > 0 &&
|
|
108
|
+
context.grantAllAccess !== true) {
|
|
109
|
+
const allowedRoots = context.workspacePaths
|
|
110
|
+
.filter((entry) => typeof entry === 'string' && entry.trim().length > 0)
|
|
111
|
+
.map(expandHomePath);
|
|
112
|
+
if (allowedRoots.length > 0 && !isPhysicalPathAllowed(linkTargetReal, allowedRoots, false)) {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
export function normalizeComparePath(value) {
|
|
119
|
+
const resolved = path.resolve(value);
|
|
120
|
+
return process.platform === 'win32' ? resolved.toLowerCase() : resolved;
|
|
121
|
+
}
|
|
122
|
+
function realHomeDir() {
|
|
123
|
+
const home = os.homedir();
|
|
124
|
+
try {
|
|
125
|
+
return fsSync.realpathSync.native(home);
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
return path.resolve(home);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
export function shouldExcludeChild(currentDirReal, childName, ignorePatterns = []) {
|
|
132
|
+
if (HEAVY_DEV_EXCLUDES.has(childName))
|
|
133
|
+
return true;
|
|
134
|
+
if (HOME_NOISE_EXCLUDES.has(childName) &&
|
|
135
|
+
normalizeComparePath(currentDirReal) === normalizeComparePath(realHomeDir())) {
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
for (const pattern of ignorePatterns) {
|
|
139
|
+
if (pattern === childName)
|
|
140
|
+
return true;
|
|
141
|
+
if (pattern.startsWith('*') && childName.endsWith(pattern.slice(1)))
|
|
142
|
+
return true;
|
|
143
|
+
if (pattern.endsWith('*') && childName.startsWith(pattern.slice(0, -1)))
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
export async function loadGitIgnoreNames(rootDir) {
|
|
149
|
+
const patterns = [];
|
|
150
|
+
try {
|
|
151
|
+
const raw = await fs.readFile(path.join(rootDir, '.gitignore'), 'utf8');
|
|
152
|
+
for (const line of raw.split('\n')) {
|
|
153
|
+
const trimmed = line.trim();
|
|
154
|
+
if (!trimmed || trimmed.startsWith('#') || trimmed.startsWith('!'))
|
|
155
|
+
continue;
|
|
156
|
+
let start = 0;
|
|
157
|
+
let end = trimmed.length;
|
|
158
|
+
while (start < end && trimmed[start] === '/')
|
|
159
|
+
start++;
|
|
160
|
+
while (end > start && trimmed[end - 1] === '/')
|
|
161
|
+
end--;
|
|
162
|
+
const cleaned = trimmed.slice(start, end);
|
|
163
|
+
if (cleaned)
|
|
164
|
+
patterns.push(cleaned);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
catch {
|
|
168
|
+
// Missing or unreadable .gitignore is normal outside repos.
|
|
169
|
+
}
|
|
170
|
+
return patterns;
|
|
171
|
+
}
|
|
172
|
+
export async function looksBinaryFile(filePath) {
|
|
173
|
+
if (BINARY_EXTENSIONS_RE.test(filePath))
|
|
174
|
+
return true;
|
|
175
|
+
let handle;
|
|
176
|
+
try {
|
|
177
|
+
handle = await fs.open(filePath, 'r');
|
|
178
|
+
const buffer = Buffer.alloc(SNIFF_BYTES);
|
|
179
|
+
const { bytesRead } = await handle.read(buffer, 0, SNIFF_BYTES, 0);
|
|
180
|
+
return buffer.subarray(0, bytesRead).includes(0);
|
|
181
|
+
}
|
|
182
|
+
catch {
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
finally {
|
|
186
|
+
if (handle) {
|
|
187
|
+
await handle.close().catch(() => { });
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
export class TraversalGuard {
|
|
192
|
+
maxDirs;
|
|
193
|
+
maxFiles;
|
|
194
|
+
maxDepth;
|
|
195
|
+
timeoutMs;
|
|
196
|
+
signal;
|
|
197
|
+
startedAt;
|
|
198
|
+
dirsVisited = 0;
|
|
199
|
+
filesVisited = 0;
|
|
200
|
+
stopReason = null;
|
|
201
|
+
constructor(options = {}) {
|
|
202
|
+
this.maxDirs = options.maxDirs ?? DEFAULT_MAX_DIRS;
|
|
203
|
+
this.maxFiles = options.maxFiles ?? DEFAULT_MAX_FILES;
|
|
204
|
+
this.maxDepth = options.maxDepth ?? DEFAULT_MAX_DEPTH;
|
|
205
|
+
this.timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
206
|
+
this.signal = options.signal;
|
|
207
|
+
this.startedAt = Date.now();
|
|
208
|
+
}
|
|
209
|
+
checkStopped() {
|
|
210
|
+
if (this.stopReason)
|
|
211
|
+
return this.stopReason;
|
|
212
|
+
if (this.signal?.aborted) {
|
|
213
|
+
this.stopReason = 'aborted';
|
|
214
|
+
return this.stopReason;
|
|
215
|
+
}
|
|
216
|
+
if (Date.now() - this.startedAt >= this.timeoutMs) {
|
|
217
|
+
this.stopReason = 'timeout';
|
|
218
|
+
return this.stopReason;
|
|
219
|
+
}
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
enterDir(depth) {
|
|
223
|
+
if (this.checkStopped())
|
|
224
|
+
return false;
|
|
225
|
+
if (depth > this.maxDepth)
|
|
226
|
+
return false;
|
|
227
|
+
if (this.dirsVisited >= this.maxDirs) {
|
|
228
|
+
this.stopReason = 'max_dirs';
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
this.dirsVisited++;
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
234
|
+
countFile() {
|
|
235
|
+
if (this.checkStopped())
|
|
236
|
+
return false;
|
|
237
|
+
if (this.filesVisited >= this.maxFiles) {
|
|
238
|
+
this.stopReason = 'max_files';
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
this.filesVisited++;
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
stopNotice() {
|
|
245
|
+
switch (this.stopReason) {
|
|
246
|
+
case 'timeout':
|
|
247
|
+
return `stopped after ${this.timeoutMs}ms budget (narrow search path or filter to continue)`;
|
|
248
|
+
case 'max_dirs':
|
|
249
|
+
return `stopped after visiting ${this.dirsVisited} directories (narrow search path)`;
|
|
250
|
+
case 'max_files':
|
|
251
|
+
return `stopped after inspecting ${this.filesVisited} files (narrow search path)`;
|
|
252
|
+
case 'aborted':
|
|
253
|
+
return 'aborted';
|
|
254
|
+
default:
|
|
255
|
+
return '';
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
export function createTraversalGuard(options = {}) {
|
|
260
|
+
return new TraversalGuard(options);
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Linear-time O(n) character scanner that rejects pathological regex patterns
|
|
264
|
+
* (e.g. `(a+)+`, `(a|aa)+`, huge `{m,n}` counts, oversized patterns) without
|
|
265
|
+
* itself invoking any backtracking regular expression.
|
|
266
|
+
*/
|
|
267
|
+
export function validateRegexSafety(pattern) {
|
|
268
|
+
if (typeof pattern !== 'string' || pattern.length === 0) {
|
|
269
|
+
return 'Regular expression pattern must be a non-empty string.';
|
|
270
|
+
}
|
|
271
|
+
if (pattern.length > MAX_REGEX_LENGTH) {
|
|
272
|
+
return `Regular expression exceeds maximum length of ${MAX_REGEX_LENGTH} characters.`;
|
|
273
|
+
}
|
|
274
|
+
const stack = [{ hasMultiRepeat: false, hasAlternation: false }];
|
|
275
|
+
let inCharClass = false;
|
|
276
|
+
let lastTokenWasQuantifiable = false;
|
|
277
|
+
let lastGroupFrame = null;
|
|
278
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
279
|
+
const ch = pattern[i];
|
|
280
|
+
if (ch === '\\') {
|
|
281
|
+
if (i + 1 >= pattern.length) {
|
|
282
|
+
return 'Trailing backslash in regular expression.';
|
|
283
|
+
}
|
|
284
|
+
const next = pattern[i + 1];
|
|
285
|
+
if (!inCharClass && next >= '1' && next <= '9') {
|
|
286
|
+
return 'Backreferences are disallowed in search regular expressions for ReDoS safety.';
|
|
287
|
+
}
|
|
288
|
+
i++;
|
|
289
|
+
lastTokenWasQuantifiable = true;
|
|
290
|
+
lastGroupFrame = null;
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
if (inCharClass) {
|
|
294
|
+
if (ch === ']') {
|
|
295
|
+
inCharClass = false;
|
|
296
|
+
lastTokenWasQuantifiable = true;
|
|
297
|
+
lastGroupFrame = null;
|
|
298
|
+
}
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
if (ch === '[') {
|
|
302
|
+
inCharClass = true;
|
|
303
|
+
lastTokenWasQuantifiable = false;
|
|
304
|
+
lastGroupFrame = null;
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
if (ch === '(') {
|
|
308
|
+
stack.push({ hasMultiRepeat: false, hasAlternation: false });
|
|
309
|
+
lastTokenWasQuantifiable = false;
|
|
310
|
+
lastGroupFrame = null;
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
if (ch === ')') {
|
|
314
|
+
if (stack.length <= 1) {
|
|
315
|
+
lastTokenWasQuantifiable = false;
|
|
316
|
+
lastGroupFrame = null;
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
const popped = stack.pop();
|
|
320
|
+
const parent = stack[stack.length - 1];
|
|
321
|
+
if (popped.hasMultiRepeat)
|
|
322
|
+
parent.hasMultiRepeat = true;
|
|
323
|
+
lastGroupFrame = popped;
|
|
324
|
+
lastTokenWasQuantifiable = true;
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
if (ch === '|') {
|
|
328
|
+
const top = stack[stack.length - 1];
|
|
329
|
+
top.hasAlternation = true;
|
|
330
|
+
lastTokenWasQuantifiable = false;
|
|
331
|
+
lastGroupFrame = null;
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
let isMultiRepeat = false;
|
|
335
|
+
let isAnyQuantifier = false;
|
|
336
|
+
let jumpTo = i;
|
|
337
|
+
if (ch === '*' || ch === '+') {
|
|
338
|
+
isAnyQuantifier = true;
|
|
339
|
+
isMultiRepeat = true;
|
|
340
|
+
}
|
|
341
|
+
else if (ch === '?') {
|
|
342
|
+
if (lastTokenWasQuantifiable) {
|
|
343
|
+
isAnyQuantifier = true;
|
|
344
|
+
isMultiRepeat = false;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
else if (ch === '{') {
|
|
348
|
+
let j = i + 1;
|
|
349
|
+
let first = '';
|
|
350
|
+
let second = '';
|
|
351
|
+
let seenComma = false;
|
|
352
|
+
let validBrace = false;
|
|
353
|
+
while (j < pattern.length) {
|
|
354
|
+
const c = pattern[j];
|
|
355
|
+
if (c >= '0' && c <= '9') {
|
|
356
|
+
if (seenComma)
|
|
357
|
+
second += c;
|
|
358
|
+
else
|
|
359
|
+
first += c;
|
|
360
|
+
j++;
|
|
361
|
+
}
|
|
362
|
+
else if (c === ',' && !seenComma && first.length > 0) {
|
|
363
|
+
seenComma = true;
|
|
364
|
+
j++;
|
|
365
|
+
}
|
|
366
|
+
else if (c === '}' && first.length > 0) {
|
|
367
|
+
validBrace = true;
|
|
368
|
+
break;
|
|
369
|
+
}
|
|
370
|
+
else {
|
|
371
|
+
break;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
if (validBrace) {
|
|
375
|
+
const minCount = Number.parseInt(first, 10);
|
|
376
|
+
const maxCount = seenComma
|
|
377
|
+
? (second.length > 0 ? Number.parseInt(second, 10) : Infinity)
|
|
378
|
+
: minCount;
|
|
379
|
+
if (minCount > MAX_QUANTIFIER_REPEAT || (Number.isFinite(maxCount) && maxCount > MAX_QUANTIFIER_REPEAT)) {
|
|
380
|
+
return `Quantifier repetition count exceeds ${MAX_QUANTIFIER_REPEAT}.`;
|
|
381
|
+
}
|
|
382
|
+
isAnyQuantifier = true;
|
|
383
|
+
isMultiRepeat = maxCount > 1;
|
|
384
|
+
jumpTo = j;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
if (isAnyQuantifier) {
|
|
388
|
+
i = jumpTo;
|
|
389
|
+
if (i + 1 < pattern.length && pattern[i + 1] === '?') {
|
|
390
|
+
i++; // lazy modifier
|
|
391
|
+
}
|
|
392
|
+
if (isMultiRepeat) {
|
|
393
|
+
const top = stack[stack.length - 1];
|
|
394
|
+
top.hasMultiRepeat = true;
|
|
395
|
+
if (lastGroupFrame && (lastGroupFrame.hasMultiRepeat || lastGroupFrame.hasAlternation)) {
|
|
396
|
+
return 'Nested quantifiers are disallowed to prevent catastrophic backtracking (ReDoS).';
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
lastTokenWasQuantifiable = false;
|
|
400
|
+
lastGroupFrame = null;
|
|
401
|
+
continue;
|
|
402
|
+
}
|
|
403
|
+
lastTokenWasQuantifiable = true;
|
|
404
|
+
lastGroupFrame = null;
|
|
405
|
+
}
|
|
406
|
+
return null;
|
|
407
|
+
}
|
|
408
|
+
export function compileSafeSearchRegex(query, options = {}) {
|
|
409
|
+
if (typeof query !== 'string' || query.length === 0) {
|
|
410
|
+
return { error: 'Query must be a non-empty string.' };
|
|
411
|
+
}
|
|
412
|
+
const flags = options.caseInsensitive ? 'i' : '';
|
|
413
|
+
if (!options.isRegex) {
|
|
414
|
+
const escaped = query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
415
|
+
return { regex: new RegExp(escaped, flags) };
|
|
416
|
+
}
|
|
417
|
+
const unsafeReason = validateRegexSafety(query);
|
|
418
|
+
if (unsafeReason) {
|
|
419
|
+
return { error: unsafeReason };
|
|
420
|
+
}
|
|
421
|
+
try {
|
|
422
|
+
return { regex: new RegExp(query, flags) };
|
|
423
|
+
}
|
|
424
|
+
catch (error) {
|
|
425
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
426
|
+
return { error: `Invalid regular expression: ${msg}` };
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
export function buildHomeGlobExcludes() {
|
|
430
|
+
const excludes = [];
|
|
431
|
+
for (const name of HEAVY_DEV_EXCLUDES) {
|
|
432
|
+
excludes.push(`!**/${name}/**`);
|
|
433
|
+
}
|
|
434
|
+
return excludes;
|
|
435
|
+
}
|
|
436
|
+
export function buildTopLevelHomeExcludes(targetRealPath) {
|
|
437
|
+
if (normalizeComparePath(targetRealPath) !== normalizeComparePath(realHomeDir())) {
|
|
438
|
+
return [];
|
|
439
|
+
}
|
|
440
|
+
const excludes = [];
|
|
441
|
+
for (const name of HOME_NOISE_EXCLUDES) {
|
|
442
|
+
excludes.push(`!${name}/**`);
|
|
443
|
+
}
|
|
444
|
+
return excludes;
|
|
445
|
+
}
|