@carrieros/cli 0.3.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/README.md +89 -15
  2. package/dist/commands/chat.js +6 -6
  3. package/dist/commands/context.js +40 -0
  4. package/dist/commands/doctor.js +34 -14
  5. package/dist/commands/logout.js +2 -0
  6. package/dist/commands/permissions.js +78 -0
  7. package/dist/commands/scope.js +9 -6
  8. package/dist/commands/sessions.js +5 -0
  9. package/dist/commands/settings.js +83 -10
  10. package/dist/commands/update.js +177 -51
  11. package/dist/commands/usage.js +10 -8
  12. package/dist/commands/version.js +1 -3
  13. package/dist/commands/whoami.js +12 -9
  14. package/dist/ide/editor-context.js +115 -12
  15. package/dist/ide/ide-session.js +288 -18
  16. package/dist/ide/tool-host.js +6 -6
  17. package/dist/main.js +10 -1
  18. package/dist/services/api.js +183 -15
  19. package/dist/services/auth-origin.js +20 -0
  20. package/dist/services/auto-update.js +221 -0
  21. package/dist/services/chat-stream.js +26 -3
  22. package/dist/services/checkpoints.js +176 -14
  23. package/dist/services/client-tools.js +123 -27
  24. package/dist/services/code-intel.js +243 -0
  25. package/dist/services/code-symbols.js +94 -0
  26. package/dist/services/coding-workflow.js +554 -0
  27. package/dist/services/command-coordinator.js +85 -12
  28. package/dist/services/command-policy.js +34 -4
  29. package/dist/services/config.js +96 -11
  30. package/dist/services/container-runner.js +254 -0
  31. package/dist/services/container-supervisor.js +38 -0
  32. package/dist/services/context.js +98 -25
  33. package/dist/services/device-auth.js +33 -6
  34. package/dist/services/execution-platform.js +30 -0
  35. package/dist/services/file-resolver.js +1 -1
  36. package/dist/services/folder-key.js +22 -0
  37. package/dist/services/git-tools.js +181 -0
  38. package/dist/services/helper-bin.js +56 -35
  39. package/dist/services/instruction-resolver.js +54 -0
  40. package/dist/services/job-helper.js +31 -10
  41. package/dist/services/local-files.js +148 -26
  42. package/dist/services/local-tools.js +540 -35
  43. package/dist/services/login-flow.js +44 -13
  44. package/dist/services/meta.js +27 -0
  45. package/dist/services/mutation-coordinator.js +186 -20
  46. package/dist/services/mutation-groups.js +309 -0
  47. package/dist/services/mutation-recovery.js +159 -0
  48. package/dist/services/mutations.js +264 -78
  49. package/dist/services/packaged-update.js +403 -0
  50. package/dist/services/packaged.js +15 -0
  51. package/dist/services/permission-mode.js +78 -0
  52. package/dist/services/process-runner.js +7 -1
  53. package/dist/services/project-context.js +4 -4
  54. package/dist/services/result-reconciliation.js +49 -0
  55. package/dist/services/secure-store.js +21 -1
  56. package/dist/services/session-resolver.js +7 -0
  57. package/dist/services/supported-runtime.js +59 -0
  58. package/dist/services/task-context.js +212 -0
  59. package/dist/services/task-snapshot.js +38 -0
  60. package/dist/services/tool-result-outbox.js +195 -0
  61. package/dist/services/update.js +457 -13
  62. package/dist/services/workspace-registry.js +4 -3
  63. package/dist/services/workspace.js +8 -1
  64. package/dist/shell/shell-chat.js +320 -34
  65. package/dist/shell/shell-commands.js +47 -5
  66. package/dist/shell/shell-prompt.js +21 -11
  67. package/dist/shell/shell-renderer.js +1 -1
  68. package/dist/shell/shell.js +107 -20
  69. package/dist/shell/task-context-view.js +40 -0
  70. package/dist/ui/agent-cards.js +111 -0
  71. package/dist/ui/agent-presentation.js +131 -0
  72. package/dist/ui/approval.js +46 -16
  73. package/dist/ui/banner.js +31 -87
  74. package/dist/ui/change-review.js +293 -46
  75. package/dist/ui/chat-renderer.js +6 -6
  76. package/dist/ui/colors.js +9 -9
  77. package/dist/ui/command-details.js +11 -0
  78. package/dist/ui/command-palette.js +27 -10
  79. package/dist/ui/command-registry.js +11 -4
  80. package/dist/ui/command-review.js +77 -28
  81. package/dist/ui/composer-layout.js +26 -20
  82. package/dist/ui/conversation.js +77 -25
  83. package/dist/ui/live.js +1 -1
  84. package/dist/ui/screen.js +19 -11
  85. package/dist/ui/select.js +6 -0
  86. package/dist/ui/task-context-card.js +99 -0
  87. package/dist/ui/trust.js +7 -12
  88. package/dist/ui/ui.js +23 -0
  89. package/package.json +9 -5
@@ -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 paths = [];
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
- paths.push(e.path);
430
- return paths.length < limit;
485
+ entries.push(e);
486
+ return entries.length < limit;
431
487
  }
432
488
  });
433
- return { paths, truncated: res.truncated || paths.length >= limit };
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
- * Plain text search across the workspace.
499
+ * Compile a model-supplied pattern, refusing the shapes that backtrack
500
+ * catastrophically.
437
501
  *
438
- * Literal substring matching, not a regular expression and emphatically not
439
- * semantic search — a query is what it says it is. Binary files are sniffed and
440
- * skipped rather than scanned as text, large files are skipped by size, and
441
- * low-signal files (lockfiles, minified assets, images) never make it here at
442
- * all because the ignore vocabulary already removed them.
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
- const whole = opts.caseSensitive ? text : text.toLowerCase();
485
- if (!whole.includes(needle))
486
- return true;
487
- const lines = text.split(/\r?\n/);
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
- const hay = opts.caseSensitive ? lines[i] : lines[i].toLowerCase();
491
- if (!hay.includes(needle))
601
+ if (!lineMatches(lines[i]))
492
602
  continue;
493
- matches.push({ path: e.path, line: i + 1, text: lines[i].trim() });
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. */