@carrieros/cli 0.3.0 → 0.4.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 +34 -13
- package/dist/commands/chat.js +3 -3
- package/dist/commands/context.js +40 -0
- package/dist/commands/doctor.js +27 -12
- package/dist/commands/logout.js +1 -0
- package/dist/commands/permissions.js +77 -0
- package/dist/commands/scope.js +9 -6
- package/dist/commands/sessions.js +5 -0
- package/dist/commands/settings.js +8 -1
- package/dist/commands/update.js +79 -9
- package/dist/commands/usage.js +5 -5
- package/dist/commands/whoami.js +12 -9
- package/dist/ide/editor-context.js +115 -12
- package/dist/ide/ide-session.js +274 -13
- package/dist/ide/tool-host.js +2 -2
- package/dist/main.js +0 -0
- package/dist/services/api.js +178 -12
- package/dist/services/auth-origin.js +20 -0
- package/dist/services/chat-stream.js +26 -3
- package/dist/services/checkpoints.js +176 -14
- package/dist/services/client-tools.js +113 -21
- package/dist/services/code-intel.js +243 -0
- package/dist/services/code-symbols.js +94 -0
- package/dist/services/coding-workflow.js +554 -0
- package/dist/services/command-coordinator.js +85 -12
- package/dist/services/command-policy.js +34 -4
- package/dist/services/config.js +67 -5
- package/dist/services/container-runner.js +254 -0
- package/dist/services/container-supervisor.js +38 -0
- package/dist/services/context.js +45 -17
- package/dist/services/device-auth.js +30 -5
- package/dist/services/execution-platform.js +30 -0
- package/dist/services/file-resolver.js +1 -1
- package/dist/services/folder-key.js +22 -0
- package/dist/services/git-tools.js +181 -0
- package/dist/services/helper-bin.js +56 -35
- package/dist/services/instruction-resolver.js +54 -0
- package/dist/services/job-helper.js +31 -10
- package/dist/services/local-files.js +148 -26
- package/dist/services/local-tools.js +540 -35
- package/dist/services/login-flow.js +43 -12
- package/dist/services/mutation-coordinator.js +186 -20
- package/dist/services/mutation-groups.js +309 -0
- package/dist/services/mutation-recovery.js +159 -0
- package/dist/services/mutations.js +264 -78
- package/dist/services/packaged-update.js +403 -0
- package/dist/services/packaged.js +15 -0
- package/dist/services/permission-mode.js +60 -0
- package/dist/services/process-runner.js +7 -1
- package/dist/services/project-context.js +4 -4
- package/dist/services/result-reconciliation.js +49 -0
- package/dist/services/secure-store.js +21 -1
- package/dist/services/session-resolver.js +5 -0
- package/dist/services/supported-runtime.js +59 -0
- package/dist/services/task-context.js +212 -0
- package/dist/services/task-snapshot.js +38 -0
- package/dist/services/tool-result-outbox.js +195 -0
- package/dist/services/update.js +37 -12
- package/dist/services/workspace-registry.js +4 -3
- package/dist/services/workspace.js +8 -1
- package/dist/shell/shell-chat.js +299 -32
- package/dist/shell/shell-commands.js +46 -4
- package/dist/shell/shell-prompt.js +21 -11
- package/dist/shell/shell-renderer.js +1 -1
- package/dist/shell/shell.js +69 -7
- package/dist/shell/task-context-view.js +40 -0
- package/dist/ui/agent-cards.js +111 -0
- package/dist/ui/agent-presentation.js +131 -0
- package/dist/ui/approval.js +46 -16
- package/dist/ui/banner.js +31 -87
- package/dist/ui/change-review.js +293 -46
- package/dist/ui/chat-renderer.js +6 -6
- package/dist/ui/colors.js +9 -9
- package/dist/ui/command-details.js +11 -0
- package/dist/ui/command-palette.js +27 -10
- package/dist/ui/command-registry.js +9 -2
- package/dist/ui/command-review.js +77 -28
- package/dist/ui/composer-layout.js +26 -20
- package/dist/ui/conversation.js +77 -25
- package/dist/ui/live.js +1 -1
- package/dist/ui/screen.js +19 -11
- package/dist/ui/select.js +6 -0
- package/dist/ui/task-context-card.js +99 -0
- package/dist/ui/trust.js +7 -12
- package/dist/ui/ui.js +23 -0
- package/package.json +8 -5
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
/** Repository instruction sources, ordered from general to specific. */
|
|
4
|
+
export const INSTRUCTION_FILE_NAMES = ["AGENTS.md", "CLAUDE.md"];
|
|
5
|
+
/**
|
|
6
|
+
* Resolve instruction files that apply to a set of repository-relative
|
|
7
|
+
* directories. Results are ordered root-to-leaf, with AGENTS.md before
|
|
8
|
+
* CLAUDE.md at each scope, so later, narrower guidance can take precedence.
|
|
9
|
+
*
|
|
10
|
+
* This only discovers paths; callers still read the files through the normal
|
|
11
|
+
* containment-checked file reader. In particular, a symlink outside the
|
|
12
|
+
* selected repository is never treated as project guidance.
|
|
13
|
+
*/
|
|
14
|
+
export function instructionPathsForDirectories(repositoryRoot, directories) {
|
|
15
|
+
const root = fs.realpathSync.native(repositoryRoot);
|
|
16
|
+
const scopes = new Set(["."]);
|
|
17
|
+
for (const directory of directories) {
|
|
18
|
+
if (typeof directory !== "string" || !directory.trim() || path.isAbsolute(directory) || /[:\0]/.test(directory)) {
|
|
19
|
+
throw new Error("Instruction scopes must be repository-relative paths.");
|
|
20
|
+
}
|
|
21
|
+
const normalized = path.normalize(directory);
|
|
22
|
+
const parts = normalized.split(path.sep).filter((part) => part && part !== ".");
|
|
23
|
+
if (parts.includes(".."))
|
|
24
|
+
throw new Error("Instruction scopes must stay inside the repository.");
|
|
25
|
+
let current = ".";
|
|
26
|
+
for (const part of parts) {
|
|
27
|
+
current = current === "." ? part : path.join(current, part);
|
|
28
|
+
scopes.add(current);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const orderedScopes = [...scopes].sort((a, b) => {
|
|
32
|
+
const depth = (value) => value === "." ? 0 : value.split(path.sep).length;
|
|
33
|
+
return depth(a) - depth(b) || a.localeCompare(b);
|
|
34
|
+
});
|
|
35
|
+
const found = [];
|
|
36
|
+
for (const scope of orderedScopes) {
|
|
37
|
+
for (const name of INSTRUCTION_FILE_NAMES) {
|
|
38
|
+
const absolute = path.join(root, scope, name);
|
|
39
|
+
try {
|
|
40
|
+
if (!fs.statSync(absolute).isFile())
|
|
41
|
+
continue;
|
|
42
|
+
const actual = fs.realpathSync.native(absolute);
|
|
43
|
+
const relative = path.relative(root, actual);
|
|
44
|
+
if (relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative))
|
|
45
|
+
continue;
|
|
46
|
+
found.push(path.relative(root, absolute).split(path.sep).join("/"));
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
// Missing, unreadable and broken-link instruction files are ignored.
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return found;
|
|
54
|
+
}
|
|
@@ -3,7 +3,8 @@ import { execFileSync } from "node:child_process";
|
|
|
3
3
|
import fs from "node:fs";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import { ConfigStore } from "./config.js";
|
|
6
|
-
import { findPrebuiltHelper } from "./helper-bin.js";
|
|
6
|
+
import { damagedInstallMessage, findPrebuiltHelper } from "./helper-bin.js";
|
|
7
|
+
import { isPackaged } from "./packaged.js";
|
|
7
8
|
/**
|
|
8
9
|
* The Windows process-containment helper.
|
|
9
10
|
*
|
|
@@ -205,12 +206,34 @@ static class CosJobRunner
|
|
|
205
206
|
Console.Error.WriteLine("cos-job-runner: AssignProcessToJobObject: " + reason);
|
|
206
207
|
return HelperFailure;
|
|
207
208
|
}
|
|
208
|
-
ResumeThread(pi.hThread)
|
|
209
|
+
if (ResumeThread(pi.hThread) == 0xFFFFFFFF)
|
|
210
|
+
{
|
|
211
|
+
int failure = Fail("ResumeThread");
|
|
212
|
+
TerminateJobObject(job, HelperFailure);
|
|
213
|
+
CloseHandle(pi.hThread);
|
|
214
|
+
CloseHandle(pi.hProcess);
|
|
215
|
+
CloseHandle(job);
|
|
216
|
+
return failure;
|
|
217
|
+
}
|
|
209
218
|
CloseHandle(pi.hThread);
|
|
210
219
|
|
|
211
|
-
WaitForSingleObject(pi.hProcess, INFINITE)
|
|
220
|
+
if (WaitForSingleObject(pi.hProcess, INFINITE) != 0)
|
|
221
|
+
{
|
|
222
|
+
int failure = Fail("WaitForSingleObject");
|
|
223
|
+
TerminateJobObject(job, HelperFailure);
|
|
224
|
+
CloseHandle(pi.hProcess);
|
|
225
|
+
CloseHandle(job);
|
|
226
|
+
return failure;
|
|
227
|
+
}
|
|
212
228
|
uint code;
|
|
213
|
-
GetExitCodeProcess(pi.hProcess, out code)
|
|
229
|
+
if (!GetExitCodeProcess(pi.hProcess, out code))
|
|
230
|
+
{
|
|
231
|
+
int failure = Fail("GetExitCodeProcess");
|
|
232
|
+
TerminateJobObject(job, HelperFailure);
|
|
233
|
+
CloseHandle(pi.hProcess);
|
|
234
|
+
CloseHandle(job);
|
|
235
|
+
return failure;
|
|
236
|
+
}
|
|
214
237
|
CloseHandle(pi.hProcess);
|
|
215
238
|
|
|
216
239
|
// Whatever the program left running dies with it.
|
|
@@ -243,7 +266,6 @@ function findCompiler() {
|
|
|
243
266
|
}
|
|
244
267
|
return null;
|
|
245
268
|
}
|
|
246
|
-
let verified = null;
|
|
247
269
|
/**
|
|
248
270
|
* Absolute path to a verified helper, compiling it if needed.
|
|
249
271
|
* Throws JobHelperError when containment cannot be provided — callers must then
|
|
@@ -257,18 +279,18 @@ export function ensureJobHelper() {
|
|
|
257
279
|
// compiler, source or npm involved. Use it when present and intact.
|
|
258
280
|
const prebuilt = findPrebuiltHelper("cos-job-runner");
|
|
259
281
|
if (prebuilt) {
|
|
260
|
-
verified = prebuilt;
|
|
261
282
|
return prebuilt;
|
|
262
283
|
}
|
|
284
|
+
// A packaged build never compiles: no prebuilt helper means a damaged install.
|
|
285
|
+
if (isPackaged()) {
|
|
286
|
+
throw new JobHelperError("HELPER_UNAVAILABLE", `${damagedInstallMessage("cos-job-runner.exe")} Nothing was run.`);
|
|
287
|
+
}
|
|
263
288
|
const tag = sha256(JOB_HELPER_SOURCE).slice(0, 16);
|
|
264
289
|
const dir = helperDir();
|
|
265
290
|
const exe = path.join(dir, `cos-job-runner-${tag}.exe`);
|
|
266
291
|
const stamp = `${exe}.sha256`;
|
|
267
|
-
if (verified === exe && fs.existsSync(exe))
|
|
268
|
-
return exe;
|
|
269
292
|
if (fs.existsSync(exe) && fs.existsSync(stamp)) {
|
|
270
293
|
if (sha256(fs.readFileSync(exe)) === fs.readFileSync(stamp, "utf8").trim()) {
|
|
271
|
-
verified = exe;
|
|
272
294
|
return exe;
|
|
273
295
|
}
|
|
274
296
|
// Corrupt or tampered: rebuild rather than run it.
|
|
@@ -302,6 +324,5 @@ export function ensureJobHelper() {
|
|
|
302
324
|
finally {
|
|
303
325
|
fs.rmSync(srcFile, { force: true });
|
|
304
326
|
}
|
|
305
|
-
verified = exe;
|
|
306
327
|
return exe;
|
|
307
328
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { enclosingSymbol } from "./code-symbols.js";
|
|
1
2
|
import fs from "node:fs";
|
|
2
3
|
import path from "node:path";
|
|
3
4
|
import { compileGlob, toPosix } from "./glob-match.js";
|
|
@@ -93,13 +94,18 @@ export function isInside(root, candidate) {
|
|
|
93
94
|
* repository containing a link to `C:\Users`.
|
|
94
95
|
*/
|
|
95
96
|
export function resolveInside(input, root = workspaceRoot()) {
|
|
97
|
+
// A foreign Windows drive must not become a relative POSIX filename.
|
|
98
|
+
// Native absolute Windows paths still pass through the normal root checks.
|
|
99
|
+
if ((/^[A-Za-z]:/.test(input) || input.startsWith("\\\\")) && !path.isAbsolute(input)) {
|
|
100
|
+
throw new LocalFileError("OUTSIDE_WORKSPACE", `Outside the workspace: ${input}`);
|
|
101
|
+
}
|
|
96
102
|
const abs = path.resolve(root, input);
|
|
97
103
|
if (!isInside(root, abs)) {
|
|
98
104
|
throw new LocalFileError("OUTSIDE_WORKSPACE", `Outside the workspace: ${input}`);
|
|
99
105
|
}
|
|
100
106
|
try {
|
|
101
107
|
const real = fs.realpathSync.native(abs);
|
|
102
|
-
if (!isInside(root, real)) {
|
|
108
|
+
if (!isInside(fs.realpathSync.native(root), real)) {
|
|
103
109
|
throw new LocalFileError("OUTSIDE_WORKSPACE", `${input} links outside the workspace.`);
|
|
104
110
|
}
|
|
105
111
|
return real;
|
|
@@ -131,8 +137,33 @@ export function walkRepository(opts = {}) {
|
|
|
131
137
|
const entries = [];
|
|
132
138
|
const ignore = new IgnoreMatcher();
|
|
133
139
|
const state = { visited: 0, skipped: 0, truncated: false, stop: false };
|
|
134
|
-
const startRel = opts.start ? toPosix(opts.start) : "";
|
|
140
|
+
const startRel = opts.start ? toPosix(path.normalize(opts.start)).replace(/^\.\/?$/, "").replace(/\/+$/, "") : "";
|
|
135
141
|
const startAbs = startRel ? resolveInside(startRel, root) : root;
|
|
142
|
+
// Starting below the root must not shed the rules above it: a root
|
|
143
|
+
// .gitignore that says `src/generated/` still governs a walk from `src`.
|
|
144
|
+
// Push every ancestor's .gitignore first, and refuse a start that those
|
|
145
|
+
// rules (or the hard excludes) already exclude — the same answer a direct
|
|
146
|
+
// read of that path gets from isPathIgnored.
|
|
147
|
+
if (startRel) {
|
|
148
|
+
const parts = startRel.split("/").filter(Boolean);
|
|
149
|
+
let absDir = root;
|
|
150
|
+
let relDir = "";
|
|
151
|
+
for (let i = 0; i < parts.length; i += 1) {
|
|
152
|
+
try {
|
|
153
|
+
ignore.push(fs.readFileSync(path.join(absDir, ".gitignore"), "utf8"), relDir);
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
/* no .gitignore at this level */
|
|
157
|
+
}
|
|
158
|
+
absDir = path.join(absDir, parts[i]);
|
|
159
|
+
relDir = relDir ? `${relDir}/${parts[i]}` : parts[i];
|
|
160
|
+
// Every component of a start is a directory, so directory-only rules
|
|
161
|
+
// such as `generated/` apply to it.
|
|
162
|
+
if (ignore.shouldIgnore(relDir, { isDirectory: true })) {
|
|
163
|
+
return { entries: [], truncated: false, visited: 0, skipped: 0 };
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
136
167
|
const descend = (absDir, relDir, depth) => {
|
|
137
168
|
if (state.stop)
|
|
138
169
|
return;
|
|
@@ -302,7 +333,7 @@ export function getFileMetadata(input) {
|
|
|
302
333
|
catch {
|
|
303
334
|
throw new LocalFileError("NOT_FOUND", `No such file: ${input.path}`);
|
|
304
335
|
}
|
|
305
|
-
const rel = toPosix(path.relative(root, abs)) || toPosix(path.basename(abs));
|
|
336
|
+
const rel = toPosix(path.relative(fs.realpathSync.native(root), abs)) || toPosix(path.basename(abs));
|
|
306
337
|
return {
|
|
307
338
|
path: rel,
|
|
308
339
|
size: st.isDirectory() ? 0 : st.size,
|
|
@@ -320,13 +351,37 @@ export function getFileMetadata(input) {
|
|
|
320
351
|
* A NUL byte in the first few KB, which is the same heuristic Git uses. Cheap,
|
|
321
352
|
* and wrong only for exotic encodings the CLI would not render usefully anyway.
|
|
322
353
|
*/
|
|
323
|
-
function looksBinary(buf) {
|
|
354
|
+
export function looksBinary(buf) {
|
|
324
355
|
const end = Math.min(buf.length, SNIFF_BYTES);
|
|
325
356
|
for (let i = 0; i < end; i += 1)
|
|
326
357
|
if (buf[i] === 0)
|
|
327
358
|
return true;
|
|
328
359
|
return false;
|
|
329
360
|
}
|
|
361
|
+
/**
|
|
362
|
+
* Slice whole lines out of `text` without normalizing them.
|
|
363
|
+
*
|
|
364
|
+
* Line endings are kept byte-for-byte, so a range covering the whole file is
|
|
365
|
+
* the whole file — which is what lets the coding workflow keep treating only a
|
|
366
|
+
* byte-identical read as a complete one.
|
|
367
|
+
*/
|
|
368
|
+
export function sliceLines(text, startLine, endLine) {
|
|
369
|
+
const starts = [0];
|
|
370
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
371
|
+
const ch = text.charCodeAt(i);
|
|
372
|
+
if (ch === 10 || (ch === 13 && text.charCodeAt(i + 1) !== 10))
|
|
373
|
+
starts.push(i + 1);
|
|
374
|
+
}
|
|
375
|
+
// A trailing newline ends the last line; it does not open another.
|
|
376
|
+
const totalLines = text.length === 0 ? 0 : starts[starts.length - 1] === text.length ? starts.length - 1 : starts.length;
|
|
377
|
+
const start = Math.max(1, Math.floor(startLine));
|
|
378
|
+
const end = Math.min(totalLines, Math.max(start, Math.floor(endLine)));
|
|
379
|
+
if (start > totalLines)
|
|
380
|
+
return { startLine: start, endLine: start - 1, totalLines, text: "" };
|
|
381
|
+
const from = starts[start - 1];
|
|
382
|
+
const to = end < totalLines ? starts[end] : text.length;
|
|
383
|
+
return { startLine: start, endLine: end, totalLines, text: text.slice(from, to) };
|
|
384
|
+
}
|
|
330
385
|
/**
|
|
331
386
|
* Read a text file from inside the workspace.
|
|
332
387
|
*
|
|
@@ -368,7 +423,7 @@ export function readFile(input) {
|
|
|
368
423
|
throw new LocalFileError("BINARY", `${input.path} is not a text file.`);
|
|
369
424
|
}
|
|
370
425
|
return {
|
|
371
|
-
path: toPosix(path.relative(root, abs)),
|
|
426
|
+
path: toPosix(path.relative(fs.realpathSync.native(root), abs)),
|
|
372
427
|
text: buf.toString("utf8"),
|
|
373
428
|
size: st.size,
|
|
374
429
|
truncated: st.size > wanted
|
|
@@ -419,42 +474,93 @@ export function isPathIgnored(relPath, root = workspaceRoot()) {
|
|
|
419
474
|
export function globFiles(opts) {
|
|
420
475
|
const re = compileGlob(toPosix(opts.pattern));
|
|
421
476
|
const limit = opts.limit ?? 500;
|
|
422
|
-
const
|
|
477
|
+
const entries = [];
|
|
423
478
|
const res = walkRepository({
|
|
424
479
|
root: opts.root,
|
|
480
|
+
start: opts.path,
|
|
425
481
|
includeDirectories: false,
|
|
426
482
|
signal: opts.signal,
|
|
427
483
|
onEntry: (e) => {
|
|
428
484
|
if (e.type === "file" && re.test(e.path))
|
|
429
|
-
|
|
430
|
-
return
|
|
485
|
+
entries.push(e);
|
|
486
|
+
return entries.length < limit;
|
|
431
487
|
}
|
|
432
488
|
});
|
|
433
|
-
return { paths, truncated: res.truncated ||
|
|
489
|
+
return { paths: entries.map((e) => e.path), entries, truncated: res.truncated || entries.length >= limit };
|
|
434
490
|
}
|
|
491
|
+
/** Longest regular expression accepted. Real searches are nowhere near this. */
|
|
492
|
+
export const MAX_REGEX_CHARS = 512;
|
|
493
|
+
/** Lines longer than this are not tested against a regex (minified bundles, data). */
|
|
494
|
+
const MAX_REGEX_LINE_CHARS = 4000;
|
|
495
|
+
/** Longest context line returned; context is orientation, not content. */
|
|
496
|
+
const MAX_CONTEXT_LINE_CHARS = 400;
|
|
497
|
+
export const MAX_CONTEXT_LINES = 5;
|
|
435
498
|
/**
|
|
436
|
-
*
|
|
499
|
+
* Compile a model-supplied pattern, refusing the shapes that backtrack
|
|
500
|
+
* catastrophically.
|
|
437
501
|
*
|
|
438
|
-
*
|
|
439
|
-
*
|
|
440
|
-
*
|
|
441
|
-
*
|
|
442
|
-
*
|
|
502
|
+
* JavaScript has no regex timeout, so the defence is structural: a bounded
|
|
503
|
+
* pattern length, a bounded line length, and no quantified group that itself
|
|
504
|
+
* contains a quantifier (`(a+)+`, `(\w*)*`, `(x|y+){2,}`) — the classic ReDoS
|
|
505
|
+
* shape. Backreferences are refused for the same reason. A refused pattern is
|
|
506
|
+
* an error the model can rewrite, never a silent literal fallback.
|
|
507
|
+
*/
|
|
508
|
+
export function compileSearchRegex(pattern, caseSensitive) {
|
|
509
|
+
if (pattern.length > MAX_REGEX_CHARS)
|
|
510
|
+
throw new LocalFileError("INVALID_PATTERN", "The regular expression is too long.");
|
|
511
|
+
if (/\\[1-9]|\\k</.test(pattern)) {
|
|
512
|
+
throw new LocalFileError("INVALID_PATTERN", "Backreferences are not supported in search patterns.");
|
|
513
|
+
}
|
|
514
|
+
if (/\((?:[^()\\]|\\.)*(?:[+*]|\{\d+,?\d*\})(?:[^()\\]|\\.)*\)\s*(?:[+*]|\{\d+,?\d*\})/.test(pattern)) {
|
|
515
|
+
throw new LocalFileError("INVALID_PATTERN", "Nested quantifiers such as (a+)+ are not supported; simplify the pattern.");
|
|
516
|
+
}
|
|
517
|
+
try {
|
|
518
|
+
return new RegExp(pattern, caseSensitive ? "" : "i");
|
|
519
|
+
}
|
|
520
|
+
catch (err) {
|
|
521
|
+
throw new LocalFileError("INVALID_PATTERN", `Invalid regular expression: ${err.message}`);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
const clip = (line) => line.length > MAX_CONTEXT_LINE_CHARS ? `${line.slice(0, MAX_CONTEXT_LINE_CHARS)}…` : line;
|
|
525
|
+
/**
|
|
526
|
+
* Text search across the workspace.
|
|
527
|
+
*
|
|
528
|
+
* Literal substring matching by default; a JavaScript regular expression when
|
|
529
|
+
* `regex` is set. Never semantic search — a query is what it says it is. Binary
|
|
530
|
+
* files are sniffed and skipped rather than scanned as text, large files are
|
|
531
|
+
* skipped by size, and low-signal files (lockfiles, minified assets, images)
|
|
532
|
+
* never make it here at all because the ignore vocabulary already removed them.
|
|
533
|
+
* The walk is the same contained, ignore-aware walk listing uses, optionally
|
|
534
|
+
* started from a subdirectory that is resolved inside the root.
|
|
443
535
|
*/
|
|
444
536
|
export function searchFiles(opts) {
|
|
445
537
|
const limit = opts.limit ?? 50;
|
|
446
538
|
const perFile = opts.maxPerFile ?? 3;
|
|
539
|
+
const context = Math.min(MAX_CONTEXT_LINES, Math.max(0, Math.floor(opts.contextLines ?? 0)));
|
|
447
540
|
const needle = opts.caseSensitive ? opts.query : opts.query.toLowerCase();
|
|
541
|
+
const pattern = opts.regex ? compileSearchRegex(opts.query, Boolean(opts.caseSensitive)) : null;
|
|
448
542
|
const globRe = opts.glob ? compileGlob(toPosix(opts.glob)) : null;
|
|
449
543
|
const matches = [];
|
|
450
544
|
let filesScanned = 0;
|
|
545
|
+
let filesMatched = 0;
|
|
546
|
+
let longLinesSkipped = 0;
|
|
451
547
|
// Hoisted: this is per-search, not per-file.
|
|
452
548
|
const root = opts.root ?? workspaceRoot();
|
|
549
|
+
const lineMatches = (line) => {
|
|
550
|
+
if (!pattern)
|
|
551
|
+
return (opts.caseSensitive ? line : line.toLowerCase()).includes(needle);
|
|
552
|
+
if (line.length > MAX_REGEX_LINE_CHARS) {
|
|
553
|
+
longLinesSkipped += 1;
|
|
554
|
+
return false;
|
|
555
|
+
}
|
|
556
|
+
return pattern.test(line);
|
|
557
|
+
};
|
|
453
558
|
const res = walkRepository({
|
|
454
559
|
// The walk MUST cover the same repository the reads below resolve against —
|
|
455
560
|
// hoisted `root` and an unrooted walk would list one repository's files and
|
|
456
561
|
// read another's.
|
|
457
562
|
root,
|
|
563
|
+
start: opts.path,
|
|
458
564
|
includeDirectories: false,
|
|
459
565
|
signal: opts.signal,
|
|
460
566
|
onEntry: (e) => {
|
|
@@ -462,6 +568,8 @@ export function searchFiles(opts) {
|
|
|
462
568
|
return true;
|
|
463
569
|
if (globRe && !globRe.test(e.path))
|
|
464
570
|
return true;
|
|
571
|
+
if (opts.include && !opts.include(e.path))
|
|
572
|
+
return true;
|
|
465
573
|
if (isLowSignalFile(e.path.slice(e.path.lastIndexOf("/") + 1)))
|
|
466
574
|
return true;
|
|
467
575
|
if (e.size > MAX_SEARCH_FILE_BYTES)
|
|
@@ -480,22 +588,34 @@ export function searchFiles(opts) {
|
|
|
480
588
|
// Reject the whole file before splitting it. Most files do not contain
|
|
481
589
|
// the term at all, and lowercasing them line by line to discover that
|
|
482
590
|
// was the entire cost of a search: 2.5s on a 1000-file repository, spent
|
|
483
|
-
// almost entirely on allocating strings nobody ever looked at.
|
|
484
|
-
|
|
485
|
-
if (!
|
|
486
|
-
|
|
487
|
-
|
|
591
|
+
// almost entirely on allocating strings nobody ever looked at. (Literal
|
|
592
|
+
// mode only: a regex cannot be prefiltered this way.)
|
|
593
|
+
if (!pattern) {
|
|
594
|
+
const whole = opts.caseSensitive ? text : text.toLowerCase();
|
|
595
|
+
if (!whole.includes(needle))
|
|
596
|
+
return true;
|
|
597
|
+
}
|
|
598
|
+
const lines = text.split(/\r\n|\r|\n/);
|
|
488
599
|
let hits = 0;
|
|
489
600
|
for (let i = 0; i < lines.length; i += 1) {
|
|
490
|
-
|
|
491
|
-
if (!hay.includes(needle))
|
|
601
|
+
if (!lineMatches(lines[i]))
|
|
492
602
|
continue;
|
|
493
|
-
|
|
603
|
+
if (hits === 0)
|
|
604
|
+
filesMatched += 1;
|
|
494
605
|
hits += 1;
|
|
606
|
+
if (hits > perFile)
|
|
607
|
+
break;
|
|
608
|
+
const match = { path: e.path, line: i + 1, text: lines[i].trim() };
|
|
609
|
+
const symbol = enclosingSymbol(lines, i);
|
|
610
|
+
if (symbol)
|
|
611
|
+
match.symbol = symbol;
|
|
612
|
+
if (context > 0) {
|
|
613
|
+
match.before = lines.slice(Math.max(0, i - context), i).map(clip);
|
|
614
|
+
match.after = lines.slice(i + 1, i + 1 + context).map(clip);
|
|
615
|
+
}
|
|
616
|
+
matches.push(match);
|
|
495
617
|
if (matches.length >= limit)
|
|
496
618
|
return false;
|
|
497
|
-
if (hits >= perFile)
|
|
498
|
-
break;
|
|
499
619
|
}
|
|
500
620
|
return true;
|
|
501
621
|
}
|
|
@@ -503,8 +623,10 @@ export function searchFiles(opts) {
|
|
|
503
623
|
return {
|
|
504
624
|
matches,
|
|
505
625
|
filesScanned,
|
|
626
|
+
filesMatched,
|
|
506
627
|
truncated: res.truncated || matches.length >= limit,
|
|
507
|
-
aborted: Boolean(opts.signal?.aborted)
|
|
628
|
+
aborted: Boolean(opts.signal?.aborted),
|
|
629
|
+
...(pattern ? { longLinesSkipped } : {})
|
|
508
630
|
};
|
|
509
631
|
}
|
|
510
632
|
/** "14.2 KB" — sizes read in the terminal, not in bytes. */
|